controlbox.js 35 KB

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