controlbox.js 70 KB

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