controlbox.js 32 KB

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