You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ChatRoom.spec.js 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import ChatRoom, { parser } from './ChatRoom';
  2. import { $pres } from 'strophe.js';
  3. import XMPPEvents from '../../service/xmpp/XMPPEvents';
  4. // This rule makes creating the xml elements take up way more
  5. // space than necessary.
  6. /* eslint-disable newline-per-chained-call */
  7. // These rules makes the xml strings harder to read
  8. /* eslint-disable operator-linebreak, max-len */
  9. describe('ChatRoom', () => {
  10. describe('packet2JSON', () => {
  11. let nodes = [];
  12. beforeEach(() => {
  13. nodes = [];
  14. });
  15. it('translates attributes correctly', () => {
  16. const p = $pres({
  17. to: 'tojid',
  18. from: 'fromjid'
  19. })
  20. .c('fake-with-attr', {
  21. fakeAttr1: 'attrValue1',
  22. fakeAttr2: 'attrValue2'
  23. }).up();
  24. parser.packet2JSON(p.tree(), nodes);
  25. expect(nodes.length).toBe(1);
  26. const fakeWithAttr = nodes
  27. .find(n => n.tagName === 'fake-with-attr');
  28. expect(fakeWithAttr).toBeTruthy();
  29. expect(Object.keys(fakeWithAttr.attributes).length).toEqual(2);
  30. expect(fakeWithAttr.attributes.fakeAttr1).toBeTruthy();
  31. expect(fakeWithAttr.attributes.fakeAttr1).toEqual('attrValue1');
  32. expect(fakeWithAttr.attributes.fakeAttr2).toBeTruthy();
  33. expect(fakeWithAttr.attributes.fakeAttr2).toEqual('attrValue2');
  34. expect(fakeWithAttr.children.length).toEqual(0);
  35. expect(fakeWithAttr.value).toBeFalsy();
  36. });
  37. it('translates element text correctly', () => {
  38. const p = $pres({
  39. to: 'tojid',
  40. from: 'fromjid'
  41. })
  42. .c('user-agent').t('user-agent-text').up();
  43. parser.packet2JSON(p.tree(), nodes);
  44. expect(nodes.length).toBe(1);
  45. const userAgent = nodes.find(n => n.tagName === 'user-agent');
  46. expect(userAgent).toBeTruthy();
  47. expect(Object.keys(userAgent.attributes).length).toEqual(0);
  48. expect(userAgent.children.length).toEqual(0);
  49. expect(userAgent.value).toEqual('user-agent-text');
  50. });
  51. it('translates elements with children correctly', () => {
  52. const p = $pres({
  53. to: 'tojid',
  54. from: 'fromjid'
  55. })
  56. .c('identity')
  57. .c('user')
  58. .c('id').t('id-text').up()
  59. .c('name').t('name-text').up()
  60. .c('avatar').t('avatar-text').up()
  61. .up()
  62. .c('group').t('group-text').up()
  63. .up();
  64. parser.packet2JSON(p.tree(), nodes);
  65. const identity = nodes.find(n => n.tagName === 'identity');
  66. expect(identity).toBeTruthy();
  67. expect(Object.keys(identity.attributes).length).toEqual(0);
  68. expect(identity.children.length).toEqual(2);
  69. {
  70. const user = identity.children
  71. .find(n => n.tagName === 'user');
  72. expect(user).toBeTruthy();
  73. expect(Object.keys(user.attributes).length).toEqual(0);
  74. expect(user.children.length).toEqual(3);
  75. {
  76. const id = user.children
  77. .find(n => n.tagName === 'id');
  78. expect(id).toBeTruthy();
  79. expect(Object.keys(id.attributes).length).toEqual(0);
  80. expect(id.children.length).toEqual(0);
  81. expect(id.value).toEqual('id-text');
  82. }
  83. {
  84. const name = user.children
  85. .find(n => n.tagName === 'name');
  86. expect(name).toBeTruthy();
  87. expect(Object.keys(name.attributes).length).toEqual(0);
  88. expect(name.children.length).toEqual(0);
  89. expect(name.value).toEqual('name-text');
  90. }
  91. {
  92. const avatar = user.children
  93. .find(n => n.tagName === 'avatar');
  94. expect(avatar).toBeTruthy();
  95. expect(Object.keys(avatar.attributes).length).toEqual(0);
  96. expect(avatar.children.length).toEqual(0);
  97. expect(avatar.value).toEqual('avatar-text');
  98. }
  99. expect(user.value).toBeFalsy();
  100. }
  101. {
  102. const group = identity.children
  103. .find(n => n.tagName === 'group');
  104. expect(group).toBeTruthy();
  105. expect(Object.keys(group.attributes).length).toEqual(0);
  106. expect(group.children.length).toEqual(0);
  107. expect(group.value).toEqual('group-text');
  108. }
  109. expect(identity.value).toBeFalsy();
  110. });
  111. });
  112. describe('onPresence', () => {
  113. let room;
  114. let emitterSpy;
  115. beforeEach(() => {
  116. const xmpp = {
  117. options: {}
  118. };
  119. room = new ChatRoom(
  120. {} /* connection */,
  121. 'jid',
  122. 'password',
  123. xmpp,
  124. {} /* options */);
  125. emitterSpy = spyOn(room.eventEmitter, 'emit');
  126. });
  127. it('parses status correctly', () => {
  128. const presStr = '' +
  129. '<presence to="tojid" from="fromjid">' +
  130. '<status>status-text</status>' +
  131. '</presence>';
  132. const pres = new DOMParser().parseFromString(presStr, 'text/xml').documentElement;
  133. room.onPresence(pres);
  134. expect(emitterSpy.calls.count()).toEqual(2);
  135. expect(emitterSpy.calls.argsFor(0)).toEqual([
  136. XMPPEvents.PRESENCE_RECEIVED,
  137. jasmine.any(Object)
  138. ]);
  139. expect(emitterSpy).toHaveBeenCalledWith(
  140. XMPPEvents.MUC_MEMBER_JOINED,
  141. 'fromjid',
  142. undefined, // nick
  143. undefined, // role
  144. undefined, // isHiddenDomain
  145. undefined, // statsID
  146. 'status-text',
  147. undefined);
  148. });
  149. it('parses muc user item correctly', () => {
  150. const presStr = '' +
  151. '<presence to="tojid" from="fromjid">' +
  152. '<x xmlns="http://jabber.org/protocol/muc#user">' +
  153. '<item jid="jid=attr" affiliation="affiliation-attr" role="role-attr"/>' +
  154. '</x>' +
  155. '</presence>';
  156. const pres = new DOMParser().parseFromString(presStr, 'text/xml').documentElement;
  157. room.onPresence(pres);
  158. expect(emitterSpy.calls.count()).toEqual(2);
  159. expect(emitterSpy.calls.argsFor(0)).toEqual([
  160. XMPPEvents.PRESENCE_RECEIVED,
  161. jasmine.any(Object)
  162. ]);
  163. expect(emitterSpy).toHaveBeenCalledWith(
  164. XMPPEvents.MUC_MEMBER_JOINED,
  165. 'fromjid',
  166. undefined, // nick
  167. 'role-attr', // role
  168. jasmine.any(Boolean), // isHiddenDomain
  169. undefined, // statsID
  170. undefined,
  171. undefined);
  172. });
  173. it('parses identity correctly', () => {
  174. const presStr = '' +
  175. '<presence to="tojid" from="fromjid">' +
  176. '<status>status-text</status>' +
  177. '<identity>' +
  178. '<user>' +
  179. '<id>id-text</id>' +
  180. '<name>name-text</name>' +
  181. '<avatar>avatar-text</avatar>' +
  182. '</user>' +
  183. '<group>group-text</group>' +
  184. '</identity>' +
  185. '</presence>';
  186. const pres = new DOMParser().parseFromString(presStr, 'text/xml').documentElement;
  187. const expectedIdentity = {
  188. user: {
  189. id: 'id-text',
  190. name: 'name-text',
  191. avatar: 'avatar-text'
  192. },
  193. group: 'group-text'
  194. };
  195. room.onPresence(pres);
  196. expect(emitterSpy.calls.count()).toEqual(2);
  197. expect(emitterSpy.calls.argsFor(0)).toEqual([
  198. XMPPEvents.PRESENCE_RECEIVED,
  199. jasmine.any(Object)
  200. ]);
  201. expect(emitterSpy).toHaveBeenCalledWith(
  202. XMPPEvents.MUC_MEMBER_JOINED,
  203. 'fromjid',
  204. undefined, // nick
  205. undefined, // role
  206. undefined, // isHiddenDomain
  207. undefined, // statsID
  208. 'status-text',
  209. expectedIdentity);
  210. });
  211. });
  212. });