選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ChatRoom.spec.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. addListener: () => {} // eslint-disable-line no-empty-function
  119. };
  120. room = new ChatRoom(
  121. {} /* connection */,
  122. 'jid',
  123. 'password',
  124. xmpp,
  125. {} /* options */);
  126. emitterSpy = spyOn(room.eventEmitter, 'emit');
  127. });
  128. it('parses status correctly', () => {
  129. const presStr = '' +
  130. '<presence to="tojid" from="fromjid">' +
  131. '<x xmlns=\'http://jabber.org/protocol/muc#user\'>' +
  132. '<item jid=\'fulljid\'/>' +
  133. '</x>' +
  134. '<status>status-text</status>' +
  135. '</presence>';
  136. const pres = new DOMParser().parseFromString(presStr, 'text/xml').documentElement;
  137. room.onPresence(pres);
  138. expect(emitterSpy.calls.count()).toEqual(2);
  139. expect(emitterSpy.calls.argsFor(0)).toEqual([
  140. XMPPEvents.PRESENCE_RECEIVED,
  141. jasmine.any(Object)
  142. ]);
  143. expect(emitterSpy.calls.argsFor(1)).toEqual([
  144. XMPPEvents.MUC_MEMBER_JOINED,
  145. 'fromjid',
  146. undefined, // nick
  147. null, // role
  148. false, // isHiddenDomain
  149. undefined, // statsID
  150. 'status-text',
  151. undefined,
  152. undefined,
  153. 'fulljid',
  154. undefined, // features
  155. 0 // isReplaceParticipant
  156. ]);
  157. });
  158. it('parses muc user item correctly', () => {
  159. const presStr = '' +
  160. '<presence to="tojid" from="fromjid">' +
  161. '<x xmlns="http://jabber.org/protocol/muc#user">' +
  162. '<item jid="jid=attr" affiliation="affiliation-attr" role="role-attr"/>' +
  163. '</x>' +
  164. '</presence>';
  165. const pres = new DOMParser().parseFromString(presStr, 'text/xml').documentElement;
  166. room.onPresence(pres);
  167. expect(emitterSpy.calls.count()).toEqual(2);
  168. expect(emitterSpy.calls.argsFor(0)).toEqual([
  169. XMPPEvents.PRESENCE_RECEIVED,
  170. jasmine.any(Object)
  171. ]);
  172. expect(emitterSpy).toHaveBeenCalledWith(
  173. XMPPEvents.MUC_MEMBER_JOINED,
  174. 'fromjid',
  175. undefined, // nick
  176. 'role-attr', // role
  177. jasmine.any(Boolean), // isHiddenDomain
  178. undefined, // statsID
  179. undefined,
  180. undefined,
  181. undefined,
  182. 'jid=attr',
  183. undefined, // features
  184. 0); // isReplaceParticipant
  185. });
  186. it('parses muc user replacing other user correctly', () => {
  187. const presStr = '' +
  188. '<presence to="tojid" from="fromjid">' +
  189. '<x xmlns="http://jabber.org/protocol/muc#user">' +
  190. '<item jid="jid=attr" affiliation="affiliation-attr" role="role-attr"/>' +
  191. '</x>' +
  192. '<flip_device />' +
  193. '</presence>';
  194. const pres = new DOMParser().parseFromString(presStr, 'text/xml').documentElement;
  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. 'role-attr', // role
  206. jasmine.any(Boolean), // isHiddenDomain
  207. undefined, // statsID
  208. undefined,
  209. undefined,
  210. undefined,
  211. 'jid=attr',
  212. undefined, // features
  213. 1); // isReplaceParticipant
  214. });
  215. it('parses identity correctly', () => {
  216. const presStr = '' +
  217. '<presence to="tojid" from="fromjid">' +
  218. '<x xmlns=\'http://jabber.org/protocol/muc#user\'>' +
  219. '<item jid=\'fulljid\'/>' +
  220. '</x>' +
  221. '<status>status-text</status>' +
  222. '<identity>' +
  223. '<user>' +
  224. '<id>id-text</id>' +
  225. '<name>name-text</name>' +
  226. '<avatar>avatar-text</avatar>' +
  227. '</user>' +
  228. '<group>group-text</group>' +
  229. '</identity>' +
  230. '</presence>';
  231. const pres = new DOMParser().parseFromString(presStr, 'text/xml').documentElement;
  232. const expectedIdentity = {
  233. user: {
  234. id: 'id-text',
  235. name: 'name-text',
  236. avatar: 'avatar-text'
  237. },
  238. group: 'group-text'
  239. };
  240. room.onPresence(pres);
  241. expect(emitterSpy.calls.count()).toEqual(2);
  242. expect(emitterSpy.calls.argsFor(0)).toEqual([
  243. XMPPEvents.PRESENCE_RECEIVED,
  244. jasmine.any(Object)
  245. ]);
  246. expect(emitterSpy.calls.argsFor(1)).toEqual([
  247. XMPPEvents.MUC_MEMBER_JOINED,
  248. 'fromjid',
  249. undefined, // nick
  250. null, // role
  251. false, // isHiddenDomain
  252. undefined, // statsID
  253. 'status-text',
  254. expectedIdentity,
  255. undefined,
  256. 'fulljid',
  257. undefined, // features
  258. 0 // isReplaceParticipant
  259. ]);
  260. });
  261. it('parses bot correctly', () => {
  262. const expectedBotType = 'some_bot_type';
  263. const presStr = '' +
  264. '<presence to="tojid" from="fromjid">' +
  265. '<x xmlns=\'http://jabber.org/protocol/muc#user\'>' +
  266. '<item jid=\'fulljid\'/>' +
  267. '</x>' +
  268. '<status>status-text</status>' +
  269. `<bot type="${expectedBotType}"/>` +
  270. '</presence>';
  271. const pres = new DOMParser().parseFromString(presStr, 'text/xml').documentElement;
  272. room.onPresence(pres);
  273. expect(emitterSpy.calls.count()).toEqual(2);
  274. expect(emitterSpy.calls.argsFor(0)).toEqual([
  275. XMPPEvents.PRESENCE_RECEIVED,
  276. jasmine.any(Object)
  277. ]);
  278. expect(emitterSpy.calls.argsFor(1)).toEqual([
  279. XMPPEvents.MUC_MEMBER_JOINED,
  280. 'fromjid',
  281. undefined, // nick
  282. null, // role
  283. false, // isHiddenDomain
  284. undefined, // statsID
  285. 'status-text',
  286. undefined,
  287. expectedBotType,
  288. 'fulljid',
  289. undefined, // features
  290. 0 // isReplaceParticipant
  291. ]);
  292. });
  293. });
  294. describe('sendMessage', () => {
  295. let room;
  296. let connectionSpy;
  297. beforeEach(() => {
  298. const xmpp = {
  299. options: {},
  300. addListener: () => {} // eslint-disable-line no-empty-function
  301. };
  302. room = new ChatRoom(
  303. // eslint-disable-next-line no-empty-function
  304. { send: () => {} } /* connection */,
  305. 'jid',
  306. 'password',
  307. xmpp,
  308. {} /* options */);
  309. connectionSpy = spyOn(room.connection, 'send');
  310. });
  311. it('sends a string msg with elementName body correctly', () => {
  312. room.sendMessage('string message', 'body', 'receiver');
  313. expect(connectionSpy.calls.argsFor(0).toString()).toBe(
  314. '<message to="jid" type="groupchat" xmlns="jabber:client">' +
  315. '<body>string message</body>' +
  316. '</message>');
  317. });
  318. it('sends a object msg with elementName body correctly', () => {
  319. room.sendMessage({ object: 'message' }, 'body', 'receiver');
  320. expect(connectionSpy.calls.argsFor(0).toString()).toBe(
  321. '<message to="jid" type="groupchat" xmlns="jabber:client">' +
  322. '<body object="message"/>' +
  323. '</message>');
  324. });
  325. it('sends a string msg with elementName json-message correctly', () => {
  326. room.sendMessage('string message', 'json-message', 'receiver');
  327. expect(connectionSpy.calls.argsFor(0).toString()).toBe(
  328. '<message to="jid" type="groupchat" xmlns="jabber:client">' +
  329. '<json-message xmlns="http://jitsi.org/jitmeet">string message</json-message>' +
  330. '</message>');
  331. });
  332. it('sends a object msg with elementName json-message correctly', () => {
  333. room.sendMessage({ object: 'message' }, 'json-message', 'receiver');
  334. expect(connectionSpy.calls.argsFor(0).toString()).toBe(
  335. '<message to="jid" type="groupchat" xmlns="jabber:client">' +
  336. '<json-message object="message" xmlns="http://jitsi.org/jitmeet"/>' +
  337. '</message>');
  338. });
  339. });
  340. });