Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ChatRoom.spec.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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('element-name').t('element-name-text').up();
  43. parser.packet2JSON(p.tree(), nodes);
  44. expect(nodes.length).toBe(1);
  45. const elem = nodes.find(n => n.tagName === 'element-name');
  46. expect(elem).toBeTruthy();
  47. expect(Object.keys(elem.attributes).length).toEqual(0);
  48. expect(elem.children.length).toEqual(0);
  49. expect(elem.value).toEqual('element-name-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. '<x xmlns=\'http://jabber.org/protocol/muc#user\'>' +
  131. '<item jid=\'fulljid\'/>' +
  132. '</x>' +
  133. '<status>status-text</status>' +
  134. '</presence>';
  135. const pres = new DOMParser().parseFromString(presStr, 'text/xml').documentElement;
  136. room.onPresence(pres);
  137. expect(emitterSpy.calls.count()).toEqual(2);
  138. expect(emitterSpy.calls.argsFor(0)).toEqual([
  139. XMPPEvents.PRESENCE_RECEIVED,
  140. jasmine.any(Object)
  141. ]);
  142. expect(emitterSpy.calls.argsFor(1)).toEqual([
  143. XMPPEvents.MUC_MEMBER_JOINED,
  144. 'fromjid',
  145. undefined, // nick
  146. null, // role
  147. false, // isHiddenDomain
  148. undefined, // statsID
  149. 'status-text',
  150. undefined,
  151. undefined,
  152. 'fulljid'
  153. ]);
  154. });
  155. it('parses muc user item correctly', () => {
  156. const presStr = '' +
  157. '<presence to="tojid" from="fromjid">' +
  158. '<x xmlns="http://jabber.org/protocol/muc#user">' +
  159. '<item jid="jid=attr" affiliation="affiliation-attr" role="role-attr"/>' +
  160. '</x>' +
  161. '</presence>';
  162. const pres = new DOMParser().parseFromString(presStr, 'text/xml').documentElement;
  163. room.onPresence(pres);
  164. expect(emitterSpy.calls.count()).toEqual(2);
  165. expect(emitterSpy.calls.argsFor(0)).toEqual([
  166. XMPPEvents.PRESENCE_RECEIVED,
  167. jasmine.any(Object)
  168. ]);
  169. expect(emitterSpy).toHaveBeenCalledWith(
  170. XMPPEvents.MUC_MEMBER_JOINED,
  171. 'fromjid',
  172. undefined, // nick
  173. 'role-attr', // role
  174. jasmine.any(Boolean), // isHiddenDomain
  175. undefined, // statsID
  176. undefined,
  177. undefined,
  178. undefined,
  179. 'jid=attr');
  180. });
  181. it('parses identity correctly', () => {
  182. const presStr = '' +
  183. '<presence to="tojid" from="fromjid">' +
  184. '<x xmlns=\'http://jabber.org/protocol/muc#user\'>' +
  185. '<item jid=\'fulljid\'/>' +
  186. '</x>' +
  187. '<status>status-text</status>' +
  188. '<identity>' +
  189. '<user>' +
  190. '<id>id-text</id>' +
  191. '<name>name-text</name>' +
  192. '<avatar>avatar-text</avatar>' +
  193. '</user>' +
  194. '<group>group-text</group>' +
  195. '</identity>' +
  196. '</presence>';
  197. const pres = new DOMParser().parseFromString(presStr, 'text/xml').documentElement;
  198. const expectedIdentity = {
  199. user: {
  200. id: 'id-text',
  201. name: 'name-text',
  202. avatar: 'avatar-text'
  203. },
  204. group: 'group-text'
  205. };
  206. room.onPresence(pres);
  207. expect(emitterSpy.calls.count()).toEqual(2);
  208. expect(emitterSpy.calls.argsFor(0)).toEqual([
  209. XMPPEvents.PRESENCE_RECEIVED,
  210. jasmine.any(Object)
  211. ]);
  212. expect(emitterSpy.calls.argsFor(1)).toEqual([
  213. XMPPEvents.MUC_MEMBER_JOINED,
  214. 'fromjid',
  215. undefined, // nick
  216. null, // role
  217. false, // isHiddenDomain
  218. undefined, // statsID
  219. 'status-text',
  220. expectedIdentity,
  221. undefined,
  222. 'fulljid'
  223. ]);
  224. });
  225. it('parses bot correctly', () => {
  226. const expectedBotType = 'some_bot_type';
  227. const presStr = '' +
  228. '<presence to="tojid" from="fromjid">' +
  229. '<x xmlns=\'http://jabber.org/protocol/muc#user\'>' +
  230. '<item jid=\'fulljid\'/>' +
  231. '</x>' +
  232. '<status>status-text</status>' +
  233. `<bot type="${expectedBotType}"/>` +
  234. '</presence>';
  235. const pres = new DOMParser().parseFromString(presStr, 'text/xml').documentElement;
  236. room.onPresence(pres);
  237. expect(emitterSpy.calls.count()).toEqual(2);
  238. expect(emitterSpy.calls.argsFor(0)).toEqual([
  239. XMPPEvents.PRESENCE_RECEIVED,
  240. jasmine.any(Object)
  241. ]);
  242. expect(emitterSpy.calls.argsFor(1)).toEqual([
  243. XMPPEvents.MUC_MEMBER_JOINED,
  244. 'fromjid',
  245. undefined, // nick
  246. null, // role
  247. false, // isHiddenDomain
  248. undefined, // statsID
  249. 'status-text',
  250. undefined,
  251. expectedBotType,
  252. 'fulljid'
  253. ]);
  254. });
  255. });
  256. });