MainSpec.js 35 KB

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