controlbox.js 31 KB

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