MainSpec.js 40 KB

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