controlbox.js 35 KB

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