converse.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /* global mock, converse */
  2. const u = converse.env.utils;
  3. describe("Converse", function() {
  4. it("Can be inserted into a custom element after having been initialized",
  5. mock.initConverse([], {'root': new DocumentFragment()}, async (done) => {
  6. expect(document.body.querySelector('div#conversejs')).toBe(null);
  7. document.body.appendChild(document.createElement('converse-root'));
  8. await u.waitUntil(() => document.body.querySelector('div#conversejs') !== null);
  9. done();
  10. }));
  11. describe("Authentication", function () {
  12. it("needs either a bosh_service_url a websocket_url or both", mock.initConverse(async (done, _converse) => {
  13. const url = _converse.bosh_service_url;
  14. const connection = _converse.connection;
  15. _converse.api.settings.set('bosh_service_url', undefined);
  16. delete _converse.connection;
  17. try {
  18. await _converse.initConnection();
  19. } catch (e) {
  20. _converse.api.settings.set('bosh_service_url', url);
  21. _converse.connection = connection;
  22. expect(e.message).toBe("initConnection: you must supply a value for either the bosh_service_url or websocket_url or both.");
  23. done();
  24. }
  25. }));
  26. });
  27. describe("A chat state indication", function () {
  28. it("are sent out when the client becomes or stops being idle",
  29. mock.initConverse(['discoInitialized'], {}, (done, _converse) => {
  30. spyOn(_converse, 'sendCSI').and.callThrough();
  31. let sent_stanza;
  32. spyOn(_converse.connection, 'send').and.callFake(function (stanza) {
  33. sent_stanza = stanza;
  34. });
  35. let i = 0;
  36. _converse.idle_seconds = 0; // Usually initialized by registerIntervalHandler
  37. _converse.disco_entities.get(_converse.domain).features['urn:xmpp:csi:0'] = true; // Mock that the server supports CSI
  38. _converse.api.settings.set('csi_waiting_time', 3);
  39. while (i <= _converse.api.settings.get("csi_waiting_time")) {
  40. expect(_converse.sendCSI).not.toHaveBeenCalled();
  41. _converse.onEverySecond();
  42. i++;
  43. }
  44. expect(_converse.sendCSI).toHaveBeenCalledWith('inactive');
  45. expect(sent_stanza.toLocaleString()).toBe('<inactive xmlns="urn:xmpp:csi:0"/>');
  46. _converse.onUserActivity();
  47. expect(_converse.sendCSI).toHaveBeenCalledWith('active');
  48. expect(sent_stanza.toLocaleString()).toBe('<active xmlns="urn:xmpp:csi:0"/>');
  49. done();
  50. }));
  51. });
  52. describe("Automatic status change", function () {
  53. it("happens when the client is idle for long enough",
  54. mock.initConverse(['initialized'], {}, async (done, _converse) => {
  55. let i = 0;
  56. // Usually initialized by registerIntervalHandler
  57. _converse.idle_seconds = 0;
  58. _converse.auto_changed_status = false;
  59. _converse.api.settings.set('auto_away', 3);
  60. _converse.api.settings.set('auto_xa', 6);
  61. expect(await _converse.api.user.status.get()).toBe('online');
  62. while (i <= _converse.api.settings.get("auto_away")) {
  63. _converse.onEverySecond(); i++;
  64. }
  65. expect(_converse.auto_changed_status).toBe(true);
  66. while (i <= _converse.auto_xa) {
  67. expect(await _converse.api.user.status.get()).toBe('away');
  68. _converse.onEverySecond();
  69. i++;
  70. }
  71. expect(await _converse.api.user.status.get()).toBe('xa');
  72. expect(_converse.auto_changed_status).toBe(true);
  73. _converse.onUserActivity();
  74. expect(await _converse.api.user.status.get()).toBe('online');
  75. expect(_converse.auto_changed_status).toBe(false);
  76. // Check that it also works for the chat feature
  77. await _converse.api.user.status.set('chat')
  78. i = 0;
  79. while (i <= _converse.api.settings.get("auto_away")) {
  80. _converse.onEverySecond();
  81. i++;
  82. }
  83. expect(_converse.auto_changed_status).toBe(true);
  84. while (i <= _converse.auto_xa) {
  85. expect(await _converse.api.user.status.get()).toBe('away');
  86. _converse.onEverySecond();
  87. i++;
  88. }
  89. expect(await _converse.api.user.status.get()).toBe('xa');
  90. expect(_converse.auto_changed_status).toBe(true);
  91. _converse.onUserActivity();
  92. expect(await _converse.api.user.status.get()).toBe('online');
  93. expect(_converse.auto_changed_status).toBe(false);
  94. // Check that it doesn't work for 'dnd'
  95. await _converse.api.user.status.set('dnd');
  96. i = 0;
  97. while (i <= _converse.api.settings.get("auto_away")) {
  98. _converse.onEverySecond();
  99. i++;
  100. }
  101. expect(await _converse.api.user.status.get()).toBe('dnd');
  102. expect(_converse.auto_changed_status).toBe(false);
  103. while (i <= _converse.auto_xa) {
  104. expect(await _converse.api.user.status.get()).toBe('dnd');
  105. _converse.onEverySecond();
  106. i++;
  107. }
  108. expect(await _converse.api.user.status.get()).toBe('dnd');
  109. expect(_converse.auto_changed_status).toBe(false);
  110. _converse.onUserActivity();
  111. expect(await _converse.api.user.status.get()).toBe('dnd');
  112. expect(_converse.auto_changed_status).toBe(false);
  113. done();
  114. }));
  115. });
  116. describe("The \"user\" grouping", function () {
  117. describe("The \"status\" API", function () {
  118. it("has a method for getting the user's availability",
  119. mock.initConverse(['statusInitialized'], {}, async(done, _converse) => {
  120. _converse.xmppstatus.set('status', 'online');
  121. expect(await _converse.api.user.status.get()).toBe('online');
  122. _converse.xmppstatus.set('status', 'dnd');
  123. expect(await _converse.api.user.status.get()).toBe('dnd');
  124. done();
  125. }));
  126. it("has a method for setting the user's availability", mock.initConverse(async (done, _converse) => {
  127. await _converse.api.user.status.set('away');
  128. expect(await _converse.xmppstatus.get('status')).toBe('away');
  129. await _converse.api.user.status.set('dnd');
  130. expect(await _converse.xmppstatus.get('status')).toBe('dnd');
  131. await _converse.api.user.status.set('xa');
  132. expect(await _converse.xmppstatus.get('status')).toBe('xa');
  133. await _converse.api.user.status.set('chat');
  134. expect(await _converse.xmppstatus.get('status')).toBe('chat');
  135. const promise = _converse.api.user.status.set('invalid')
  136. promise.catch(e => {
  137. expect(e.message).toBe('Invalid availability value. See https://xmpp.org/rfcs/rfc3921.html#rfc.section.2.2.2.1');
  138. done();
  139. });
  140. }));
  141. it("allows setting the status message as well", mock.initConverse(async (done, _converse) => {
  142. await _converse.api.user.status.set('away', "I'm in a meeting");
  143. expect(_converse.xmppstatus.get('status')).toBe('away');
  144. expect(_converse.xmppstatus.get('status_message')).toBe("I'm in a meeting");
  145. done();
  146. }));
  147. it("has a method for getting the user's status message",
  148. mock.initConverse(['statusInitialized'], {}, async (done, _converse) => {
  149. await _converse.xmppstatus.set('status_message', undefined);
  150. expect(await _converse.api.user.status.message.get()).toBe(undefined);
  151. await _converse.xmppstatus.set('status_message', "I'm in a meeting");
  152. expect(await _converse.api.user.status.message.get()).toBe("I'm in a meeting");
  153. done();
  154. }));
  155. it("has a method for setting the user's status message",
  156. mock.initConverse(['statusInitialized'], {}, async (done, _converse) => {
  157. _converse.xmppstatus.set('status_message', undefined);
  158. await _converse.api.user.status.message.set("I'm in a meeting");
  159. expect(_converse.xmppstatus.get('status_message')).toBe("I'm in a meeting");
  160. done();
  161. }));
  162. });
  163. });
  164. describe("The \"tokens\" API", function () {
  165. it("has a method for retrieving the next RID", mock.initConverse((done, _converse) => {
  166. mock.createContacts(_converse, 'current');
  167. const old_connection = _converse.connection;
  168. _converse.connection._proto.rid = '1234';
  169. expect(_converse.api.tokens.get('rid')).toBe('1234');
  170. _converse.connection = undefined;
  171. expect(_converse.api.tokens.get('rid')).toBe(null);
  172. // Restore the connection
  173. _converse.connection = old_connection;
  174. done();
  175. }));
  176. it("has a method for retrieving the SID", mock.initConverse((done, _converse) => {
  177. mock.createContacts(_converse, 'current');
  178. const old_connection = _converse.connection;
  179. _converse.connection._proto.sid = '1234';
  180. expect(_converse.api.tokens.get('sid')).toBe('1234');
  181. _converse.connection = undefined;
  182. expect(_converse.api.tokens.get('sid')).toBe(null);
  183. // Restore the connection
  184. _converse.connection = old_connection;
  185. done();
  186. }));
  187. });
  188. describe("The \"contacts\" API", function () {
  189. it("has a method 'get' which returns wrapped contacts",
  190. mock.initConverse([], {}, async function (done, _converse) {
  191. await mock.waitForRoster(_converse, 'current');
  192. let contact = await _converse.api.contacts.get('non-existing@jabber.org');
  193. expect(contact).toBeFalsy();
  194. // Check when a single jid is given
  195. const jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit';
  196. contact = await _converse.api.contacts.get(jid);
  197. expect(contact.getDisplayName()).toBe(mock.cur_names[0]);
  198. expect(contact.get('jid')).toBe(jid);
  199. // You can retrieve multiple contacts by passing in an array
  200. const jid2 = mock.cur_names[1].replace(/ /g,'.').toLowerCase() + '@montague.lit';
  201. let list = await _converse.api.contacts.get([jid, jid2]);
  202. expect(Array.isArray(list)).toBeTruthy();
  203. expect(list[0].getDisplayName()).toBe(mock.cur_names[0]);
  204. expect(list[1].getDisplayName()).toBe(mock.cur_names[1]);
  205. // Check that all JIDs are returned if you call without any parameters
  206. list = await _converse.api.contacts.get();
  207. expect(list.length).toBe(mock.cur_names.length);
  208. done();
  209. }));
  210. it("has a method 'add' with which contacts can be added",
  211. mock.initConverse(['rosterInitialized'], {}, async (done, _converse) => {
  212. await mock.waitForRoster(_converse, 'current', 0);
  213. try {
  214. await _converse.api.contacts.add();
  215. throw new Error('Call should have failed');
  216. } catch (e) {
  217. expect(e.message).toBe('contacts.add: invalid jid');
  218. }
  219. try {
  220. await _converse.api.contacts.add("invalid jid");
  221. throw new Error('Call should have failed');
  222. } catch (e) {
  223. expect(e.message).toBe('contacts.add: invalid jid');
  224. }
  225. spyOn(_converse.roster, 'addAndSubscribe');
  226. await _converse.api.contacts.add("newcontact@example.org");
  227. expect(_converse.roster.addAndSubscribe).toHaveBeenCalled();
  228. done();
  229. }));
  230. });
  231. describe("The \"chats\" API", function() {
  232. it("has a method 'get' which returns the promise that resolves to a chat model", mock.initConverse(
  233. ['rosterInitialized', 'chatBoxesInitialized'], {},
  234. async (done, _converse) => {
  235. const u = converse.env.utils;
  236. await mock.openControlBox(_converse);
  237. await mock.waitForRoster(_converse, 'current', 2);
  238. // Test on chat that doesn't exist.
  239. let chat = await _converse.api.chats.get('non-existing@jabber.org');
  240. expect(chat).toBeFalsy();
  241. const jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit';
  242. const jid2 = mock.cur_names[1].replace(/ /g,'.').toLowerCase() + '@montague.lit';
  243. // Test on chat that's not open
  244. chat = await _converse.api.chats.get(jid);
  245. expect(chat === null).toBeTruthy();
  246. expect(_converse.chatboxes.length).toBe(1);
  247. // Test for one JID
  248. chat = await _converse.api.chats.open(jid);
  249. expect(chat instanceof Object).toBeTruthy();
  250. expect(chat.get('box_id')).toBe(`box-${jid}`);
  251. const view = _converse.chatboxviews.get(jid);
  252. await u.waitUntil(() => u.isVisible(view.el));
  253. // Test for multiple JIDs
  254. mock.openChatBoxFor(_converse, jid2);
  255. await u.waitUntil(() => _converse.chatboxes.length == 3);
  256. const list = await _converse.api.chats.get([jid, jid2]);
  257. expect(Array.isArray(list)).toBeTruthy();
  258. expect(list[0].get('box_id')).toBe(`box-${jid}`);
  259. expect(list[1].get('box_id')).toBe(`box-${jid2}`);
  260. done();
  261. }));
  262. it("has a method 'open' which opens and returns a promise that resolves to a chat model", mock.initConverse(
  263. ['rosterGroupsFetched', 'chatBoxesInitialized'], {},
  264. async (done, _converse) => {
  265. const u = converse.env.utils;
  266. await mock.openControlBox(_converse);
  267. await mock.waitForRoster(_converse, 'current', 2);
  268. const jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit';
  269. const jid2 = mock.cur_names[1].replace(/ /g,'.').toLowerCase() + '@montague.lit';
  270. // Test on chat that doesn't exist.
  271. let chat = await _converse.api.chats.get('non-existing@jabber.org');
  272. expect(chat).toBeFalsy();
  273. chat = await _converse.api.chats.open(jid);
  274. expect(chat instanceof Object).toBeTruthy();
  275. expect(chat.get('box_id')).toBe(`box-${jid}`);
  276. expect(
  277. Object.keys(chat),
  278. ['close', 'endOTR', 'focus', 'get', 'initiateOTR', 'is_chatroom', 'maximize', 'minimize', 'open', 'set']
  279. );
  280. const view = _converse.chatboxviews.get(jid);
  281. await u.waitUntil(() => u.isVisible(view.el));
  282. // Test for multiple JIDs
  283. const list = await _converse.api.chats.open([jid, jid2]);
  284. expect(Array.isArray(list)).toBeTruthy();
  285. expect(list[0].get('box_id')).toBe(`box-${jid}`);
  286. expect(list[1].get('box_id')).toBe(`box-${jid2}`);
  287. done();
  288. }));
  289. });
  290. describe("The \"settings\" API", function() {
  291. it("has methods 'get' and 'set' to set configuration settings",
  292. mock.initConverse(null, {'play_sounds': true}, (done, _converse) => {
  293. expect(Object.keys(_converse.api.settings)).toEqual(["extend", "update", "get", "set"]);
  294. expect(_converse.api.settings.get("play_sounds")).toBe(true);
  295. _converse.api.settings.set("play_sounds", false);
  296. expect(_converse.api.settings.get("play_sounds")).toBe(false);
  297. _converse.api.settings.set({"play_sounds": true});
  298. expect(_converse.api.settings.get("play_sounds")).toBe(true);
  299. // Only whitelisted settings allowed.
  300. expect(typeof _converse.api.settings.get("non_existing")).toBe("undefined");
  301. _converse.api.settings.set("non_existing", true);
  302. expect(typeof _converse.api.settings.get("non_existing")).toBe("undefined");
  303. done();
  304. }));
  305. it("extended via settings.extend don't override settings passed in via converse.initialize",
  306. mock.initConverse([], {'emoji_categories': {"travel": ":rocket:"}}, (done, _converse) => {
  307. expect(_converse.api.settings.get('emoji_categories')?.travel).toBe(':rocket:');
  308. // Test that the extend command doesn't override user-provided site
  309. // settings (i.e. settings passed in via converse.initialize).
  310. _converse.api.settings.extend({'emoji_categories': {"travel": ":motorcycle:", "food": ":burger:"}});
  311. expect(_converse.api.settings.get('emoji_categories')?.travel).toBe(':rocket:');
  312. expect(_converse.api.settings.get('emoji_categories')?.food).toBe(undefined);
  313. done();
  314. }));
  315. it("only overrides the passed in properties",
  316. mock.initConverse([],
  317. {
  318. 'root': document.createElement('div').attachShadow({ 'mode': 'open' }),
  319. 'emoji_categories': { 'travel': ':rocket:' },
  320. },
  321. (done, _converse) => {
  322. expect(_converse.api.settings.get('emoji_categories')?.travel).toBe(':rocket:');
  323. // Test that the extend command doesn't override user-provided site
  324. // settings (i.e. settings passed in via converse.initialize).
  325. _converse.api.settings.extend({
  326. 'emoji_categories': { 'travel': ':motorcycle:', 'food': ':burger:' },
  327. });
  328. expect(_converse.api.settings.get('emoji_categories').travel).toBe(':rocket:');
  329. expect(_converse.api.settings.get('emoji_categories').food).toBe(undefined);
  330. done();
  331. }
  332. )
  333. );
  334. });
  335. describe("The \"plugins\" API", function() {
  336. it("only has a method 'add' for registering plugins", mock.initConverse((done, _converse) => {
  337. expect(Object.keys(converse.plugins)).toEqual(["add"]);
  338. // Cheating a little bit. We clear the plugins to test more easily.
  339. const _old_plugins = _converse.pluggable.plugins;
  340. _converse.pluggable.plugins = [];
  341. converse.plugins.add('plugin1', {});
  342. expect(Object.keys(_converse.pluggable.plugins)).toEqual(['plugin1']);
  343. converse.plugins.add('plugin2', {});
  344. expect(Object.keys(_converse.pluggable.plugins)).toEqual(['plugin1', 'plugin2']);
  345. _converse.pluggable.plugins = _old_plugins;
  346. done();
  347. }));
  348. describe("The \"plugins.add\" method", function() {
  349. it("throws an error when multiple plugins attempt to register with the same name",
  350. mock.initConverse((done, _converse) => { // eslint-disable-line no-unused-vars
  351. converse.plugins.add('myplugin', {});
  352. const error = new TypeError('Error: plugin with name "myplugin" has already been registered!');
  353. expect(() => converse.plugins.add('myplugin', {})).toThrow(error);
  354. done();
  355. }));
  356. });
  357. });
  358. });