MainSpec.js 33 KB

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