MainSpec.js 33 KB

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