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 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. undefined);
  149. });
  150. it('parses muc user item correctly', () => {
  151. const presStr = '' +
  152. '<presence to="tojid" from="fromjid">' +
  153. '<x xmlns="http://jabber.org/protocol/muc#user">' +
  154. '<item jid="jid=attr" affiliation="affiliation-attr" role="role-attr"/>' +
  155. '</x>' +
  156. '</presence>';
  157. const pres = new DOMParser().parseFromString(presStr, 'text/xml').documentElement;
  158. room.onPresence(pres);
  159. expect(emitterSpy.calls.count()).toEqual(2);
  160. expect(emitterSpy.calls.argsFor(0)).toEqual([
  161. XMPPEvents.PRESENCE_RECEIVED,
  162. jasmine.any(Object)
  163. ]);
  164. expect(emitterSpy).toHaveBeenCalledWith(
  165. XMPPEvents.MUC_MEMBER_JOINED,
  166. 'fromjid',
  167. undefined, // nick
  168. 'role-attr', // role
  169. jasmine.any(Boolean), // isHiddenDomain
  170. undefined, // statsID
  171. undefined,
  172. undefined,
  173. undefined);
  174. });
  175. it('parses identity correctly', () => {
  176. const presStr = '' +
  177. '<presence to="tojid" from="fromjid">' +
  178. '<status>status-text</status>' +
  179. '<identity>' +
  180. '<user>' +
  181. '<id>id-text</id>' +
  182. '<name>name-text</name>' +
  183. '<avatar>avatar-text</avatar>' +
  184. '</user>' +
  185. '<group>group-text</group>' +
  186. '</identity>' +
  187. '</presence>';
  188. const pres = new DOMParser().parseFromString(presStr, 'text/xml').documentElement;
  189. const expectedIdentity = {
  190. user: {
  191. id: 'id-text',
  192. name: 'name-text',
  193. avatar: 'avatar-text'
  194. },
  195. group: 'group-text'
  196. };
  197. room.onPresence(pres);
  198. expect(emitterSpy.calls.count()).toEqual(2);
  199. expect(emitterSpy.calls.argsFor(0)).toEqual([
  200. XMPPEvents.PRESENCE_RECEIVED,
  201. jasmine.any(Object)
  202. ]);
  203. expect(emitterSpy).toHaveBeenCalledWith(
  204. XMPPEvents.MUC_MEMBER_JOINED,
  205. 'fromjid',
  206. undefined, // nick
  207. undefined, // role
  208. undefined, // isHiddenDomain
  209. undefined, // statsID
  210. 'status-text',
  211. expectedIdentity,
  212. undefined);
  213. });
  214. it('parses bot correctly', () => {
  215. const expectedBotType = 'some_bot_type';
  216. const presStr = '' +
  217. '<presence to="tojid" from="fromjid">' +
  218. '<status>status-text</status>' +
  219. `<bot type="${expectedBotType}"/>` +
  220. '</presence>';
  221. const pres = new DOMParser().parseFromString(presStr, 'text/xml').documentElement;
  222. room.onPresence(pres);
  223. expect(emitterSpy.calls.count()).toEqual(2);
  224. expect(emitterSpy.calls.argsFor(0)).toEqual([
  225. XMPPEvents.PRESENCE_RECEIVED,
  226. jasmine.any(Object)
  227. ]);
  228. expect(emitterSpy).toHaveBeenCalledWith(
  229. XMPPEvents.MUC_MEMBER_JOINED,
  230. 'fromjid',
  231. undefined, // nick
  232. undefined, // role
  233. undefined, // isHiddenDomain
  234. undefined, // statsID
  235. 'status-text',
  236. undefined,
  237. expectedBotType);
  238. });
  239. });
  240. });