controlbox.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. (function (root, factory) {
  2. define([
  3. "mock",
  4. "utils"
  5. ], function (mock, utils) {
  6. return factory(mock, utils);
  7. }
  8. );
  9. } (this, function (mock, utils) {
  10. var checkHeaderToggling = function ($header) {
  11. var $toggle = $header.find('a.group-toggle');
  12. expect($header.css('display')).toEqual('block');
  13. spyOn(this.rosterview, 'toggleGroup').andCallThrough();
  14. expect($header.nextUntil('dt', 'dd').length === $header.nextUntil('dt', 'dd:visible').length).toBeTruthy();
  15. this.rosterview.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  16. expect($toggle.hasClass('icon-closed')).toBeFalsy();
  17. expect($toggle.hasClass('icon-opened')).toBeTruthy();
  18. $toggle.click();
  19. expect(this.rosterview.toggleGroup).toHaveBeenCalled();
  20. expect($toggle.hasClass('icon-closed')).toBeTruthy();
  21. expect($toggle.hasClass('icon-opened')).toBeFalsy();
  22. expect($header.nextUntil('dt', 'dd').length === $header.nextUntil('dt', 'dd:hidden').length).toBeTruthy();
  23. $toggle.click();
  24. expect(this.rosterview.toggleGroup).toHaveBeenCalled();
  25. expect($toggle.hasClass('icon-closed')).toBeFalsy();
  26. expect($toggle.hasClass('icon-opened')).toBeTruthy();
  27. expect($header.nextUntil('dt', 'dd').length === $header.nextUntil('dt', 'dd:visible').length).toBeTruthy();
  28. };
  29. describe("The Control Box", $.proxy(function (mock, utils) {
  30. beforeEach(function () {
  31. runs(function () {
  32. utils.openControlBox();
  33. });
  34. });
  35. it("can be opened by clicking a DOM element with class 'toggle-controlbox'", $.proxy(function () {
  36. runs(function () {
  37. utils.closeControlBox();
  38. });
  39. waits(50);
  40. runs(function () {
  41. // This spec will only pass if the controlbox is not currently
  42. // open yet.
  43. expect($("div#controlbox").is(':visible')).toBe(false);
  44. spyOn(this.controlboxtoggle, 'onClick').andCallThrough();
  45. spyOn(this.controlboxtoggle, 'showControlBox').andCallThrough();
  46. spyOn(converse, 'emit');
  47. // Redelegate so that the spies are now registered as the event handlers (specifically for 'onClick')
  48. this.controlboxtoggle.delegateEvents();
  49. $('.toggle-controlbox').click();
  50. }.bind(converse));
  51. waits(50);
  52. runs(function () {
  53. expect(this.controlboxtoggle.onClick).toHaveBeenCalled();
  54. expect(this.controlboxtoggle.showControlBox).toHaveBeenCalled();
  55. expect(this.emit).toHaveBeenCalledWith('controlBoxOpened', jasmine.any(Object));
  56. expect($("div#controlbox").is(':visible')).toBe(true);
  57. }.bind(converse));
  58. }, converse));
  59. describe("The Status Widget", $.proxy(function () {
  60. beforeEach(function () {
  61. utils.openControlBox();
  62. });
  63. it("shows the user's chat status, which is online by default", $.proxy(function () {
  64. var view = this.xmppstatusview;
  65. expect(view.$el.find('a.choose-xmpp-status').hasClass('online')).toBe(true);
  66. expect(view.$el.find('a.choose-xmpp-status').attr('data-value')).toBe('I am online');
  67. }, converse));
  68. it("can be used to set the current user's chat status", $.proxy(function () {
  69. var view = this.xmppstatusview;
  70. spyOn(view, 'toggleOptions').andCallThrough();
  71. spyOn(view, 'setStatus').andCallThrough();
  72. spyOn(converse, 'emit');
  73. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  74. runs(function () {
  75. view.$el.find('a.choose-xmpp-status').click();
  76. expect(view.toggleOptions).toHaveBeenCalled();
  77. });
  78. waits(250);
  79. runs(function () {
  80. spyOn(view, 'updateStatusUI').andCallThrough();
  81. view.initialize(); // Rebind events for spy
  82. $(view.$el.find('.dropdown dd ul li a')[1]).click(); // Change status to "dnd"
  83. expect(view.setStatus).toHaveBeenCalled();
  84. expect(converse.emit).toHaveBeenCalledWith('statusChanged', 'dnd');
  85. });
  86. waits(250);
  87. runs($.proxy(function () {
  88. expect(view.updateStatusUI).toHaveBeenCalled();
  89. expect(view.$el.find('a.choose-xmpp-status').hasClass('online')).toBe(false);
  90. expect(view.$el.find('a.choose-xmpp-status').hasClass('dnd')).toBe(true);
  91. expect(view.$el.find('a.choose-xmpp-status').attr('data-value')).toBe('I am busy');
  92. }, converse));
  93. }, converse));
  94. it("can be used to set a custom status message", $.proxy(function () {
  95. var view = this.xmppstatusview;
  96. this.xmppstatus.save({'status': 'online'});
  97. spyOn(view, 'setStatusMessage').andCallThrough();
  98. spyOn(view, 'renderStatusChangeForm').andCallThrough();
  99. spyOn(converse, 'emit');
  100. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  101. view.$el.find('a.change-xmpp-status-message').click();
  102. expect(view.renderStatusChangeForm).toHaveBeenCalled();
  103. // The async testing here is used only to provide time for
  104. // visual feedback
  105. var msg = 'I am happy';
  106. runs (function () {
  107. view.$el.find('form input.custom-xmpp-status').val(msg);
  108. });
  109. waits(250);
  110. runs (function () {
  111. view.$el.find('form#set-custom-xmpp-status').submit();
  112. expect(view.setStatusMessage).toHaveBeenCalled();
  113. expect(converse.emit).toHaveBeenCalledWith('statusMessageChanged', msg);
  114. expect(view.$el.find('a.choose-xmpp-status').hasClass('online')).toBe(true);
  115. expect(view.$el.find('a.choose-xmpp-status').attr('data-value')).toBe(msg);
  116. });
  117. }, converse));
  118. }, converse));
  119. }, converse, mock, utils));
  120. describe("The Contacts Roster", $.proxy(function (mock, utils) {
  121. describe("Roster Groups", $.proxy(function () {
  122. function _clearContacts () {
  123. utils.clearBrowserStorage();
  124. converse.rosterview.model.reset();
  125. converse.rosterview.initialize(); // Register new listeners and document fragment
  126. };
  127. it("can be used to organize existing contacts", $.proxy(function () {
  128. _clearContacts();
  129. var i=0, j=0, t;
  130. spyOn(converse, 'emit');
  131. spyOn(this.rosterview, 'updateCount').andCallThrough();
  132. converse.roster_groups = true;
  133. converse.rosterview.render();
  134. utils.createContacts('pending');
  135. utils.createContacts('requesting');
  136. var groups = {
  137. 'colleagues': 3,
  138. 'friends & acquaintences': 3,
  139. 'Family': 4,
  140. 'ænemies': 3,
  141. 'Ungrouped': 2
  142. };
  143. _.each(_.keys(groups), $.proxy(function (name) {
  144. j = i;
  145. for (i=j; i<j+groups[name]; i++) {
  146. this.roster.create({
  147. jid: mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  148. subscription: 'both',
  149. ask: null,
  150. groups: name === 'ungrouped'? [] : [name],
  151. fullname: mock.cur_names[i],
  152. is_last: i===(mock.cur_names.length-1)
  153. });
  154. }
  155. }, converse));
  156. // Check that the groups appear alphabetically and that
  157. // requesting and pending contacts are last.
  158. var group_titles = $.map(this.rosterview.$el.find('dt'), function (o) { return $(o).text().trim(); });
  159. expect(group_titles).toEqual([
  160. "colleagues",
  161. "Family",
  162. "friends & acquaintences",
  163. "Ungrouped",
  164. "Contact requests",
  165. "Pending contacts"
  166. ]);
  167. // Check that usernames appear alphabetically per group
  168. _.each(_.keys(groups), $.proxy(function (name) {
  169. var $contacts = this.rosterview.$('dt.roster-group[data-group="'+name+'"]').nextUntil('dt', 'dd');
  170. var names = $.map($contacts, function (o) { return $(o).text().trim(); });
  171. expect(names).toEqual(_.clone(names).sort());
  172. }, converse));
  173. // Check that pending and requesting contacts appear after
  174. // current contacts.
  175. }, converse));
  176. }, converse));
  177. describe("Pending Contacts", $.proxy(function () {
  178. function _clearContacts () {
  179. utils.clearBrowserStorage();
  180. converse.rosterview.model.reset();
  181. converse.rosterview.initialize(); // Register new listeners and document fragment
  182. };
  183. function _addContacts () {
  184. _clearContacts();
  185. // Must be initialized, so that render is called and documentFragment set up.
  186. utils.createContacts('pending').openControlBox().openContactsPanel();
  187. };
  188. it("do not have a header if there aren't any", $.proxy(function () {
  189. _addContacts();
  190. this.rosterview.model.reset();
  191. expect(this.rosterview.$el.find('dt#pending-xmpp-contacts').css('display')).toEqual('none');
  192. }, converse));
  193. it("can be collapsed under their own header", $.proxy(function () {
  194. _addContacts();
  195. checkHeaderToggling.apply(this, [this.rosterview.$el.find('dt#pending-xmpp-contacts')]);
  196. }, converse));
  197. it("can be added to the roster", $.proxy(function () {
  198. _clearContacts();
  199. spyOn(converse, 'emit');
  200. spyOn(this.rosterview, 'updateCount').andCallThrough();
  201. runs($.proxy(function () {
  202. this.roster.create({
  203. jid: mock.pend_names[0].replace(/ /g,'.').toLowerCase() + '@localhost',
  204. subscription: 'none',
  205. ask: 'subscribe',
  206. fullname: mock.pend_names[0],
  207. is_last: true
  208. });
  209. }, converse));
  210. waits(300);
  211. runs($.proxy(function () {
  212. expect(this.rosterview.$el.is(':visible')).toEqual(true);
  213. expect(this.rosterview.updateCount).toHaveBeenCalled();
  214. }, converse));
  215. }, converse));
  216. it("can be removed by the user", $.proxy(function () {
  217. _addContacts();
  218. var jid = mock.pend_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
  219. var view = this.rosterview.get(jid);
  220. spyOn(window, 'confirm').andReturn(true);
  221. spyOn(converse, 'emit');
  222. spyOn(this.connection.roster, 'remove').andCallThrough();
  223. spyOn(this.connection.roster, 'unauthorize');
  224. spyOn(this.rosterview.model, 'remove').andCallThrough();
  225. view.$el.find('.remove-xmpp-contact').click();
  226. expect(window.confirm).toHaveBeenCalled();
  227. expect(this.connection.roster.remove).toHaveBeenCalled();
  228. expect(this.connection.roster.unauthorize).toHaveBeenCalled();
  229. expect(this.rosterview.model.remove).toHaveBeenCalled();
  230. // The element must now be detached from the DOM.
  231. expect(view.$el.closest('html').length).toBeFalsy();
  232. }, converse));
  233. it("will lose their own header once the last one has been removed", $.proxy(function () {
  234. _addContacts();
  235. var view;
  236. spyOn(window, 'confirm').andReturn(true);
  237. for (i=0; i<mock.pend_names.length; i++) {
  238. view = this.rosterview.get(mock.pend_names[i].replace(/ /g,'.').toLowerCase() + '@localhost');
  239. view.$el.find('.remove-xmpp-contact').click();
  240. }
  241. expect(this.rosterview.$el.find('dt#pending-xmpp-contacts').is(':visible')).toBeFalsy();
  242. }, converse));
  243. it("can be added to the roster and they will be sorted alphabetically", $.proxy(function () {
  244. _clearContacts();
  245. var i, t, is_last;
  246. spyOn(converse, 'emit');
  247. spyOn(this.rosterview, 'updateCount').andCallThrough();
  248. for (i=0; i<mock.pend_names.length; i++) {
  249. is_last = i===(mock.pend_names.length-1);
  250. this.roster.create({
  251. jid: mock.pend_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  252. subscription: 'none',
  253. ask: 'subscribe',
  254. fullname: mock.pend_names[i],
  255. is_last: is_last
  256. });
  257. expect(this.rosterview.updateCount).toHaveBeenCalled();
  258. }
  259. // Check that they are sorted alphabetically
  260. t = this.rosterview.$el.find('dt#pending-xmpp-contacts').siblings('dd.pending-xmpp-contact').find('span').text();
  261. expect(t).toEqual(mock.pend_names.slice(0,i+1).sort().join(''));
  262. }, converse));
  263. }, converse));
  264. describe("Existing Contacts", $.proxy(function () {
  265. function _clearContacts () {
  266. utils.clearBrowserStorage();
  267. converse.rosterview.model.reset();
  268. converse.rosterview.initialize(); // Register new listeners and document fragment
  269. };
  270. var _addContacts = function () {
  271. _clearContacts();
  272. utils.createContacts().openControlBox().openContactsPanel();
  273. };
  274. it("do not have a header if there aren't any", $.proxy(function () {
  275. // To properly tests, we add contacts and then remove them all again.
  276. _addContacts();
  277. this.rosterview.model.reset();
  278. expect(this.rosterview.$el.find('dt.roster-group').css('display')).toEqual('none');
  279. }, converse));
  280. it("can be collapsed under their own header", $.proxy(function () {
  281. _addContacts();
  282. checkHeaderToggling.apply(this, [this.rosterview.$el.find('dt.roster-group')]);
  283. }, converse));
  284. it("can be added to the roster and they will be sorted alphabetically", $.proxy(function () {
  285. _clearContacts();
  286. var i, t;
  287. spyOn(converse, 'emit');
  288. spyOn(this.rosterview, 'updateCount').andCallThrough();
  289. for (i=0; i<mock.cur_names.length; i++) {
  290. this.roster.create({
  291. jid: mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  292. subscription: 'both',
  293. ask: null,
  294. fullname: mock.cur_names[i],
  295. is_last: i===(mock.cur_names.length-1)
  296. });
  297. expect(this.rosterview.updateCount).toHaveBeenCalled();
  298. }
  299. // Check that they are sorted alphabetically
  300. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.offline').find('a.open-chat').text();
  301. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  302. }, converse));
  303. it("can change their status to online and be sorted alphabetically", $.proxy(function () {
  304. _addContacts();
  305. var item, view, jid, t;
  306. spyOn(converse, 'emit');
  307. spyOn(this.rosterview, 'updateCount').andCallThrough();
  308. for (i=0; i<mock.cur_names.length; i++) {
  309. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  310. view = this.rosterview.get(jid);
  311. spyOn(view, 'render').andCallThrough();
  312. item = view.model;
  313. item.set('chat_status', 'online');
  314. expect(view.render).toHaveBeenCalled();
  315. expect(this.rosterview.updateCount).toHaveBeenCalled();
  316. // Check that they are sorted alphabetically
  317. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.online').find('a.open-chat').text();
  318. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  319. }
  320. }, converse));
  321. it("can change their status to busy and be sorted alphabetically", $.proxy(function () {
  322. _addContacts();
  323. var item, view, jid, t;
  324. spyOn(converse, 'emit');
  325. spyOn(this.rosterview, 'updateCount').andCallThrough();
  326. for (i=0; i<mock.cur_names.length; i++) {
  327. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  328. view = this.rosterview.get(jid);
  329. spyOn(view, 'render').andCallThrough();
  330. item = view.model;
  331. item.set('chat_status', 'dnd');
  332. expect(view.render).toHaveBeenCalled();
  333. expect(this.rosterview.updateCount).toHaveBeenCalled();
  334. // Check that they are sorted alphabetically
  335. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.dnd').find('a.open-chat').text();
  336. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  337. }
  338. }, converse));
  339. it("can change their status to away and be sorted alphabetically", $.proxy(function () {
  340. _addContacts();
  341. var item, view, jid, t;
  342. spyOn(converse, 'emit');
  343. spyOn(this.rosterview, 'updateCount').andCallThrough();
  344. for (i=0; i<mock.cur_names.length; i++) {
  345. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  346. view = this.rosterview.get(jid);
  347. spyOn(view, 'render').andCallThrough();
  348. item = view.model;
  349. item.set('chat_status', 'away');
  350. expect(view.render).toHaveBeenCalled();
  351. expect(this.rosterview.updateCount).toHaveBeenCalled();
  352. // Check that they are sorted alphabetically
  353. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.away').find('a.open-chat').text();
  354. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  355. }
  356. }, converse));
  357. it("can change their status to xa and be sorted alphabetically", $.proxy(function () {
  358. _addContacts();
  359. var item, view, jid, t;
  360. spyOn(converse, 'emit');
  361. spyOn(this.rosterview, 'updateCount').andCallThrough();
  362. for (i=0; i<mock.cur_names.length; i++) {
  363. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  364. view = this.rosterview.get(jid);
  365. spyOn(view, 'render').andCallThrough();
  366. item = view.model;
  367. item.set('chat_status', 'xa');
  368. expect(view.render).toHaveBeenCalled();
  369. expect(this.rosterview.updateCount).toHaveBeenCalled();
  370. // Check that they are sorted alphabetically
  371. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.xa').find('a.open-chat').text();
  372. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  373. }
  374. }, converse));
  375. it("can change their status to unavailable and be sorted alphabetically", $.proxy(function () {
  376. _addContacts();
  377. var item, view, jid, t;
  378. spyOn(converse, 'emit');
  379. spyOn(this.rosterview, 'updateCount').andCallThrough();
  380. for (i=0; i<mock.cur_names.length; i++) {
  381. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  382. view = this.rosterview.get(jid);
  383. spyOn(view, 'render').andCallThrough();
  384. item = view.model;
  385. item.set('chat_status', 'unavailable');
  386. expect(view.render).toHaveBeenCalled();
  387. expect(this.rosterview.updateCount).toHaveBeenCalled();
  388. // Check that they are sorted alphabetically
  389. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.unavailable').find('a.open-chat').text();
  390. expect(t).toEqual(mock.cur_names.slice(0, i+1).sort().join(''));
  391. }
  392. }, converse));
  393. it("are ordered according to status: online, busy, away, xa, unavailable, offline", $.proxy(function () {
  394. _addContacts();
  395. var i;
  396. for (i=0; i<3; i++) {
  397. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  398. view = this.rosterview.get(jid);
  399. view.model.set('chat_status', 'online');
  400. }
  401. for (i=3; i<6; i++) {
  402. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  403. view = this.rosterview.get(jid);
  404. view.model.set('chat_status', 'dnd');
  405. }
  406. for (i=6; i<9; i++) {
  407. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  408. view = this.rosterview.get(jid);
  409. view.model.set('chat_status', 'away');
  410. }
  411. for (i=9; i<12; i++) {
  412. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  413. view = this.rosterview.get(jid);
  414. view.model.set('chat_status', 'xa');
  415. }
  416. for (i=12; i<15; i++) {
  417. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  418. view = this.rosterview.get(jid);
  419. view.model.set('chat_status', 'unavailable');
  420. }
  421. var contacts = this.rosterview.$el.find('dd.current-xmpp-contact');
  422. for (i=0; i<3; i++) {
  423. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('online');
  424. }
  425. for (i=3; i<6; i++) {
  426. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('dnd');
  427. }
  428. for (i=6; i<9; i++) {
  429. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('away');
  430. }
  431. for (i=9; i<12; i++) {
  432. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('xa');
  433. }
  434. for (i=12; i<15; i++) {
  435. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('unavailable');
  436. }
  437. for (i=15; i<mock.cur_names.length; i++) {
  438. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('offline');
  439. }
  440. }, converse));
  441. }, converse));
  442. describe("Requesting Contacts", $.proxy(function () {
  443. beforeEach($.proxy(function () {
  444. runs(function () {
  445. utils.clearBrowserStorage();
  446. converse.rosterview.model.reset();
  447. utils.createContacts('requesting').openControlBox();
  448. });
  449. waits(50);
  450. runs(function () {
  451. utils.openContactsPanel();
  452. });
  453. }, converse));
  454. it("do not have a header if there aren't any", $.proxy(function () {
  455. // by default the dts are hidden from css class and only later they will be hidden
  456. // by jQuery therefore for the first check we will see if visible instead of none
  457. converse.rosterview.model.reset();
  458. expect(this.rosterview.$el.find('dt#xmpp-contact-requests').is(':visible')).toEqual(false);
  459. }, converse));
  460. it("can be added to the roster and they will be sorted alphabetically", $.proxy(function () {
  461. converse.rosterview.model.reset(); // We want to manually create users so that we can spy
  462. var i, children;
  463. var names = [];
  464. spyOn(converse, 'emit');
  465. spyOn(this.rosterview, 'updateCount').andCallThrough();
  466. spyOn(this.controlboxtoggle, 'showControlBox').andCallThrough();
  467. var addName = function (idx, item) {
  468. if (!$(item).hasClass('request-actions')) {
  469. names.push($(item).text().replace(/^\s+|\s+$/g, ''));
  470. }
  471. };
  472. for (i=0; i<mock.req_names.length; i++) {
  473. this.roster.create({
  474. jid: mock.req_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  475. subscription: 'none',
  476. ask: null,
  477. requesting: true,
  478. fullname: mock.req_names[i],
  479. is_last: i===(mock.req_names.length-1)
  480. });
  481. expect(this.rosterview.updateCount).toHaveBeenCalled();
  482. // When a requesting contact is added, the controlbox must
  483. // be opened.
  484. expect(this.controlboxtoggle.showControlBox).toHaveBeenCalled();
  485. }
  486. // Check that they are sorted alphabetically
  487. children = this.rosterview.$el.find('dt#xmpp-contact-requests').siblings('dd.requesting-xmpp-contact').children('span');
  488. names = [];
  489. children.each(addName);
  490. expect(names.join('')).toEqual(mock.req_names.slice(0,i+1).sort().join(''));
  491. }, converse));
  492. it("can be collapsed under their own header", $.proxy(function () {
  493. checkHeaderToggling.apply(this, [this.rosterview.$el.find('dt#xmpp-contact-requests')]);
  494. }, converse));
  495. it("can have their requests accepted by the user", $.proxy(function () {
  496. // TODO: Testing can be more thorough here, the user is
  497. // actually not accepted/authorized because of
  498. // mock_connection.
  499. var jid = mock.req_names.sort()[0].replace(/ /g,'.').toLowerCase() + '@localhost';
  500. var view = this.rosterview.get(jid);
  501. spyOn(this.connection.roster, 'authorize');
  502. spyOn(view, 'acceptRequest').andCallThrough();
  503. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  504. var accept_button = view.$el.find('.accept-xmpp-request');
  505. accept_button.click();
  506. expect(view.acceptRequest).toHaveBeenCalled();
  507. expect(this.connection.roster.authorize).toHaveBeenCalled();
  508. }, converse));
  509. it("can have their requests denied by the user", $.proxy(function () {
  510. this.rosterview.model.reset();
  511. spyOn(converse, 'emit');
  512. spyOn(this.connection.roster, 'unauthorize');
  513. spyOn(this.rosterview, 'removeRosterItemView').andCallThrough();
  514. spyOn(window, 'confirm').andReturn(true);
  515. this.rosterview.initialize(); // Must be initialized only after the spy has been called
  516. utils.createContacts('requesting').openControlBox();
  517. var jid = mock.req_names.sort()[1].replace(/ /g,'.').toLowerCase() + '@localhost';
  518. var view = this.rosterview.get(jid);
  519. spyOn(view, 'declineRequest').andCallThrough();
  520. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  521. var accept_button = view.$el.find('.decline-xmpp-request');
  522. accept_button.click();
  523. expect(view.declineRequest).toHaveBeenCalled();
  524. expect(window.confirm).toHaveBeenCalled();
  525. expect(this.rosterview.removeRosterItemView).toHaveBeenCalled();
  526. expect(this.connection.roster.unauthorize).toHaveBeenCalled();
  527. // There should now be one less contact
  528. expect(this.roster.length).toEqual(mock.req_names.length-1);
  529. }, converse));
  530. }, converse));
  531. describe("All Contacts", $.proxy(function () {
  532. beforeEach($.proxy(function () {
  533. runs(function () {
  534. utils.clearBrowserStorage();
  535. converse.rosterview.model.reset();
  536. converse.rosterview.model.browserStorage._clear();
  537. utils.createContacts('all').openControlBox();
  538. });
  539. waits(50);
  540. runs(function () {
  541. utils.openContactsPanel();
  542. });
  543. }, converse));
  544. it("are saved to, and can be retrieved from, browserStorage", $.proxy(function () {
  545. var new_attrs, old_attrs, attrs, old_roster;
  546. var num_contacts = this.roster.length;
  547. new_roster = new this.RosterItems();
  548. // Roster items are yet to be fetched from browserStorage
  549. expect(new_roster.length).toEqual(0);
  550. new_roster.browserStorage = new Backbone.BrowserStorage.session(
  551. b64_sha1('converse.rosteritems-dummy@localhost'));
  552. new_roster.fetch();
  553. expect(new_roster.length).toEqual(num_contacts);
  554. // Check that the roster items retrieved from browserStorage
  555. // have the same attributes values as the original ones.
  556. attrs = ['jid', 'fullname', 'subscription', 'ask'];
  557. for (i=0; i<attrs.length; i++) {
  558. new_attrs = _.pluck(_.pluck(new_roster.models, 'attributes'), attrs[i]);
  559. old_attrs = _.pluck(_.pluck(this.roster.models, 'attributes'), attrs[i]);
  560. // Roster items in storage are not necessarily sorted,
  561. // so we have to sort them here to do a proper
  562. // comparison
  563. expect(_.isEqual(new_attrs.sort(), old_attrs.sort())).toEqual(true);
  564. }
  565. // XXX: this.rosterview.updateCount();
  566. }, converse));
  567. afterEach($.proxy(function () {
  568. // Contacts retrieved from browserStorage have chat_status of
  569. // "offline".
  570. // In the next test suite, we need some online contacts, so
  571. // we make some online now
  572. for (i=0; i<5; i++) {
  573. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  574. view = this.rosterview.get(jid);
  575. view.model.set('chat_status', 'online');
  576. }
  577. }, converse));
  578. }, converse));
  579. }, converse, mock, utils));
  580. describe("The 'Add Contact' widget", $.proxy(function (mock, utils) {
  581. it("opens up an add form when you click on it", $.proxy(function () {
  582. var panel = this.chatboxviews.get('controlbox').contactspanel;
  583. spyOn(panel, 'toggleContactForm').andCallThrough();
  584. panel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  585. panel.$el.find('a.toggle-xmpp-contact-form').click();
  586. expect(panel.toggleContactForm).toHaveBeenCalled();
  587. // XXX: Awaiting more tests, close it again for now...
  588. panel.$el.find('a.toggle-xmpp-contact-form').click();
  589. }, converse));
  590. }, converse, mock, utils));
  591. describe("The Controlbox Tabs", $.proxy(function () {
  592. beforeEach($.proxy(function () {
  593. runs(function () {
  594. utils.closeAllChatBoxes();
  595. });
  596. waits(50);
  597. runs(function () {
  598. utils.openControlBox();
  599. });
  600. }, converse));
  601. it("contains two tabs, 'Contacts' and 'ChatRooms'", $.proxy(function () {
  602. var cbview = this.chatboxviews.get('controlbox');
  603. var $panels = cbview.$el.find('.controlbox-panes');
  604. expect($panels.children().length).toBe(2);
  605. expect($panels.children().first().attr('id')).toBe('users');
  606. expect($panels.children().first().is(':visible')).toBe(true);
  607. expect($panels.children().last().attr('id')).toBe('chatrooms');
  608. expect($panels.children().last().is(':visible')).toBe(false);
  609. }, converse));
  610. describe("chatrooms panel", $.proxy(function () {
  611. beforeEach($.proxy(function () {
  612. runs(function () {
  613. utils.closeAllChatBoxes();
  614. });
  615. waits(50);
  616. runs(function () {
  617. utils.openControlBox();
  618. });
  619. }, converse));
  620. it("is opened by clicking the 'Chatrooms' tab", $.proxy(function () {
  621. var cbview = this.chatboxviews.get('controlbox');
  622. var $tabs = cbview.$el.find('#controlbox-tabs');
  623. var $panels = cbview.$el.find('.controlbox-panes');
  624. var $contacts = $panels.children().first();
  625. var $chatrooms = $panels.children().last();
  626. spyOn(cbview, 'switchTab').andCallThrough();
  627. cbview.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  628. runs(function () {
  629. $tabs.find('li').last().find('a').click(); // Clicks the chatrooms tab
  630. });
  631. waits(250);
  632. runs(function () {
  633. expect($contacts.is(':visible')).toBe(false);
  634. expect($chatrooms.is(':visible')).toBe(true);
  635. expect(cbview.switchTab).toHaveBeenCalled();
  636. });
  637. }, converse));
  638. it("contains a form through which a new chatroom can be created", $.proxy(function () {
  639. var roomspanel = this.chatboxviews.get('controlbox').roomspanel;
  640. var $input = roomspanel.$el.find('input.new-chatroom-name');
  641. var $nick = roomspanel.$el.find('input.new-chatroom-nick');
  642. var $server = roomspanel.$el.find('input.new-chatroom-server');
  643. expect($input.length).toBe(1);
  644. expect($server.length).toBe(1);
  645. expect($('.chatroom:visible').length).toBe(0); // There shouldn't be any chatrooms open currently
  646. spyOn(roomspanel, 'createChatRoom').andCallThrough();
  647. roomspanel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  648. runs(function () {
  649. $input.val('Lounge');
  650. $nick.val('dummy');
  651. $server.val('muc.localhost');
  652. });
  653. waits('250');
  654. runs(function () {
  655. roomspanel.$el.find('form').submit();
  656. expect(roomspanel.createChatRoom).toHaveBeenCalled();
  657. });
  658. waits('250');
  659. runs($.proxy(function () {
  660. expect($('.chatroom:visible').length).toBe(1); // There should now be an open chatroom
  661. }, converse));
  662. }, converse));
  663. }, converse));
  664. }, converse, mock, utils));
  665. }));