roster.js 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
  1. (function (root, factory) {
  2. define(["jquery", "jasmine", "mock", "test-utils"], factory);
  3. } (this, function ($, jasmine, mock, test_utils) {
  4. const $iq = converse.env.$iq;
  5. const $msg = converse.env.$msg;
  6. const $pres = converse.env.$pres;
  7. const Strophe = converse.env.Strophe;
  8. const _ = converse.env._;
  9. const sizzle = converse.env.sizzle;
  10. const u = converse.env.utils;
  11. const checkHeaderToggling = async function (group) {
  12. var toggle = group.querySelector('a.group-toggle');
  13. expect(u.isVisible(group)).toBeTruthy();
  14. expect(group.querySelectorAll('ul.collapsed').length).toBe(0);
  15. expect(u.hasClass('fa-caret-right', toggle.firstElementChild)).toBeFalsy();
  16. expect(u.hasClass('fa-caret-down', toggle.firstElementChild)).toBeTruthy();
  17. toggle.click();
  18. await test_utils.waitUntil(() => group.querySelectorAll('ul.collapsed').length === 1);
  19. expect(u.hasClass('fa-caret-right', toggle.firstElementChild)).toBeTruthy();
  20. expect(u.hasClass('fa-caret-down', toggle.firstElementChild)).toBeFalsy();
  21. toggle.click();
  22. await test_utils.waitUntil(() => group.querySelectorAll('li').length === $(group).find('li:visible').length);
  23. expect(u.hasClass('fa-caret-right', toggle.firstElementChild)).toBeFalsy();
  24. expect(u.hasClass('fa-caret-down', toggle.firstElementChild)).toBeTruthy();
  25. };
  26. describe("The Contacts Roster", function () {
  27. it("supports roster versioning",
  28. mock.initConverseWithPromises(
  29. null, ['rosterGroupsFetched'], {},
  30. async function (done, _converse) {
  31. const IQ_stanzas = _converse.connection.IQ_stanzas;
  32. let node = await test_utils.waitUntil(
  33. () => _.filter(IQ_stanzas, iq => iq.nodeTree.querySelector('iq query[xmlns="jabber:iq:roster"]')).pop()
  34. );
  35. let stanza = node.nodeTree;
  36. expect(_converse.roster.data.get('version')).toBeUndefined();
  37. expect(node.toLocaleString()).toBe(
  38. `<iq id="${stanza.getAttribute('id')}" type="get" xmlns="jabber:client">`+
  39. `<query xmlns="jabber:iq:roster"/>`+
  40. `</iq>`);
  41. let result = $iq({
  42. 'to': _converse.connection.jid,
  43. 'type': 'result',
  44. 'id': stanza.getAttribute('id')
  45. }).c('query', {
  46. 'xmlns': 'jabber:iq:roster',
  47. 'ver': 'ver7'
  48. }).c('item', {'jid': 'nurse@example.com'}).up()
  49. .c('item', {'jid': 'romeo@example.com'})
  50. _converse.connection._dataRecv(test_utils.createRequest(result));
  51. await test_utils.waitUntil(() => _converse.roster.models.length > 1);
  52. expect(_converse.roster.data.get('version')).toBe('ver7');
  53. expect(_converse.roster.models.length).toBe(2);
  54. _converse.roster.fetchFromServer();
  55. node = _converse.connection.IQ_stanzas.pop();
  56. stanza = node.nodeTree;
  57. expect(node.toLocaleString()).toBe(
  58. `<iq id="${stanza.getAttribute('id')}" type="get" xmlns="jabber:client">`+
  59. `<query ver="ver7" xmlns="jabber:iq:roster"/>`+
  60. `</iq>`);
  61. result = $iq({
  62. 'to': _converse.connection.jid,
  63. 'type': 'result',
  64. 'id': stanza.getAttribute('id')
  65. });
  66. _converse.connection._dataRecv(test_utils.createRequest(result));
  67. const roster_push = $iq({
  68. 'to': _converse.connection.jid,
  69. 'type': 'set',
  70. }).c('query', {'xmlns': 'jabber:iq:roster', 'ver': 'ver34'})
  71. .c('item', {'jid': 'romeo@example.com', 'subscription': 'remove'});
  72. _converse.connection._dataRecv(test_utils.createRequest(roster_push));
  73. expect(_converse.roster.data.get('version')).toBe('ver34');
  74. expect(_converse.roster.models.length).toBe(1);
  75. expect(_converse.roster.at(0).get('jid')).toBe('nurse@example.com');
  76. done();
  77. }));
  78. describe("The live filter", function () {
  79. it("will only appear when roster contacts flow over the visible area",
  80. mock.initConverseWithPromises(
  81. null, ['rosterGroupsFetched'], {},
  82. function (done, _converse) {
  83. var $filter = $(_converse.rosterview.el.querySelector('.roster-filter'));
  84. var names = mock.cur_names;
  85. test_utils.openControlBox();
  86. _converse.rosterview.update(); // XXX: Will normally called as event handler
  87. expect($filter.length).toBe(1);
  88. test_utils.waitUntil(function () {
  89. return !$filter.is(':visible');
  90. }).then(function () {
  91. for (var i=0; i<names.length; i++) {
  92. _converse.roster.create({
  93. ask: null,
  94. fullname: names[i],
  95. jid: names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  96. requesting: 'false',
  97. subscription: 'both'
  98. });
  99. _converse.rosterview.update(); // XXX: Will normally called as event handler
  100. }
  101. $.fn.hasScrollBar = function() {
  102. if (!$.contains(document, this.get(0))) {
  103. return false;
  104. }
  105. if(this.parent().height() < this.get(0).scrollHeight) {
  106. return true;
  107. }
  108. return false;
  109. };
  110. return test_utils.waitUntil(function () {
  111. if ($(_converse.rosterview.roster_el).hasScrollBar()) {
  112. return $filter.is(':visible');
  113. } else {
  114. return !$filter.is(':visible');
  115. }
  116. }).then(function () {
  117. done();
  118. });
  119. });
  120. }));
  121. it("can be used to filter the contacts shown",
  122. mock.initConverseWithPromises(
  123. null, ['rosterGroupsFetched'], {},
  124. function (done, _converse) {
  125. _converse.roster_groups = true;
  126. test_utils.openControlBox();
  127. test_utils.createGroupedContacts(_converse);
  128. var $filter = $(_converse.rosterview.el).find('.roster-filter');
  129. var $roster = $(_converse.rosterview.roster_el);
  130. _converse.rosterview.filter_view.delegateEvents();
  131. var promise = test_utils.waitUntil(() => $roster.find('li:visible').length === 15, 600)
  132. .then(function (contacts) {
  133. expect($roster.find('ul.roster-group-contacts:visible').length).toBe(5);
  134. $filter[0].value = "candice";
  135. u.triggerEvent($filter[0], "keydown", "KeyboardEvent");
  136. return test_utils.waitUntil(() => $roster.find('li:visible').length === 1, 600);
  137. }).then(function (contacts) {
  138. // Only one roster contact is now visible
  139. expect($roster.find('li:visible').length).toBe(1);
  140. expect($roster.find('li:visible').eq(0).text().trim()).toBe('Candice van der Knijff');
  141. // Only one foster group is still visible
  142. expect($roster.find('.roster-group:visible').length).toBe(1);
  143. expect(_.trim($roster.find('.roster-group:visible a.group-toggle').eq(0).text())).toBe('colleagues');
  144. $filter = $(_converse.rosterview.el).find('.roster-filter');
  145. $filter.val("an");
  146. u.triggerEvent($filter[0], "keydown", "KeyboardEvent");
  147. return test_utils.waitUntil(function () {
  148. return $roster.find('li:visible').length === 5;
  149. }, 600)
  150. }).then(function (contacts) {
  151. // Five roster contact is now visible
  152. expect($roster.find('li:visible').length).toBe(5);
  153. // Four groups are still visible
  154. var $groups = $roster.find('.roster-group:visible a.group-toggle');
  155. expect($groups.length).toBe(4);
  156. expect(_.trim($groups.eq(0).text())).toBe('colleagues');
  157. expect(_.trim($groups.eq(1).text())).toBe('Family');
  158. expect(_.trim($groups.eq(2).text())).toBe('friends & acquaintences');
  159. expect(_.trim($groups.eq(3).text())).toBe('ænemies');
  160. $filter = $(_converse.rosterview.el).find('.roster-filter');
  161. $filter.val("xxx");
  162. u.triggerEvent($filter[0], "keydown", "KeyboardEvent");
  163. return test_utils.waitUntil(function () {
  164. return $roster.find('li:visible').length === 0;
  165. }, 600)
  166. }).then(function () {
  167. expect($roster.find('ul.roster-group-contacts:visible a.group-toggle').length).toBe(0);
  168. $filter = $(_converse.rosterview.el).find('.roster-filter');
  169. $filter.val(""); // Check that contacts are shown again, when the filter string is cleared.
  170. u.triggerEvent($filter[0], "keydown", "KeyboardEvent");
  171. return test_utils.waitUntil(function () {
  172. return $roster.find('li:visible').length === 15;
  173. }, 600)
  174. }).then(function () {
  175. expect($roster.find('ul.roster-group-contacts:visible').length).toBe(5);
  176. _converse.roster_groups = false;
  177. done();
  178. }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
  179. }));
  180. it("will also filter out contacts added afterwards",
  181. mock.initConverseWithPromises(
  182. null, ['rosterGroupsFetched'], {},
  183. function (done, _converse) {
  184. test_utils.openControlBox();
  185. test_utils.createGroupedContacts(_converse);
  186. var $filter = $(_converse.rosterview.el).find('.roster-filter');
  187. var $roster = $(_converse.rosterview.roster_el);
  188. _converse.rosterview.filter_view.delegateEvents();
  189. test_utils.waitUntil(function () {
  190. return $roster.find('li:visible').length === 15;
  191. }, 300).then(function (contacts) {
  192. $filter.val("an");
  193. u.triggerEvent($filter[0], "keydown", "KeyboardEvent");
  194. return test_utils.waitUntil(function () {
  195. return $roster.find('li:visible').length === 5;
  196. }, 500)
  197. }).then(function (contacts) {
  198. // Five roster contact is now visible
  199. expect($roster.find('li:visible').length).toBe(5);
  200. // Four groups are still visible
  201. var $groups = $roster.find('.roster-group:visible a.group-toggle');
  202. expect($groups.length).toBe(4);
  203. expect(_.trim($groups.eq(0).text())).toBe('colleagues');
  204. expect(_.trim($groups.eq(1).text())).toBe('Family');
  205. expect(_.trim($groups.eq(2).text())).toBe('friends & acquaintences');
  206. expect(_.trim($groups.eq(3).text())).toBe('ænemies');
  207. _converse.roster.create({
  208. jid: 'latecomer@localhost',
  209. subscription: 'both',
  210. ask: null,
  211. groups: ['newgroup'],
  212. fullname: 'Marty McLatecomer'
  213. });
  214. return test_utils.waitUntil(function () {
  215. return $roster.find('.roster-group[data-group="newgroup"] li').length;
  216. }, 300);
  217. }).then(function (contacts) {
  218. // The "newgroup" group doesn't appear
  219. expect($roster.find('.roster-group:visible').length).toBe(4);
  220. expect($roster.find('.roster-group').length).toBe(6);
  221. done();
  222. });
  223. }));
  224. it("can be used to filter the groups shown",
  225. mock.initConverseWithPromises(
  226. null, ['rosterGroupsFetched'], {},
  227. function (done, _converse) {
  228. _converse.roster_groups = true;
  229. test_utils.openControlBox();
  230. test_utils.createGroupedContacts(_converse);
  231. _converse.rosterview.filter_view.delegateEvents();
  232. var $roster = $(_converse.rosterview.roster_el);
  233. var button = _converse.rosterview.el.querySelector('span[data-type="groups"]');
  234. button.click();
  235. test_utils.waitUntil(function () {
  236. return $roster.find('li:visible').length === 15;
  237. }, 600).then(function () {
  238. expect($roster.find('div.roster-group:visible a.group-toggle').length).toBe(5);
  239. var filter = _converse.rosterview.el.querySelector('.roster-filter');
  240. filter.value = "colleagues";
  241. u.triggerEvent(filter, "keydown", "KeyboardEvent");
  242. return test_utils.waitUntil(function () {
  243. return $roster.find('div.roster-group:not(.collapsed) a.group-toggle').length === 1;
  244. }, 600);
  245. }).then(function () {
  246. expect(_.trim($roster.find('div.roster-group:not(.collapsed) a').eq(0).text())).toBe('colleagues');
  247. expect($roster.find('div.roster-group:not(.collapsed) li:visible').length).toBe(3);
  248. // Check that all contacts under the group are shown
  249. expect($roster.find('div.roster-group:not(.collapsed) li:hidden').length).toBe(0);
  250. var filter = _converse.rosterview.el.querySelector('.roster-filter');
  251. filter.value = "xxx";
  252. u.triggerEvent(filter, "keydown", "KeyboardEvent");
  253. return test_utils.waitUntil(function () {
  254. return $roster.find('div.roster-group.collapsed a.group-toggle').length === 5;
  255. }, 700);
  256. }).then(function () {
  257. expect($roster.find('div.roster-group:not(.collapsed) a').length).toBe(0);
  258. var filter = _converse.rosterview.el.querySelector('.roster-filter');
  259. filter.value = ""; // Check that groups are shown again, when the filter string is cleared.
  260. u.triggerEvent(filter, "keydown", "KeyboardEvent");
  261. return test_utils.waitUntil(function () {
  262. return $roster.find('div.roster-group.collapsed a.group-toggle').length === 0;
  263. }, 600);
  264. }).then(function () {
  265. expect($roster.find('div.roster-group:not(collapsed)').length).toBe(5);
  266. expect($roster.find('div.roster-group:not(collapsed) li').length).toBe(15);
  267. done();
  268. });
  269. }));
  270. it("has a button with which its contents can be cleared",
  271. mock.initConverseWithPromises(
  272. null, ['rosterGroupsFetched'], {},
  273. function (done, _converse) {
  274. _converse.roster_groups = true;
  275. test_utils.openControlBox();
  276. test_utils.createGroupedContacts(_converse);
  277. var filter = _converse.rosterview.el.querySelector('.roster-filter');
  278. filter.value = "xxx";
  279. u.triggerEvent(filter, "keydown", "KeyboardEvent");
  280. expect(_.includes(filter.classList, "x")).toBeFalsy();
  281. expect(u.hasClass('hidden', _converse.rosterview.el.querySelector('.roster-filter-form .clear-input'))).toBeTruthy();
  282. test_utils.waitUntil(function () {
  283. return !u.hasClass('hidden', _converse.rosterview.el.querySelector('.roster-filter-form .clear-input'));
  284. }, 900).then(function () {
  285. var filter = _converse.rosterview.el.querySelector('.roster-filter');
  286. _converse.rosterview.el.querySelector('.clear-input').click();
  287. expect(document.querySelector('.roster-filter').value).toBe("");
  288. done();
  289. });
  290. }));
  291. it("can be used to filter contacts by their chat state",
  292. mock.initConverseWithPromises(
  293. null, ['rosterGroupsFetched'], {},
  294. function (done, _converse) {
  295. test_utils.createGroupedContacts(_converse);
  296. var jid = mock.cur_names[3].replace(/ /g,'.').toLowerCase() + '@localhost';
  297. _converse.roster.get(jid).presence.set('show', 'online');
  298. jid = mock.cur_names[4].replace(/ /g,'.').toLowerCase() + '@localhost';
  299. _converse.roster.get(jid).presence.set('show', 'dnd');
  300. test_utils.openControlBox();
  301. var button = _converse.rosterview.el.querySelector('span[data-type="state"]');
  302. button.click();
  303. var $roster = $(_converse.rosterview.roster_el);
  304. test_utils.waitUntil(() => $roster.find('li:visible').length === 15, 500).then(function () {
  305. var filter = _converse.rosterview.el.querySelector('.state-type');
  306. expect($roster.find('ul.roster-group-contacts:visible').length).toBe(5);
  307. filter.value = "online";
  308. u.triggerEvent(filter, 'change');
  309. return test_utils.waitUntil(() => $roster.find('li:visible').length === 1, 500);
  310. }).then(function () {
  311. expect($roster.find('li:visible').eq(0).text().trim()).toBe('Rinse Sommer');
  312. expect($roster.find('ul.roster-group-contacts:visible').length).toBe(1);
  313. var filter = _converse.rosterview.el.querySelector('.state-type');
  314. filter.value = "dnd";
  315. u.triggerEvent(filter, 'change');
  316. return test_utils.waitUntil(function () {
  317. return $roster.find('li:visible').eq(0).text().trim() === 'Annegreet Gomez';
  318. }, 900)
  319. }).then(function () {
  320. expect($roster.find('ul.roster-group-contacts:visible').length).toBe(1);
  321. done();
  322. }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
  323. }));
  324. });
  325. describe("A Roster Group", function () {
  326. it("can be used to organize existing contacts",
  327. mock.initConverseWithPromises(
  328. null, ['rosterGroupsFetched'], {},
  329. async function (done, _converse) {
  330. _converse.roster_groups = true;
  331. spyOn(_converse.rosterview, 'update').and.callThrough();
  332. _converse.rosterview.render();
  333. test_utils.openControlBox();
  334. test_utils.createContacts(_converse, 'pending');
  335. test_utils.createContacts(_converse, 'requesting');
  336. test_utils.createGroupedContacts(_converse);
  337. // Check that the groups appear alphabetically and that
  338. // requesting and pending contacts are last.
  339. await test_utils.waitUntil(() => sizzle('.roster-group a.group-toggle', _converse.rosterview.el).length);
  340. const group_titles = _.map(
  341. sizzle('.roster-group a.group-toggle', _converse.rosterview.el),
  342. o => o.textContent.trim()
  343. );
  344. expect(group_titles).toEqual([
  345. "Contact requests",
  346. "colleagues",
  347. "Family",
  348. "friends & acquaintences",
  349. "ænemies",
  350. "Ungrouped",
  351. "Pending contacts"
  352. ]);
  353. // Check that usernames appear alphabetically per group
  354. let names;
  355. _.each(_.keys(mock.groups), function (name) {
  356. const contacts = sizzle('.roster-group[data-group="'+name+'"] ul', _converse.rosterview.el);
  357. const names = _.map(contacts, o => o.textContent.trim());
  358. expect(names).toEqual(_.clone(names).sort());
  359. });
  360. done();
  361. }));
  362. it("gets created when a contact's \"groups\" attribute changes",
  363. mock.initConverseWithPromises(
  364. null, ['rosterGroupsFetched'], {},
  365. async function (done, _converse) {
  366. _converse.roster_groups = true;
  367. spyOn(_converse.rosterview, 'update').and.callThrough();
  368. _converse.rosterview.render();
  369. test_utils.openControlBox();
  370. _converse.roster.create({
  371. jid: 'groupchanger@localhost',
  372. subscription: 'both',
  373. ask: null,
  374. groups: ['firstgroup'],
  375. fullname: 'George Groupchanger'
  376. });
  377. // Check that the groups appear alphabetically and that
  378. // requesting and pending contacts are last.
  379. let group_titles = await test_utils.waitUntil(() => {
  380. const toggles = sizzle('.roster-group a.group-toggle', _converse.rosterview.el);
  381. if (_.reduce(toggles, (result, t) => result && u.isVisible(t), true)) {
  382. return _.map(toggles, o => o.textContent.trim());
  383. } else {
  384. return false;
  385. }
  386. }, 1000);
  387. expect(group_titles).toEqual(['firstgroup']);
  388. const contact = _converse.roster.get('groupchanger@localhost');
  389. contact.set({'groups': ['secondgroup']});
  390. group_titles = await test_utils.waitUntil(() => {
  391. const toggles = sizzle('.roster-group[data-group="secondgroup"] a.group-toggle', _converse.rosterview.el);
  392. if (_.reduce(toggles, (result, t) => result && u.isVisible(t), true)) {
  393. return _.map(toggles, o => o.textContent.trim());
  394. } else {
  395. return false;
  396. }
  397. }, 1000);
  398. expect(group_titles).toEqual(['secondgroup']);
  399. done();
  400. }));
  401. it("can share contacts with other roster groups",
  402. mock.initConverseWithPromises(
  403. null, ['rosterGroupsFetched'], {},
  404. function (done, _converse) {
  405. _converse.roster_groups = true;
  406. var groups = ['colleagues', 'friends'];
  407. spyOn(_converse.rosterview, 'update').and.callThrough();
  408. test_utils.openControlBox();
  409. _converse.rosterview.render();
  410. for (var i=0; i<mock.cur_names.length; i++) {
  411. _converse.roster.create({
  412. jid: mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  413. subscription: 'both',
  414. ask: null,
  415. groups: groups,
  416. fullname: mock.cur_names[i]
  417. });
  418. }
  419. test_utils.waitUntil(function () {
  420. return $(_converse.rosterview.el).find('li:visible').length === 30;
  421. }, 600).then(function () {
  422. // Check that usernames appear alphabetically per group
  423. _.each(groups, function (name) {
  424. var $contacts = $(_converse.rosterview.el).find('.roster-group[data-group="'+name+'"] ul li');
  425. var names = $.map($contacts, function (o) { return $(o).text().trim(); });
  426. expect(names).toEqual(_.clone(names).sort());
  427. expect(names.length).toEqual(mock.cur_names.length);
  428. });
  429. done();
  430. });
  431. }));
  432. it("remembers whether it is closed or opened",
  433. mock.initConverseWithPromises(
  434. null, ['rosterGroupsFetched'], {},
  435. async function (done, _converse) {
  436. _converse.roster_groups = true;
  437. test_utils.openControlBox();
  438. var i=0, j=0;
  439. var groups = {
  440. 'colleagues': 3,
  441. 'friends & acquaintences': 3,
  442. 'Ungrouped': 2
  443. };
  444. _.each(_.keys(groups), function (name) {
  445. j = i;
  446. for (i=j; i<j+groups[name]; i++) {
  447. _converse.roster.create({
  448. jid: mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  449. subscription: 'both',
  450. ask: null,
  451. groups: name === 'ungrouped'? [] : [name],
  452. fullname: mock.cur_names[i]
  453. });
  454. }
  455. });
  456. const view = _converse.rosterview.get('colleagues');
  457. const toggle = view.el.querySelector('a.group-toggle');
  458. expect(view.model.get('state')).toBe('opened');
  459. toggle.click();
  460. await test_utils.waitUntil(() => view.model.get('state') === 'closed');
  461. toggle.click();
  462. await test_utils.waitUntil(() => view.model.get('state') === 'opened');
  463. done();
  464. }));
  465. });
  466. describe("Pending Contacts", function () {
  467. function _addContacts (_converse) {
  468. // Must be initialized, so that render is called and documentFragment set up.
  469. test_utils.createContacts(_converse, 'pending');
  470. test_utils.openControlBox();
  471. }
  472. it("can be collapsed under their own header",
  473. mock.initConverseWithPromises(
  474. null, ['rosterGroupsFetched'], {},
  475. async function (done, _converse) {
  476. _addContacts(_converse);
  477. await test_utils.waitUntil(() => $(_converse.rosterview.el).find('.roster-group:visible li').length, 1000);
  478. await checkHeaderToggling.apply(
  479. _converse,
  480. [_converse.rosterview.get('Pending contacts').el]
  481. );
  482. done();
  483. }));
  484. it("can be added to the roster",
  485. mock.initConverseWithPromises(
  486. null, ['rosterGroupsFetched'], {},
  487. function (done, _converse) {
  488. spyOn(_converse.rosterview, 'update').and.callThrough();
  489. test_utils.openControlBox();
  490. _converse.roster.create({
  491. jid: mock.pend_names[0].replace(/ /g,'.').toLowerCase() + '@localhost',
  492. subscription: 'none',
  493. ask: 'subscribe',
  494. fullname: mock.pend_names[0]
  495. });
  496. expect(_converse.rosterview.update).toHaveBeenCalled();
  497. done();
  498. }));
  499. it("are shown in the roster when show_only_online_users",
  500. mock.initConverseWithPromises(
  501. null, ['rosterGroupsFetched'], {},
  502. async function (done, _converse) {
  503. _converse.show_only_online_users = true;
  504. test_utils.openControlBox();
  505. spyOn(_converse.rosterview, 'update').and.callThrough();
  506. _addContacts(_converse);
  507. await test_utils.waitUntil(() => _.reduce(_converse.rosterview.el.querySelectorAll('li'), (result, el) => result && u.isVisible(el), true), 500);
  508. expect(u.isVisible(_converse.rosterview.el)).toEqual(true);
  509. expect(_converse.rosterview.update).toHaveBeenCalled();
  510. expect(_converse.rosterview.el.querySelectorAll('li').length).toBe(3);
  511. expect(_.filter(_converse.rosterview.el.querySelectorAll('ul.roster-group-contacts'), u.isVisible).length).toBe(1);
  512. done();
  513. }));
  514. it("are shown in the roster when hide_offline_users",
  515. mock.initConverseWithPromises(
  516. null, ['rosterGroupsFetched'], {},
  517. function (done, _converse) {
  518. _converse.hide_offline_users = true;
  519. spyOn(_converse.rosterview, 'update').and.callThrough();
  520. _addContacts(_converse);
  521. test_utils.waitUntil(function () {
  522. return $(_converse.rosterview.el).find('li:visible').length;
  523. }, 500)
  524. .then(function () {
  525. expect(_converse.rosterview.update).toHaveBeenCalled();
  526. expect($(_converse.rosterview.el).is(':visible')).toBe(true);
  527. expect($(_converse.rosterview.el).find('li:visible').length).toBe(3);
  528. expect($(_converse.rosterview.el).find('ul.roster-group-contacts:visible').length).toBe(1);
  529. done();
  530. });
  531. }));
  532. it("can be removed by the user",
  533. mock.initConverseWithPromises(
  534. null, ['rosterGroupsFetched'], {},
  535. function (done, _converse) {
  536. _addContacts(_converse);
  537. var name = mock.pend_names[0];
  538. var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  539. var contact = _converse.roster.get(jid);
  540. var sent_IQ;
  541. spyOn(window, 'confirm').and.returnValue(true);
  542. spyOn(contact, 'unauthorize').and.callFake(function () { return contact; });
  543. spyOn(contact, 'removeFromRoster').and.callThrough();
  544. test_utils.waitUntil(function () {
  545. return $(_converse.rosterview.el).find(".pending-contact-name:contains('"+name+"')").length;
  546. }, 700).then(function () {
  547. var sendIQ = _converse.connection.sendIQ;
  548. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  549. sent_IQ = iq;
  550. callback();
  551. });
  552. $(_converse.rosterview.el).find(".pending-contact-name:contains('"+name+"')")
  553. .parent().siblings('.remove-xmpp-contact')[0].click();
  554. return test_utils.waitUntil(function () {
  555. return $(_converse.rosterview.el).find(".pending-contact-name:contains('"+name+"')").length === 0
  556. }, 1000)
  557. }).then(function () {
  558. expect(window.confirm).toHaveBeenCalled();
  559. expect(contact.removeFromRoster).toHaveBeenCalled();
  560. expect(sent_IQ.toLocaleString()).toBe(
  561. `<iq type="set" xmlns="jabber:client">`+
  562. `<query xmlns="jabber:iq:roster">`+
  563. `<item jid="suleyman.van.beusichem@localhost" subscription="remove"/>`+
  564. `</query>`+
  565. `</iq>`);
  566. done();
  567. });
  568. }));
  569. it("do not have a header if there aren't any",
  570. mock.initConverseWithPromises(
  571. null, ['rosterGroupsFetched'], {},
  572. async function (done, _converse) {
  573. test_utils.openControlBox();
  574. const name = mock.pend_names[0];
  575. _converse.roster.create({
  576. jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
  577. subscription: 'none',
  578. ask: 'subscribe',
  579. fullname: name
  580. });
  581. spyOn(window, 'confirm').and.returnValue(true);
  582. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback) {
  583. if (typeof callback === "function") { return callback(); }
  584. });
  585. await test_utils.waitUntil(function () {
  586. var $pending_contacts = $(_converse.rosterview.get('Pending contacts').el);
  587. return $pending_contacts.is(':visible') && $pending_contacts.find('li:visible').length;
  588. }, 700)
  589. $(_converse.rosterview.el).find(".pending-contact-name:contains('"+name+"')")
  590. .parent().siblings('.remove-xmpp-contact')[0].click();
  591. expect(window.confirm).toHaveBeenCalled();
  592. expect(_converse.connection.sendIQ).toHaveBeenCalled();
  593. await test_utils.waitUntil(() => !u.isVisible(_converse.rosterview.get('Pending contacts').el));
  594. done();
  595. }));
  596. it("is shown when a new private message is received",
  597. mock.initConverseWithPromises(
  598. null, ['rosterGroupsFetched'], {},
  599. function (done, _converse) {
  600. _addContacts(_converse);
  601. return test_utils.waitUntil(() => _converse.roster.at(0).vcard.get('fullname'))
  602. .then(function () {
  603. var name;
  604. spyOn(window, 'confirm').and.returnValue(true);
  605. for (var i=0; i<mock.pend_names.length; i++) {
  606. name = mock.pend_names[i];
  607. $(_converse.rosterview.el).find(".pending-contact-name:contains('"+name+"')")
  608. .parent().siblings('.remove-xmpp-contact')[0].click();
  609. }
  610. expect($(_converse.rosterview.el).find('#pending-xmpp-contacts').is(':visible')).toBeFalsy();
  611. done();
  612. });
  613. }));
  614. it("can be added to the roster and they will be sorted alphabetically",
  615. mock.initConverseWithPromises(
  616. null, ['rosterGroupsFetched'], {},
  617. function (done, _converse) {
  618. var i, t;
  619. test_utils.openControlBox();
  620. spyOn(_converse.rosterview, 'update').and.callThrough();
  621. for (i=0; i<mock.pend_names.length; i++) {
  622. _converse.roster.create({
  623. jid: mock.pend_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  624. subscription: 'none',
  625. ask: 'subscribe',
  626. fullname: mock.pend_names[i]
  627. });
  628. expect(_converse.rosterview.update).toHaveBeenCalled();
  629. }
  630. return test_utils.waitUntil(function () {
  631. return $(_converse.rosterview.get('Pending contacts').el).find('li:visible').length;
  632. }, 700).then(function () {
  633. // Check that they are sorted alphabetically
  634. t = _.reduce(_converse.rosterview.get('Pending contacts').el.querySelectorAll('.pending-xmpp-contact span'),
  635. function (result, value) {
  636. return result + _.trim(value.textContent);
  637. }, '');
  638. expect(t).toEqual(mock.pend_names.slice(0,i+1).sort().join(''));
  639. done();
  640. });
  641. }));
  642. });
  643. describe("Existing Contacts", function () {
  644. var _addContacts = function (_converse) {
  645. test_utils.createContacts(_converse, 'current')
  646. .openControlBox()
  647. };
  648. it("can be collapsed under their own header",
  649. mock.initConverseWithPromises(
  650. null, ['rosterGroupsFetched'], {},
  651. function (done, _converse) {
  652. _addContacts(_converse);
  653. test_utils.waitUntil(function () {
  654. return $(_converse.rosterview.el).find('li:visible').length;
  655. }, 500).then(function () {
  656. checkHeaderToggling.apply(
  657. _converse,
  658. [_converse.rosterview.el.querySelector('.roster-group')]
  659. ).then(done);
  660. });
  661. }));
  662. it("will be hidden when appearing under a collapsed group",
  663. mock.initConverseWithPromises(
  664. null, ['rosterGroupsFetched'], {},
  665. function (done, _converse) {
  666. _converse.roster_groups = false;
  667. _addContacts(_converse);
  668. test_utils.waitUntil(function () {
  669. return $(_converse.rosterview.el).find('li:visible').length;
  670. }, 500)
  671. .then(function () {
  672. _converse.rosterview.el.querySelector('.roster-group a.group-toggle').click();
  673. var name = "Max Mustermann";
  674. var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  675. _converse.roster.create({
  676. ask: null,
  677. fullname: name,
  678. jid: jid,
  679. requesting: false,
  680. subscription: 'both'
  681. });
  682. var view = _converse.rosterview.get('My contacts').get(jid);
  683. expect($(view.el).is(':visible')).toBe(false);
  684. done();
  685. });
  686. }));
  687. it("can be added to the roster and they will be sorted alphabetically",
  688. mock.initConverseWithPromises(
  689. null, ['rosterGroupsFetched'], {},
  690. function (done, _converse) {
  691. test_utils.openControlBox();
  692. spyOn(_converse.rosterview, 'update').and.callThrough();
  693. for (var i=0; i<mock.cur_names.length; i++) {
  694. _converse.roster.create({
  695. jid: mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  696. subscription: 'both',
  697. ask: null,
  698. fullname: mock.cur_names[i]
  699. });
  700. expect(_converse.rosterview.update).toHaveBeenCalled();
  701. }
  702. test_utils.waitUntil(function () {
  703. return $(_converse.rosterview.el).find('li').length;
  704. }, 600).then(function () {
  705. // Check that they are sorted alphabetically
  706. var t = _.reduce($(_converse.rosterview.el.querySelector('.roster-group'))
  707. .find('.current-xmpp-contact.offline a.open-chat'),
  708. function (result, value) {
  709. return result + _.trim(value.textContent);
  710. }, '');
  711. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  712. done();
  713. });
  714. }));
  715. it("can be removed by the user",
  716. mock.initConverseWithPromises(
  717. null, ['rosterGroupsFetched'], {},
  718. async function (done, _converse) {
  719. _addContacts(_converse);
  720. await test_utils.waitUntil(() => _converse.rosterview.el.querySelectorAll('li').length);
  721. const name = mock.cur_names[0];
  722. const jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  723. const contact = _converse.roster.get(jid);
  724. spyOn(window, 'confirm').and.returnValue(true);
  725. spyOn(contact, 'removeFromRoster').and.callThrough();
  726. const sendIQ = _converse.connection.sendIQ;
  727. let sent_IQ;
  728. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
  729. sent_IQ = iq;
  730. callback();
  731. });
  732. $(_converse.rosterview.el).find(".open-chat:contains('"+name+"')")
  733. .parent().find('.remove-xmpp-contact')[0].click();
  734. expect(window.confirm).toHaveBeenCalled();
  735. expect(sent_IQ.toLocaleString()).toBe(
  736. `<iq type="set" xmlns="jabber:client">`+
  737. `<query xmlns="jabber:iq:roster"><item jid="max.frankfurter@localhost" subscription="remove"/></query>`+
  738. `</iq>`);
  739. expect(contact.removeFromRoster).toHaveBeenCalled();
  740. await test_utils.waitUntil(() => $(_converse.rosterview.el).find(".open-chat:contains('"+name+"')").length === 0);
  741. done();
  742. }));
  743. it("do not have a header if there aren't any",
  744. mock.initConverseWithPromises(
  745. null, ['rosterGroupsFetched'], {},
  746. async function (done, _converse) {
  747. test_utils.openControlBox();
  748. var name = mock.cur_names[0];
  749. var contact;
  750. contact = _converse.roster.create({
  751. jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
  752. subscription: 'both',
  753. ask: null,
  754. fullname: name
  755. });
  756. await test_utils.waitUntil(() => $(_converse.rosterview.el).find('.roster-group:visible li').length, 1000);
  757. spyOn(window, 'confirm').and.returnValue(true);
  758. spyOn(contact, 'removeFromRoster').and.callThrough();
  759. spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback) {
  760. if (typeof callback === "function") { return callback(); }
  761. });
  762. expect($(_converse.rosterview.el).find('.roster-group').css('display')).toEqual('block');
  763. $(_converse.rosterview.el).find(".open-chat:contains('"+name+"')")
  764. .parent().find('.remove-xmpp-contact')[0].click();
  765. expect(window.confirm).toHaveBeenCalled();
  766. expect(_converse.connection.sendIQ).toHaveBeenCalled();
  767. expect(contact.removeFromRoster).toHaveBeenCalled();
  768. await test_utils.waitUntil(() => _converse.rosterview.el.querySelectorAll('.roster-group').length === 0);
  769. done();
  770. }));
  771. it("can change their status to online and be sorted alphabetically",
  772. mock.initConverseWithPromises(
  773. null, ['rosterGroupsFetched'], {},
  774. function (done, _converse) {
  775. _addContacts(_converse);
  776. test_utils.waitUntil(() => $(_converse.rosterview.el).find('.roster-group li').length, 700)
  777. .then(function () {
  778. var jid, t;
  779. spyOn(_converse.rosterview, 'update').and.callThrough();
  780. var $roster = $(_converse.rosterview.el);
  781. for (var i=0; i<mock.cur_names.length; i++) {
  782. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  783. _converse.roster.get(jid).presence.set('show', 'online');
  784. expect(_converse.rosterview.update).toHaveBeenCalled();
  785. // Check that they are sorted alphabetically
  786. t = _.reduce($roster.find('.roster-group').find('.current-xmpp-contact.online a.open-chat'), function (result, value) {
  787. return result + _.trim(value.textContent);
  788. }, '');
  789. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  790. }
  791. done();
  792. });
  793. }));
  794. it("can change their status to busy and be sorted alphabetically",
  795. mock.initConverseWithPromises(
  796. null, ['rosterGroupsFetched'], {},
  797. function (done, _converse) {
  798. _addContacts(_converse);
  799. test_utils.waitUntil(function () {
  800. return $(_converse.rosterview.el).find('.roster-group li').length;
  801. }, 700).then(function () {
  802. var jid, t;
  803. spyOn(_converse.rosterview, 'update').and.callThrough();
  804. var $roster = $(_converse.rosterview.el);
  805. for (var i=0; i<mock.cur_names.length; i++) {
  806. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  807. _converse.roster.get(jid).presence.set('show', 'dnd');
  808. expect(_converse.rosterview.update).toHaveBeenCalled();
  809. // Check that they are sorted alphabetically
  810. t = _.reduce($roster.find('.roster-group .current-xmpp-contact.dnd a.open-chat'),
  811. function (result, value) {
  812. return result + _.trim(value.textContent);
  813. }, '');
  814. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  815. }
  816. done();
  817. });
  818. }));
  819. it("can change their status to away and be sorted alphabetically",
  820. mock.initConverseWithPromises(
  821. null, ['rosterGroupsFetched'], {},
  822. function (done, _converse) {
  823. _addContacts(_converse);
  824. test_utils.waitUntil(function () {
  825. return $(_converse.rosterview.el).find('.roster-group li').length;
  826. }, 700).then(function () {
  827. var jid, t;
  828. spyOn(_converse.rosterview, 'update').and.callThrough();
  829. var $roster = $(_converse.rosterview.el);
  830. for (var i=0; i<mock.cur_names.length; i++) {
  831. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  832. _converse.roster.get(jid).presence.set('show', 'away');
  833. expect(_converse.rosterview.update).toHaveBeenCalled();
  834. // Check that they are sorted alphabetically
  835. t = _.reduce($roster.find('.roster-group .current-xmpp-contact.away a.open-chat'),
  836. function (result, value) {
  837. return result + _.trim(value.textContent);
  838. }, '');
  839. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  840. }
  841. done();
  842. });
  843. }));
  844. it("can change their status to xa and be sorted alphabetically",
  845. mock.initConverseWithPromises(
  846. null, ['rosterGroupsFetched'], {},
  847. function (done, _converse) {
  848. _addContacts(_converse);
  849. test_utils.waitUntil(function () {
  850. return $(_converse.rosterview.el).find('.roster-group li').length;
  851. }, 700).then(function () {
  852. var jid, t;
  853. spyOn(_converse.rosterview, 'update').and.callThrough();
  854. var $roster = $(_converse.rosterview.el);
  855. for (var i=0; i<mock.cur_names.length; i++) {
  856. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  857. _converse.roster.get(jid).presence.set('show', 'xa');
  858. expect(_converse.rosterview.update).toHaveBeenCalled();
  859. // Check that they are sorted alphabetically
  860. t = _.reduce($roster.find('.roster-group .current-xmpp-contact.xa a.open-chat'),
  861. function (result, value) {
  862. return result + _.trim(value.textContent);
  863. }, '');
  864. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  865. }
  866. done();
  867. });
  868. }));
  869. it("can change their status to unavailable and be sorted alphabetically",
  870. mock.initConverseWithPromises(
  871. null, ['rosterGroupsFetched'], {},
  872. function (done, _converse) {
  873. _addContacts(_converse);
  874. test_utils.waitUntil(function () {
  875. return $(_converse.rosterview.el).find('.roster-group li').length;
  876. }, 500)
  877. .then(function () {
  878. var jid, t;
  879. spyOn(_converse.rosterview, 'update').and.callThrough();
  880. var $roster = $(_converse.rosterview.el);
  881. for (var i=0; i<mock.cur_names.length; i++) {
  882. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  883. _converse.roster.get(jid).presence.set('show', 'unavailable');
  884. expect(_converse.rosterview.update).toHaveBeenCalled();
  885. // Check that they are sorted alphabetically
  886. t = _.reduce($roster.find('.roster-group .current-xmpp-contact.unavailable a.open-chat'),
  887. function (result, value) {
  888. return result + _.trim(value.textContent);
  889. }, '');
  890. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  891. }
  892. done();
  893. });
  894. }));
  895. it("are ordered according to status: online, busy, away, xa, unavailable, offline",
  896. mock.initConverseWithPromises(
  897. null, ['rosterGroupsFetched'], {},
  898. function (done, _converse) {
  899. _addContacts(_converse);
  900. test_utils.waitUntil(function () {
  901. return $(_converse.rosterview.el).find('.roster-group li').length;
  902. }, 700).then(function () {
  903. var i, jid;
  904. for (i=0; i<3; i++) {
  905. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  906. _converse.roster.get(jid).presence.set('show', 'online');
  907. }
  908. for (i=3; i<6; i++) {
  909. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  910. _converse.roster.get(jid).presence.set('show', 'dnd');
  911. }
  912. for (i=6; i<9; i++) {
  913. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  914. _converse.roster.get(jid).presence.set('show', 'away');
  915. }
  916. for (i=9; i<12; i++) {
  917. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  918. _converse.roster.get(jid).presence.set('show', 'xa');
  919. }
  920. for (i=12; i<15; i++) {
  921. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  922. _converse.roster.get(jid).presence.set('show', 'unavailable');
  923. }
  924. return test_utils.waitUntil(function () {
  925. return $(_converse.rosterview.el).find('li.online').length
  926. })
  927. }).then(function () {
  928. return test_utils.waitUntil(function () {
  929. return $(_converse.rosterview.el).find('li:first').text().trim() === 'Candice van der Knijff'
  930. }, 900);
  931. }).then(function () {
  932. var i;
  933. var contacts = $(_converse.rosterview.el).find('.current-xmpp-contact');
  934. for (i=0; i<3; i++) {
  935. expect($(contacts[i]).hasClass('online')).toBeTruthy();
  936. expect($(contacts[i]).hasClass('both')).toBeTruthy();
  937. expect($(contacts[i]).hasClass('dnd')).toBeFalsy();
  938. expect($(contacts[i]).hasClass('away')).toBeFalsy();
  939. expect($(contacts[i]).hasClass('xa')).toBeFalsy();
  940. expect($(contacts[i]).hasClass('unavailable')).toBeFalsy();
  941. expect($(contacts[i]).hasClass('offline')).toBeFalsy();
  942. }
  943. for (i=3; i<6; i++) {
  944. expect($(contacts[i]).hasClass('dnd')).toBeTruthy();
  945. expect($(contacts[i]).hasClass('both')).toBeTruthy();
  946. expect($(contacts[i]).hasClass('online')).toBeFalsy();
  947. expect($(contacts[i]).hasClass('away')).toBeFalsy();
  948. expect($(contacts[i]).hasClass('xa')).toBeFalsy();
  949. expect($(contacts[i]).hasClass('unavailable')).toBeFalsy();
  950. expect($(contacts[i]).hasClass('offline')).toBeFalsy();
  951. }
  952. for (i=6; i<9; i++) {
  953. expect($(contacts[i]).hasClass('away')).toBeTruthy();
  954. expect($(contacts[i]).hasClass('both')).toBeTruthy();
  955. expect($(contacts[i]).hasClass('online')).toBeFalsy();
  956. expect($(contacts[i]).hasClass('dnd')).toBeFalsy();
  957. expect($(contacts[i]).hasClass('xa')).toBeFalsy();
  958. expect($(contacts[i]).hasClass('unavailable')).toBeFalsy();
  959. expect($(contacts[i]).hasClass('offline')).toBeFalsy();
  960. }
  961. for (i=9; i<12; i++) {
  962. expect($(contacts[i]).hasClass('xa')).toBeTruthy();
  963. expect($(contacts[i]).hasClass('both')).toBeTruthy();
  964. expect($(contacts[i]).hasClass('online')).toBeFalsy();
  965. expect($(contacts[i]).hasClass('dnd')).toBeFalsy();
  966. expect($(contacts[i]).hasClass('away')).toBeFalsy();
  967. expect($(contacts[i]).hasClass('unavailable')).toBeFalsy();
  968. expect($(contacts[i]).hasClass('offline')).toBeFalsy();
  969. }
  970. for (i=12; i<15; i++) {
  971. expect($(contacts[i]).hasClass('unavailable')).toBeTruthy();
  972. expect($(contacts[i]).hasClass('both')).toBeTruthy();
  973. expect($(contacts[i]).hasClass('online')).toBeFalsy();
  974. expect($(contacts[i]).hasClass('dnd')).toBeFalsy();
  975. expect($(contacts[i]).hasClass('away')).toBeFalsy();
  976. expect($(contacts[i]).hasClass('xa')).toBeFalsy();
  977. expect($(contacts[i]).hasClass('offline')).toBeFalsy();
  978. }
  979. for (i=15; i<mock.cur_names.length; i++) {
  980. expect($(contacts[i]).hasClass('offline')).toBeTruthy();
  981. expect($(contacts[i]).hasClass('both')).toBeTruthy();
  982. expect($(contacts[i]).hasClass('online')).toBeFalsy();
  983. expect($(contacts[i]).hasClass('dnd')).toBeFalsy();
  984. expect($(contacts[i]).hasClass('away')).toBeFalsy();
  985. expect($(contacts[i]).hasClass('xa')).toBeFalsy();
  986. expect($(contacts[i]).hasClass('unavailable')).toBeFalsy();
  987. }
  988. done();
  989. });
  990. }));
  991. });
  992. describe("Requesting Contacts", function () {
  993. it("can be added to the roster and they will be sorted alphabetically",
  994. mock.initConverseWithPromises(
  995. null, ['rosterGroupsFetched'], {},
  996. function (done, _converse) {
  997. var i, children;
  998. var names = [];
  999. var addName = function (item) {
  1000. if (!$(item).hasClass('request-actions')) {
  1001. names.push($(item).text().replace(/^\s+|\s+$/g, ''));
  1002. }
  1003. };
  1004. spyOn(_converse.rosterview, 'update').and.callThrough();
  1005. spyOn(_converse.controlboxtoggle, 'showControlBox').and.callThrough();
  1006. for (i=0; i<mock.req_names.length; i++) {
  1007. _converse.roster.create({
  1008. jid: mock.req_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  1009. subscription: 'none',
  1010. ask: null,
  1011. requesting: true,
  1012. fullname: mock.req_names[i]
  1013. });
  1014. }
  1015. test_utils.waitUntil(function () {
  1016. return _converse.rosterview.get('Contact requests').el.querySelectorAll('li').length;
  1017. }, 700).then(function () {
  1018. expect(_converse.rosterview.update).toHaveBeenCalled();
  1019. // Check that they are sorted alphabetically
  1020. children = _converse.rosterview.get('Contact requests').el.querySelectorAll('.requesting-xmpp-contact span');
  1021. names = [];
  1022. _.each(children, addName);
  1023. expect(names.join('')).toEqual(mock.req_names.slice(0,mock.req_names.length+1).sort().join(''));
  1024. done();
  1025. });
  1026. }));
  1027. it("do not have a header if there aren't any",
  1028. mock.initConverseWithPromises(
  1029. null, ['rosterGroupsFetched'], {},
  1030. function (done, _converse) {
  1031. test_utils.openControlBox();
  1032. var name = mock.req_names[0];
  1033. spyOn(window, 'confirm').and.returnValue(true);
  1034. _converse.roster.create({
  1035. jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
  1036. subscription: 'none',
  1037. ask: null,
  1038. requesting: true,
  1039. fullname: name
  1040. });
  1041. test_utils.waitUntil(function () {
  1042. return $(_converse.rosterview.el).find('.roster-group:visible li').length;
  1043. }, 900).then(function () {
  1044. expect(u.isVisible(_converse.rosterview.get('Contact requests').el)).toEqual(true);
  1045. expect($(_converse.rosterview.el).find('.roster-group:visible li').length).toBe(1);
  1046. $(_converse.rosterview.el).find('.roster-group:visible li .decline-xmpp-request')[0].click();
  1047. expect(window.confirm).toHaveBeenCalled();
  1048. expect(u.isVisible(_converse.rosterview.get('Contact requests').el)).toEqual(false);
  1049. done();
  1050. });
  1051. }));
  1052. it("can be collapsed under their own header",
  1053. mock.initConverseWithPromises(
  1054. null, ['rosterGroupsFetched'], {},
  1055. function (done, _converse) {
  1056. test_utils.createContacts(_converse, 'requesting').openControlBox();
  1057. test_utils.waitUntil(function () {
  1058. return $(_converse.rosterview.el).find('.roster-group:visible li').length;
  1059. }, 700).then(function () {
  1060. checkHeaderToggling.apply(
  1061. _converse,
  1062. [_converse.rosterview.get('Contact requests').el]
  1063. ).then(done);
  1064. });
  1065. }));
  1066. it("can have their requests accepted by the user",
  1067. mock.initConverseWithPromises(
  1068. null, ['rosterGroupsFetched'], {},
  1069. async function (done, _converse) {
  1070. test_utils.openControlBox();
  1071. test_utils.createContacts(_converse, 'requesting').openControlBox();
  1072. await test_utils.waitUntil(() => _converse.rosterview.el.querySelectorAll('.roster-group li').length)
  1073. // TODO: Testing can be more thorough here, the user is
  1074. // actually not accepted/authorized because of
  1075. // mock_connection.
  1076. const jid = mock.req_names.sort()[0].replace(/ /g,'.').toLowerCase() + '@localhost';
  1077. const contact = _converse.roster.get(jid);
  1078. spyOn(contact, 'authorize').and.callFake(() => contact);
  1079. spyOn(_converse.roster, 'sendContactAddIQ').and.callFake(() => Promise.resolve());
  1080. $(_converse.rosterview.el).find(".req-contact-name:contains('"+name+"')")
  1081. .parent().parent().find('.accept-xmpp-request')[0].click();
  1082. expect(_converse.roster.sendContactAddIQ).toHaveBeenCalled();
  1083. await test_utils.waitUntil(() => contact.authorize.calls.count());
  1084. expect(contact.authorize).toHaveBeenCalled();
  1085. done();
  1086. }));
  1087. it("can have their requests denied by the user",
  1088. mock.initConverseWithPromises(
  1089. null, ['rosterGroupsFetched'], {},
  1090. function (done, _converse) {
  1091. test_utils.createContacts(_converse, 'requesting').openControlBox();
  1092. test_utils.waitUntil(function () {
  1093. return $(_converse.rosterview.el).find('.roster-group li').length;
  1094. }, 700).then(function () {
  1095. _converse.rosterview.update(); // XXX: Hack to make sure $roster element is attaced.
  1096. var name = mock.req_names.sort()[1];
  1097. var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  1098. var contact = _converse.roster.get(jid);
  1099. spyOn(window, 'confirm').and.returnValue(true);
  1100. spyOn(contact, 'unauthorize').and.callFake(function () { return contact; });
  1101. $(_converse.rosterview.el).find(".req-contact-name:contains('"+name+"')")
  1102. .parent().parent().find('.decline-xmpp-request')[0].click();
  1103. expect(window.confirm).toHaveBeenCalled();
  1104. expect(contact.unauthorize).toHaveBeenCalled();
  1105. // There should now be one less contact
  1106. expect(_converse.roster.length).toEqual(mock.req_names.length-1);
  1107. done();
  1108. });
  1109. }));
  1110. it("are persisted even if other contacts' change their presence ", mock.initConverseWithPromises(
  1111. null, ['rosterGroupsFetched'], {}, function (done, _converse) {
  1112. /* This is a regression test.
  1113. * https://github.com/jcbrand/_converse.js/issues/262
  1114. */
  1115. expect(_converse.roster.pluck('jid').length).toBe(0);
  1116. var stanza = $pres({from: 'data@enterprise/resource', type: 'subscribe'});
  1117. _converse.connection._dataRecv(test_utils.createRequest(stanza));
  1118. test_utils.waitUntil(function () {
  1119. return $('a:contains("Contact requests")').length;
  1120. }, 700).then(function () {
  1121. expect(_converse.roster.pluck('jid').length).toBe(1);
  1122. expect(_.includes(_converse.roster.pluck('jid'), 'data@enterprise')).toBeTruthy();
  1123. // Taken from the spec
  1124. // http://xmpp.org/rfcs/rfc3921.html#rfc.section.7.3
  1125. stanza = $iq({
  1126. to: _converse.connection.jid,
  1127. type: 'result',
  1128. id: 'roster_1'
  1129. }).c('query', {
  1130. xmlns: 'jabber:iq:roster',
  1131. }).c('item', {
  1132. jid: 'romeo@example.net',
  1133. name: 'Romeo',
  1134. subscription:'both'
  1135. }).c('group').t('Friends').up().up()
  1136. .c('item', {
  1137. jid: 'mercutio@example.org',
  1138. name: 'Mercutio',
  1139. subscription:'from'
  1140. }).c('group').t('Friends').up().up()
  1141. .c('item', {
  1142. jid: 'benvolio@example.org',
  1143. name: 'Benvolio',
  1144. subscription:'both'
  1145. }).c('group').t('Friends');
  1146. _converse.roster.onReceivedFromServer(stanza.tree());
  1147. expect(_.includes(_converse.roster.pluck('jid'), 'data@enterprise')).toBeTruthy();
  1148. done();
  1149. });
  1150. }));
  1151. });
  1152. describe("All Contacts", function () {
  1153. it("are saved to, and can be retrieved from browserStorage",
  1154. mock.initConverseWithPromises(
  1155. null, ['rosterGroupsFetched'], {},
  1156. function (done, _converse) {
  1157. test_utils.createContacts(_converse, 'all').openControlBox();
  1158. var new_attrs, old_attrs, attrs;
  1159. var num_contacts = _converse.roster.length;
  1160. var new_roster = new _converse.RosterContacts();
  1161. // Roster items are yet to be fetched from browserStorage
  1162. expect(new_roster.length).toEqual(0);
  1163. new_roster.browserStorage = _converse.roster.browserStorage;
  1164. new_roster.fetch();
  1165. expect(new_roster.length).toEqual(num_contacts);
  1166. // Check that the roster items retrieved from browserStorage
  1167. // have the same attributes values as the original ones.
  1168. attrs = ['jid', 'fullname', 'subscription', 'ask'];
  1169. for (var i=0; i<attrs.length; i++) {
  1170. new_attrs = _.map(_.map(new_roster.models, 'attributes'), attrs[i]);
  1171. old_attrs = _.map(_.map(_converse.roster.models, 'attributes'), attrs[i]);
  1172. // Roster items in storage are not necessarily sorted,
  1173. // so we have to sort them here to do a proper
  1174. // comparison
  1175. expect(_.isEqual(new_attrs.sort(), old_attrs.sort())).toEqual(true);
  1176. }
  1177. done();
  1178. }));
  1179. it("will show fullname and jid properties on tooltip",
  1180. mock.initConverseWithPromises(
  1181. null, ['rosterGroupsFetched'], {},
  1182. function (done, _converse) {
  1183. test_utils.createContacts(_converse, 'all').openControlBox();
  1184. test_utils.waitUntil(function () {
  1185. return $(_converse.rosterview.el).find('.roster-group li').length;
  1186. }, 700).then(function () {
  1187. var jid, name, i;
  1188. for (i=0; i<mock.cur_names.length; i++) {
  1189. name = mock.cur_names[i];
  1190. jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  1191. var $dd = $(_converse.rosterview.el).find("li:contains('"+name+"')").children().first();
  1192. var dd_text = $dd.text();
  1193. var dd_title = $dd.attr('title');
  1194. expect(_.trim(dd_text)).toBe(name);
  1195. expect(dd_title).toContain(name);
  1196. expect(dd_title).toContain(jid);
  1197. }
  1198. done();
  1199. });
  1200. }));
  1201. });
  1202. });
  1203. }));