MainSpec.js 33 KB

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