MainSpec.js 41 KB

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