MainSpec.js 35 KB

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