MainSpec.js 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. (function (root, factory) {
  2. define([
  3. "mock"
  4. ], function (mock_connection) {
  5. return factory(mock_connection);
  6. }
  7. );
  8. } (this, function (mock_connection) {
  9. return describe("Converse.js", function() {
  10. // Names from http://www.fakenamegenerator.com/
  11. var req_names = [
  12. 'Louw Spekman', 'Mohamad Stet', 'Dominik Beyer'
  13. ];
  14. var pend_names = [
  15. 'Suleyman van Beusichem', 'Nanja van Yperen', 'Nicole Diederich'
  16. ];
  17. var cur_names = [
  18. 'Max Frankfurter', 'Candice van der Knijff', 'Irini Vlastuin', 'Rinse Sommer', 'Annegreet Gomez',
  19. 'Robin Schook', 'Marcel Eberhardt', 'Simone Brauer', 'Asmaa Haakman', 'Felix Amsel',
  20. 'Lena Grunewald', 'Laura Grunewald', 'Mandy Seiler', 'Sven Bosch', 'Nuriye Cuypers',
  21. 'Staal Burger', 'Brenner Brand', 'Vleis Visagie'
  22. ];
  23. var num_contacts = req_names.length + pend_names.length + cur_names.length;
  24. var open_controlbox;
  25. closeAllChatBoxes = function () {
  26. var i, chatbox, num_chatboxes = converse.chatboxes.models.length;
  27. for (i=num_chatboxes-1; i>-1; i--) {
  28. chatbox = converse.chatboxes.models[i];
  29. converse.chatboxesview.views[chatbox.get('id')].closeChat();
  30. }
  31. };
  32. openControlBox = function () {
  33. if (!$("div#controlbox").is(':visible')) {
  34. $('.toggle-online-users').click();
  35. }
  36. };
  37. describe("The Control Box", $.proxy(function () {
  38. it("is not shown by default", $.proxy(function () {
  39. expect(this.rosterview.$el.is(':visible')).toEqual(false);
  40. }, converse));
  41. open_controlbox = $.proxy(function () {
  42. // This spec will only pass if the controlbox is not currently
  43. // open yet.
  44. expect($("div#controlbox").is(':visible')).toBe(false);
  45. spyOn(this.controlboxtoggle, 'onClick').andCallThrough();
  46. spyOn(this.controlboxtoggle, 'showControlBox').andCallThrough();
  47. // Redelegate so that the spies are now registered as the event handlers (specifically for 'onClick')
  48. this.controlboxtoggle.delegateEvents();
  49. $('.toggle-online-users').click();
  50. expect(this.controlboxtoggle.onClick).toHaveBeenCalled();
  51. expect(this.controlboxtoggle.showControlBox).toHaveBeenCalled();
  52. expect($("div#controlbox").is(':visible')).toBe(true);
  53. }, converse);
  54. it("can be opened by clicking a DOM element with class 'toggle-online-users'", open_controlbox);
  55. describe("The Status Widget", $.proxy(function () {
  56. it("shows the user's chat status, which is online by default", $.proxy(function () {
  57. var view = this.xmppstatusview;
  58. expect(view.$el.find('a.choose-xmpp-status').hasClass('online')).toBe(true);
  59. expect(view.$el.find('a.choose-xmpp-status').attr('data-value')).toBe('I am online');
  60. }, converse));
  61. it("can be used to set the current user's chat status", $.proxy(function () {
  62. var view = this.xmppstatusview;
  63. spyOn(view, 'toggleOptions').andCallThrough();
  64. spyOn(view, 'setStatus').andCallThrough();
  65. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  66. runs(function () {
  67. view.$el.find('a.choose-xmpp-status').click();
  68. expect(view.toggleOptions).toHaveBeenCalled();
  69. });
  70. waits(250);
  71. runs(function () {
  72. spyOn(view, 'updateStatusUI').andCallThrough();
  73. view.initialize(); // Rebind events for spy
  74. $(view.$el.find('.dropdown dd ul li a')[1]).click();
  75. expect(view.setStatus).toHaveBeenCalled();
  76. });
  77. waits(250);
  78. runs($.proxy(function () {
  79. expect(view.updateStatusUI).toHaveBeenCalled();
  80. expect(view.$el.find('a.choose-xmpp-status').hasClass('online')).toBe(false);
  81. expect(view.$el.find('a.choose-xmpp-status').hasClass('dnd')).toBe(true);
  82. expect(view.$el.find('a.choose-xmpp-status').attr('data-value')).toBe('I am busy');
  83. }, converse));
  84. }, converse));
  85. it("can be used to set a custom status message", $.proxy(function () {
  86. var view = this.xmppstatusview;
  87. this.xmppstatus.save({'status': 'online'});
  88. spyOn(view, 'setStatusMessage').andCallThrough();
  89. spyOn(view, 'renderStatusChangeForm').andCallThrough();
  90. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  91. view.$el.find('a.change-xmpp-status-message').click();
  92. expect(view.renderStatusChangeForm).toHaveBeenCalled();
  93. // The async testing here is used only to provide time for
  94. // visual feedback
  95. var msg = 'I am happy';
  96. runs (function () {
  97. view.$el.find('form input.custom-xmpp-status').val(msg);
  98. });
  99. waits(250);
  100. runs (function () {
  101. view.$el.find('form#set-custom-xmpp-status').submit();
  102. expect(view.setStatusMessage).toHaveBeenCalled();
  103. expect(view.$el.find('a.choose-xmpp-status').hasClass('online')).toBe(true);
  104. expect(view.$el.find('a.choose-xmpp-status').attr('data-value')).toBe(msg);
  105. });
  106. }, converse));
  107. }, converse));
  108. }, converse));
  109. describe("The Contacts Roster", $.proxy(function () {
  110. describe("Pending Contacts", $.proxy(function () {
  111. beforeEach(function () {
  112. if (!$("div#controlbox").is(':visible')) {
  113. $('.toggle-online-users').click();
  114. }
  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(this.rosterview, 'render').andCallThrough();
  121. this.roster.create({
  122. jid: pend_names[0].replace(/ /g,'.').toLowerCase() + '@localhost',
  123. subscription: 'none',
  124. ask: 'subscribe',
  125. fullname: pend_names[0],
  126. is_last: true
  127. });
  128. expect(this.rosterview.$el.is(':visible')).toEqual(true);
  129. expect(this.rosterview.render).toHaveBeenCalled();
  130. }, converse));
  131. it("can be removed by the user", $.proxy(function () {
  132. var view = _.toArray(this.rosterview.rosteritemviews).pop();
  133. spyOn(window, 'confirm').andReturn(true);
  134. spyOn(this.connection.roster, 'remove').andCallThrough();
  135. spyOn(this.connection.roster, 'unauthorize');
  136. spyOn(this.rosterview.model, 'remove').andCallThrough();
  137. //spyOn(view, 'removeContact').andCallThrough();
  138. runs($.proxy(function () {
  139. view.$el.find('.remove-xmpp-contact').click();
  140. }, converse));
  141. waits(500);
  142. runs($.proxy(function () {
  143. expect(window.confirm).toHaveBeenCalled();
  144. //expect(view.removeContact).toHaveBeenCalled();
  145. expect(this.connection.roster.remove).toHaveBeenCalled();
  146. expect(this.connection.roster.unauthorize).toHaveBeenCalled();
  147. expect(this.rosterview.model.remove).toHaveBeenCalled();
  148. // The element must now be detached from the DOM.
  149. expect(view.$el.closest('html').length).toBeFalsy();
  150. }, converse));
  151. }, converse));
  152. it("will lose their own heading once the last one has been removed", $.proxy(function () {
  153. expect(this.rosterview.$el.find('dt#pending-xmpp-contacts').is(':visible')).toBeFalsy();
  154. }, converse));
  155. it("can be added to the roster and they will be sorted alphabetically", $.proxy(function () {
  156. var i, t, is_last;
  157. spyOn(this.rosterview, 'render').andCallThrough();
  158. for (i=0; i<pend_names.length; i++) {
  159. is_last = i===(pend_names.length-1);
  160. this.roster.create({
  161. jid: pend_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  162. subscription: 'none',
  163. ask: 'subscribe',
  164. fullname: pend_names[i],
  165. is_last: is_last
  166. });
  167. expect(this.rosterview.render).toHaveBeenCalled();
  168. // Check that they are sorted alphabetically
  169. t = this.rosterview.$el.find('dt#pending-xmpp-contacts').siblings('dd.pending-xmpp-contact').text();
  170. expect(t).toEqual(pend_names.slice(0,i+1).sort().join(''));
  171. }
  172. }, converse));
  173. it("will have their own heading once they have been added", $.proxy(function () {
  174. expect(this.rosterview.$el.find('dt#pending-xmpp-contacts').css('display')).toEqual('block');
  175. }, converse));
  176. }, converse));
  177. describe("Existing Contacts", $.proxy(function () {
  178. beforeEach($.proxy(function () {
  179. openControlBox();
  180. }, converse));
  181. it("do not have a heading if there aren't any", $.proxy(function () {
  182. expect(this.rosterview.$el.find('dt#xmpp-contacts').css('display')).toEqual('none');
  183. }, converse));
  184. it("can be added to the roster and they will be sorted alphabetically", $.proxy(function () {
  185. var i, t;
  186. spyOn(this.rosterview, 'render').andCallThrough();
  187. for (i=0; i<cur_names.length; i++) {
  188. this.roster.create({
  189. jid: cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  190. subscription: 'both',
  191. ask: null,
  192. fullname: cur_names[i],
  193. is_last: i===(cur_names.length-1)
  194. });
  195. expect(this.rosterview.render).toHaveBeenCalled();
  196. // Check that they are sorted alphabetically
  197. t = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.offline').find('a.open-chat').text();
  198. expect(t).toEqual(cur_names.slice(0,i+1).sort().join(''));
  199. }
  200. }, converse));
  201. it("will have their own heading once they have been added", $.proxy(function () {
  202. expect(this.rosterview.$el.find('dt#xmpp-contacts').css('display')).toEqual('block');
  203. }, converse));
  204. it("can change their status to online and be sorted alphabetically", $.proxy(function () {
  205. var item, view, jid, t;
  206. spyOn(this.rosterview, 'render').andCallThrough();
  207. for (i=0; i<3; i++) {
  208. jid = cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  209. view = this.rosterview.rosteritemviews[jid];
  210. spyOn(view, 'render').andCallThrough();
  211. item = view.model;
  212. item.set('chat_status', 'online');
  213. expect(view.render).toHaveBeenCalled();
  214. expect(this.rosterview.render).toHaveBeenCalled();
  215. // Check that they are sorted alphabetically
  216. t = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.online').find('a.open-chat').text();
  217. expect(t).toEqual(cur_names.slice(0,i+1).sort().join(''));
  218. }
  219. }, converse));
  220. it("can change their status to busy and be sorted alphabetically", $.proxy(function () {
  221. var item, view, jid, t;
  222. spyOn(this.rosterview, 'render').andCallThrough();
  223. for (i=3; i<6; i++) {
  224. jid = cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  225. view = this.rosterview.rosteritemviews[jid];
  226. spyOn(view, 'render').andCallThrough();
  227. item = view.model;
  228. item.set('chat_status', 'dnd');
  229. expect(view.render).toHaveBeenCalled();
  230. expect(this.rosterview.render).toHaveBeenCalled();
  231. // Check that they are sorted alphabetically
  232. t = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.dnd').find('a.open-chat').text();
  233. expect(t).toEqual(cur_names.slice(3,i+1).sort().join(''));
  234. }
  235. }, converse));
  236. it("can change their status to away and be sorted alphabetically", $.proxy(function () {
  237. var item, view, jid, t;
  238. spyOn(this.rosterview, 'render').andCallThrough();
  239. for (i=6; i<9; i++) {
  240. jid = cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  241. view = this.rosterview.rosteritemviews[jid];
  242. spyOn(view, 'render').andCallThrough();
  243. item = view.model;
  244. item.set('chat_status', 'away');
  245. expect(view.render).toHaveBeenCalled();
  246. expect(this.rosterview.render).toHaveBeenCalled();
  247. // Check that they are sorted alphabetically
  248. t = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.away').find('a.open-chat').text();
  249. expect(t).toEqual(cur_names.slice(6,i+1).sort().join(''));
  250. }
  251. }, converse));
  252. it("can change their status to xa and be sorted alphabetically", $.proxy(function () {
  253. var item, view, jid, t;
  254. spyOn(this.rosterview, 'render').andCallThrough();
  255. for (i=9; i<12; i++) {
  256. jid = cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  257. view = this.rosterview.rosteritemviews[jid];
  258. spyOn(view, 'render').andCallThrough();
  259. item = view.model;
  260. item.set('chat_status', 'xa');
  261. expect(view.render).toHaveBeenCalled();
  262. expect(this.rosterview.render).toHaveBeenCalled();
  263. // Check that they are sorted alphabetically
  264. t = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.xa').find('a.open-chat').text();
  265. expect(t).toEqual(cur_names.slice(9,i+1).sort().join(''));
  266. }
  267. }, converse));
  268. it("can change their status to unavailable and be sorted alphabetically", $.proxy(function () {
  269. var item, view, jid, t;
  270. spyOn(this.rosterview, 'render').andCallThrough();
  271. for (i=12; i<15; i++) {
  272. jid = cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  273. view = this.rosterview.rosteritemviews[jid];
  274. spyOn(view, 'render').andCallThrough();
  275. item = view.model;
  276. item.set('chat_status', 'unavailable');
  277. expect(view.render).toHaveBeenCalled();
  278. expect(this.rosterview.render).toHaveBeenCalled();
  279. // Check that they are sorted alphabetically
  280. t = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.unavailable').find('a.open-chat').text();
  281. expect(t).toEqual(cur_names.slice(12, i+1).sort().join(''));
  282. }
  283. }, converse));
  284. it("are ordered according to status: online, busy, away, xa, unavailable, offline", $.proxy(function () {
  285. var contacts = this.rosterview.$el.find('dd.current-xmpp-contact');
  286. var i;
  287. for (i=0; i<3; i++) {
  288. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('online');
  289. }
  290. for (i=3; i<6; i++) {
  291. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('dnd');
  292. }
  293. for (i=6; i<9; i++) {
  294. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('away');
  295. }
  296. for (i=9; i<12; i++) {
  297. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('xa');
  298. }
  299. for (i=12; i<15; i++) {
  300. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('unavailable');
  301. }
  302. for (i=15; i<cur_names.length; i++) {
  303. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('offline');
  304. }
  305. }, converse));
  306. }, converse));
  307. describe("Requesting Contacts", $.proxy(function () {
  308. // by default the dts are hidden from css class and only later they will be hidden
  309. // by jQuery therefore for the first check we will see if visible instead of none
  310. it("do not have a heading if there aren't any", $.proxy(function () {
  311. expect(this.rosterview.$el.find('dt#xmpp-contact-requests').is(':visible')).toEqual(false);
  312. }, converse));
  313. it("can be added to the roster and they will be sorted alphabetically", $.proxy(function () {
  314. var i, t;
  315. spyOn(this.rosterview, 'render').andCallThrough();
  316. spyOn(this.controlboxtoggle, 'showControlBox').andCallThrough();
  317. for (i=0; i<req_names.length; i++) {
  318. this.roster.create({
  319. jid: req_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  320. subscription: 'none',
  321. ask: 'request',
  322. fullname: req_names[i],
  323. is_last: i===(req_names.length-1)
  324. });
  325. expect(this.rosterview.render).toHaveBeenCalled();
  326. // Check that they are sorted alphabetically
  327. t = this.rosterview.$el.find('dt#xmpp-contact-requests').siblings('dd.requesting-xmpp-contact').text().replace(/AcceptDecline/g, '');
  328. expect(t).toEqual(req_names.slice(0,i+1).sort().join(''));
  329. // When a requesting contact is added, the controlbox must
  330. // be opened.
  331. expect(this.controlboxtoggle.showControlBox).toHaveBeenCalled();
  332. }
  333. }, converse));
  334. it("will have their own heading once they have been added", $.proxy(function () {
  335. expect(this.rosterview.$el.find('dt#xmpp-contact-requests').css('display')).toEqual('block');
  336. }, converse));
  337. it("can have their requests accepted by the user", $.proxy(function () {
  338. // TODO: Testing can be more thorough here, the user is
  339. // actually not accepted/authorized because of
  340. // mock_connection.
  341. var jid = req_names.sort()[0].replace(/ /g,'.').toLowerCase() + '@localhost';
  342. var view = this.rosterview.rosteritemviews[jid];
  343. spyOn(this.connection.roster, 'authorize');
  344. spyOn(view, 'acceptRequest').andCallThrough();
  345. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  346. var accept_button = view.$el.find('.accept-xmpp-request');
  347. accept_button.click();
  348. expect(view.acceptRequest).toHaveBeenCalled();
  349. expect(this.connection.roster.authorize).toHaveBeenCalled();
  350. }, converse));
  351. it("can have their requests denied by the user", $.proxy(function () {
  352. var jid = req_names.sort()[1].replace(/ /g,'.').toLowerCase() + '@localhost';
  353. var view = this.rosterview.rosteritemviews[jid];
  354. spyOn(this.connection.roster, 'unauthorize');
  355. spyOn(this.rosterview, 'removeRosterItemView').andCallThrough();
  356. spyOn(view, 'declineRequest').andCallThrough();
  357. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  358. var accept_button = view.$el.find('.decline-xmpp-request');
  359. accept_button.click();
  360. expect(view.declineRequest).toHaveBeenCalled();
  361. expect(this.rosterview.removeRosterItemView).toHaveBeenCalled();
  362. expect(this.connection.roster.unauthorize).toHaveBeenCalled();
  363. // There should now be one less contact
  364. expect(this.roster.length).toEqual(num_contacts-1);
  365. }, converse));
  366. }, converse));
  367. describe("All Contacts", $.proxy(function () {
  368. it("are saved to, and can be retrieved from, localStorage", $.proxy(function () {
  369. var new_attrs, old_attrs, attrs, old_roster;
  370. var num_contacts = this.roster.length;
  371. new_roster = new this.RosterItems();
  372. // Roster items are yet to be fetched from localStorage
  373. expect(new_roster.length).toEqual(0);
  374. new_roster.localStorage = new Backbone.LocalStorage(
  375. hex_sha1('converse.rosteritems-dummy@localhost'));
  376. new_roster.fetch();
  377. expect(this.roster.length).toEqual(num_contacts);
  378. // Check that the roster items retrieved from localStorage
  379. // have the same attributes values as the original ones.
  380. attrs = ['jid', 'fullname', 'subscription', 'ask'];
  381. for (i=0; i<attrs.length; i++) {
  382. new_attrs = _.pluck(_.pluck(new_roster.models, 'attributes'), attrs[i]);
  383. old_attrs = _.pluck(_.pluck(this.roster.models, 'attributes'), attrs[i]);
  384. // Roster items in storage are not necessarily sorted,
  385. // so we have to sort them here to do a proper
  386. // comparison
  387. expect(_.isEqual(new_attrs.sort(), old_attrs.sort())).toEqual(true);
  388. }
  389. this.rosterview.render();
  390. }, converse));
  391. afterEach($.proxy(function () {
  392. // Contacts retrieved from localStorage have chat_status of
  393. // "offline".
  394. // In the next test suite, we need some online contacts, so
  395. // we make some online now
  396. for (i=0; i<5; i++) {
  397. jid = cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  398. view = this.rosterview.rosteritemviews[jid];
  399. view.model.set('chat_status', 'online');
  400. }
  401. }, converse));
  402. }, converse));
  403. }, converse));
  404. describe("The 'Add Contact' widget", $.proxy(function () {
  405. it("opens up an add form when you click on it", $.proxy(function () {
  406. var panel = this.chatboxesview.views.controlbox.contactspanel;
  407. spyOn(panel, 'toggleContactForm').andCallThrough();
  408. panel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  409. panel.$el.find('a.toggle-xmpp-contact-form').click();
  410. expect(panel.toggleContactForm).toHaveBeenCalled();
  411. // XXX: Awaiting more tests, close it again for now...
  412. panel.$el.find('a.toggle-xmpp-contact-form').click();
  413. }, converse));
  414. }, converse));
  415. describe("A Chatbox", $.proxy(function () {
  416. it("is created when you click on a roster item", $.proxy(function () {
  417. var i, $el, click, jid, view;
  418. // showControlBox was called earlier, so the controlbox is
  419. // visible, but no other chat boxes have been created.
  420. expect(this.chatboxes.length).toEqual(1);
  421. var online_contacts = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.online').find('a.open-chat');
  422. for (i=0; i<online_contacts.length; i++) {
  423. $el = $(online_contacts[i]);
  424. jid = $el.text().replace(/ /g,'.').toLowerCase() + '@localhost';
  425. view = this.rosterview.rosteritemviews[jid];
  426. spyOn(view, 'openChat').andCallThrough();
  427. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  428. $el.click();
  429. expect(view.openChat).toHaveBeenCalled();
  430. expect(this.chatboxes.length).toEqual(i+2);
  431. }
  432. }, converse));
  433. it("can be saved to, and retrieved from, localStorage", $.proxy(function () {
  434. // We instantiate a new ChatBoxes collection, which by default
  435. // will be empty.
  436. var newchatboxes = new this.ChatBoxes();
  437. expect(newchatboxes.length).toEqual(0);
  438. // The chatboxes will then be fetched from localStorage inside the
  439. // onConnected method
  440. newchatboxes.onConnected();
  441. expect(newchatboxes.length).toEqual(6);
  442. // Check that the chatboxes items retrieved from localStorage
  443. // have the same attributes values as the original ones.
  444. attrs = ['id', 'box_id', 'visible'];
  445. for (i=0; i<attrs.length; i++) {
  446. new_attrs = _.pluck(_.pluck(newchatboxes.models, 'attributes'), attrs[i]);
  447. old_attrs = _.pluck(_.pluck(this.chatboxes.models, 'attributes'), attrs[i]);
  448. expect(_.isEqual(new_attrs, old_attrs)).toEqual(true);
  449. }
  450. this.rosterview.render();
  451. }, converse));
  452. it("can be closed again by clicking a DOM element with class 'close-chatbox-button'", $.proxy(function () {
  453. var chatbox, view, $el,
  454. num_open_chats = this.chatboxes.length;
  455. for (i=0; i<num_open_chats; i++) {
  456. chatbox = this.chatboxes.models[0];
  457. view = this.chatboxesview.views[chatbox.get('id')];
  458. spyOn(view, 'closeChat').andCallThrough();
  459. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  460. view.$el.find('.close-chatbox-button').click();
  461. expect(view.closeChat).toHaveBeenCalled();
  462. }
  463. }, converse));
  464. it("will be removed from localStorage when closed", $.proxy(function () {
  465. var newchatboxes = new this.ChatBoxes();
  466. expect(newchatboxes.length).toEqual(0);
  467. // onConnected will fetch chatboxes in localStorage, but
  468. // because there aren't any open chatboxes, there won't be any
  469. // in localStorage either.
  470. newchatboxes.onConnected();
  471. expect(newchatboxes.length).toEqual(0);
  472. // Lets open the controlbox again, purely for visual feedback
  473. open_controlbox();
  474. }, converse));
  475. describe("A Chat Message", $.proxy(function () {
  476. it("can be received which will open a chatbox and be displayed inside it", $.proxy(function () {
  477. var message = 'This is a received message';
  478. var sender_jid = cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
  479. msg = $msg({
  480. from: sender_jid,
  481. to: this.connection.jid,
  482. type: 'chat',
  483. id: (new Date()).getTime()
  484. }).c('body').t(message).up()
  485. .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
  486. // We don't already have an open chatbox for this user
  487. expect(this.chatboxes.get(sender_jid)).not.toBeDefined();
  488. runs($.proxy(function () {
  489. // messageReceived is a handler for received XMPP
  490. // messages
  491. this.chatboxes.messageReceived(msg);
  492. }, converse));
  493. waits(500);
  494. runs($.proxy(function () {
  495. // Check that the chatbox and its view now exist
  496. var chatbox = this.chatboxes.get(sender_jid);
  497. var chatboxview = this.chatboxesview.views[sender_jid];
  498. expect(chatbox).toBeDefined();
  499. expect(chatboxview).toBeDefined();
  500. // Check that the message was received and check the
  501. // message parameters
  502. expect(chatbox.messages.length).toEqual(1);
  503. var msg_obj = chatbox.messages.models[0];
  504. expect(msg_obj.get('message')).toEqual(message);
  505. // XXX: This is stupid, fullname is actually only the
  506. // users first name
  507. expect(msg_obj.get('fullname')).toEqual(cur_names[0].split(' ')[0]);
  508. expect(msg_obj.get('sender')).toEqual('them');
  509. expect(msg_obj.get('delayed')).toEqual(false);
  510. // Now check that the message appears inside the
  511. // chatbox in the DOM
  512. var $chat_content = chatboxview.$el.find('.chat-content');
  513. var msg_txt = $chat_content.find('.chat-message').find('.chat-message-content').text();
  514. expect(msg_txt).toEqual(message);
  515. var sender_txt = $chat_content.find('span.chat-message-them').text();
  516. expect(sender_txt.match(/^[0-9][0-9]:[0-9][0-9] /)).toBeTruthy();
  517. }, converse));
  518. }, converse));
  519. it("can be sent from a chatbox, and will appear inside it", $.proxy(function () {
  520. var contact_jid = cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
  521. var view = this.chatboxesview.views[contact_jid];
  522. var message = 'This message is sent from this chatbox';
  523. spyOn(view, 'sendMessage').andCallThrough();
  524. view.$el.find('.chat-textarea').val(message).text(message);
  525. view.$el.find('textarea.chat-textarea').trigger($.Event('keypress', {keyCode: 13}));
  526. expect(view.sendMessage).toHaveBeenCalled();
  527. expect(view.model.messages.length, 2);
  528. var txt = view.$el.find('.chat-content').find('.chat-message').last().find('.chat-message-content').text();
  529. expect(txt).toEqual(message);
  530. }, converse));
  531. }, converse));
  532. describe("Special Messages", $.proxy(function () {
  533. it("'/clear' can be used to clear messages in a conversation", $.proxy(function () {
  534. var contact_jid = cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
  535. var view = this.chatboxesview.views[contact_jid];
  536. var message = 'This message is another sent from this chatbox';
  537. // Lets make sure there is at least one message already
  538. // (e.g for when this test is run on its own).
  539. view.$el.find('.chat-textarea').val(message).text(message);
  540. view.$el.find('textarea.chat-textarea').trigger($.Event('keypress', {keyCode: 13}));
  541. expect(view.model.messages.length > 0).toBeTruthy();
  542. expect(view.model.messages.localStorage.records.length > 0).toBeTruthy();
  543. message = '/clear';
  544. var old_length = view.model.messages.length;
  545. spyOn(view, 'sendMessage').andCallThrough();
  546. view.$el.find('.chat-textarea').val(message).text(message);
  547. view.$el.find('textarea.chat-textarea').trigger($.Event('keypress', {keyCode: 13}));
  548. expect(view.sendMessage).toHaveBeenCalled();
  549. expect(view.model.messages.length, 0); // The messages must be removed from the modal
  550. expect(view.model.messages.localStorage.records.length, 0); // And also from localStorage
  551. }, converse));
  552. }, converse));
  553. }, converse));
  554. describe("A Message Counter", $.proxy(function () {
  555. beforeEach($.proxy(function () {
  556. converse.clearMsgCounter();
  557. }, converse));
  558. it("is incremented when the message is received and the window is not focused", $.proxy(function () {
  559. expect(this.msg_counter).toBe(0);
  560. spyOn(converse, 'incrementMsgCounter').andCallThrough();
  561. $(window).trigger('blur');
  562. var message = 'This message will increment the message counter';
  563. var sender_jid = cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
  564. msg = $msg({
  565. from: sender_jid,
  566. to: this.connection.jid,
  567. type: 'chat',
  568. id: (new Date()).getTime()
  569. }).c('body').t(message).up()
  570. .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
  571. this.chatboxes.messageReceived(msg);
  572. expect(converse.incrementMsgCounter).toHaveBeenCalled();
  573. expect(this.msg_counter).toBe(1);
  574. }, converse));
  575. it("is cleared when the window is focused", $.proxy(function () {
  576. spyOn(converse, 'clearMsgCounter').andCallThrough();
  577. runs(function () {
  578. $(window).trigger('focus');
  579. });
  580. waits(50);
  581. runs(function () {
  582. expect(converse.clearMsgCounter).toHaveBeenCalled();
  583. });
  584. }, converse));
  585. it("is not incremented when the message is received and the window is focused", $.proxy(function () {
  586. expect(this.msg_counter).toBe(0);
  587. spyOn(converse, 'incrementMsgCounter').andCallThrough();
  588. $(window).trigger('focus');
  589. var message = 'This message will not increment the message counter';
  590. var sender_jid = cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
  591. msg = $msg({
  592. from: sender_jid,
  593. to: this.connection.jid,
  594. type: 'chat',
  595. id: (new Date()).getTime()
  596. }).c('body').t(message).up()
  597. .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
  598. this.chatboxes.messageReceived(msg);
  599. expect(converse.incrementMsgCounter).not.toHaveBeenCalled();
  600. expect(this.msg_counter).toBe(0);
  601. }, converse));
  602. }, converse));
  603. describe("The Controlbox Tabs", $.proxy(function () {
  604. beforeEach($.proxy(function () {
  605. closeAllChatBoxes();
  606. openControlBox();
  607. }, converse));
  608. it("contains two tabs, 'Contacts' and 'ChatRooms'", $.proxy(function () {
  609. var cbview = this.chatboxesview.views.controlbox;
  610. var $panels = cbview.$el.find('.controlbox-panes');
  611. expect($panels.children().length).toBe(2);
  612. expect($panels.children().first().attr('id')).toBe('users');
  613. expect($panels.children().first().is(':visible')).toBe(true);
  614. expect($panels.children().last().attr('id')).toBe('chatrooms');
  615. expect($panels.children().last().is(':visible')).toBe(false);
  616. }, converse));
  617. describe("The Chatrooms Panel", $.proxy(function () {
  618. beforeEach($.proxy(function () {
  619. closeAllChatBoxes();
  620. openControlBox();
  621. }, converse));
  622. it("is opened by clicking the 'Chatrooms' tab", $.proxy(function () {
  623. var cbview = this.chatboxesview.views.controlbox;
  624. var $tabs = cbview.$el.find('#controlbox-tabs');
  625. var $panels = cbview.$el.find('.controlbox-panes');
  626. var $contacts = $panels.children().first();
  627. var $chatrooms = $panels.children().last();
  628. spyOn(cbview, 'switchTab').andCallThrough();
  629. cbview.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  630. runs(function () {
  631. $tabs.find('li').last().find('a').click(); // Clicks the chatrooms tab
  632. });
  633. waits(250);
  634. runs(function () {
  635. expect($contacts.is(':visible')).toBe(false);
  636. expect($chatrooms.is(':visible')).toBe(true);
  637. expect(cbview.switchTab).toHaveBeenCalled();
  638. });
  639. }, converse));
  640. it("contains a form through which a new chatroom can be created", $.proxy(function () {
  641. var roomspanel = this.chatboxesview.views.controlbox.roomspanel;
  642. var $input = roomspanel.$el.find('input.new-chatroom-name');
  643. var $nick = roomspanel.$el.find('input.new-chatroom-nick');
  644. var $server = roomspanel.$el.find('input.new-chatroom-server');
  645. expect($input.length).toBe(1);
  646. expect($server.length).toBe(1);
  647. expect($('.chatroom:visible').length).toBe(0); // There shouldn't be any chatrooms open currently
  648. spyOn(roomspanel, 'createChatRoom').andCallThrough();
  649. roomspanel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  650. runs(function () {
  651. $input.val('Lounge');
  652. $nick.val('dummy');
  653. $server.val('muc.localhost');
  654. });
  655. waits('250');
  656. runs(function () {
  657. roomspanel.$el.find('form').submit();
  658. expect(roomspanel.createChatRoom).toHaveBeenCalled();
  659. });
  660. waits('250');
  661. runs($.proxy(function () {
  662. expect($('.chatroom:visible').length).toBe(1); // There should now be an open chatroom
  663. }, converse));
  664. }, converse));
  665. }, converse));
  666. }, converse));
  667. });
  668. }));