您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ChatRoom.spec.js 11KB

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