MainSpec.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. (function (root, factory) {
  2. define([
  3. "converse"
  4. ], function (xmppchat) {
  5. return factory(xmppchat);
  6. }
  7. );
  8. } (this, function (xmppchat) {
  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. },
  28. 'jid': this.bare_jid,
  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.removeItem(
  54. hex_sha1('converse.rosteritems-'+this.bare_jid));
  55. window.localStorage.removeItem(
  56. hex_sha1('converse.chatboxes-'+this.bare_jid));
  57. window.localStorage.removeItem(
  58. hex_sha1('converse.xmppstatus-'+this.bare_jid));
  59. window.localStorage.removeItem(
  60. hex_sha1('converse.messages'+cur_names[0].replace(' ','.').toLowerCase() + '@localhost'));
  61. this.prebind = true;
  62. this.onConnected(mock_connection);
  63. this.animate = false; // don't use animations
  64. describe("The Control Box", $.proxy(function () {
  65. it("is not shown by default", $.proxy(function () {
  66. expect(this.rosterview.$el.is(':visible')).toEqual(false);
  67. }, xmppchat));
  68. it("can be opened by clicking a DOM element with id 'toggle-online-users'", $.proxy(function () {
  69. spyOn(this, 'toggleControlBox').andCallThrough();
  70. $('#toggle-online-users').click();
  71. expect(this.toggleControlBox).toHaveBeenCalled();
  72. }, xmppchat));
  73. describe("The Status Widget", $.proxy(function () {
  74. it("can be used to set the current user's chat status", $.proxy(function () {
  75. // TODO
  76. }, xmppchat));
  77. it("can be used to set a custom status message", $.proxy(function () {
  78. // TODO
  79. }, xmppchat));
  80. }, xmppchat));
  81. }, xmppchat));
  82. describe("The Contacts Roster", $.proxy(function () {
  83. describe("Pending Contacts", $.proxy(function () {
  84. it("do not have a heading if there aren't any", $.proxy(function () {
  85. expect(this.rosterview.$el.find('dt#pending-xmpp-contacts').css('display')).toEqual('none');
  86. }, xmppchat));
  87. it("can be added to the roster and they will be sorted alphabetically", $.proxy(function () {
  88. var i, t, is_last;
  89. spyOn(this.rosterview, 'render').andCallThrough();
  90. for (i=0; i<pend_names.length; i++) {
  91. is_last = i==(pend_names.length-1);
  92. this.roster.create({
  93. jid: pend_names[i].replace(' ','.').toLowerCase() + '@localhost',
  94. subscription: 'none',
  95. ask: 'subscribe',
  96. fullname: pend_names[i],
  97. is_last: is_last
  98. });
  99. // For performance reasons, the roster should only be shown once
  100. // the last contact has been added.
  101. if (is_last) {
  102. expect(this.rosterview.$el.is(':visible')).toEqual(true);
  103. } else {
  104. expect(this.rosterview.$el.is(':visible')).toEqual(false);
  105. }
  106. expect(this.rosterview.render).toHaveBeenCalled();
  107. // Check that they are sorted alphabetically
  108. t = this.rosterview.$el.find('dt#pending-xmpp-contacts').siblings('dd.pending-xmpp-contact').text();
  109. expect(t).toEqual(pend_names.slice(0,i+1).sort().join(''));
  110. }
  111. }, xmppchat));
  112. it("will have their own heading once they have been added", $.proxy(function () {
  113. expect(this.rosterview.$el.find('dt#pending-xmpp-contacts').css('display')).toEqual('block');
  114. }, xmppchat));
  115. }, xmppchat));
  116. describe("Existing Contacts", $.proxy(function () {
  117. it("do not have a heading if there aren't any", $.proxy(function () {
  118. expect(this.rosterview.$el.find('dt#xmpp-contacts').css('display')).toEqual('none');
  119. }, xmppchat));
  120. it("can be added to the roster and they will be sorted alphabetically", $.proxy(function () {
  121. var i, t;
  122. spyOn(this.rosterview, 'render').andCallThrough();
  123. for (i=0; i<cur_names.length; i++) {
  124. this.roster.create({
  125. jid: cur_names[i].replace(' ','.').toLowerCase() + '@localhost',
  126. subscription: 'both',
  127. ask: null,
  128. fullname: cur_names[i],
  129. is_last: i==(cur_names.length-1)
  130. });
  131. expect(this.rosterview.render).toHaveBeenCalled();
  132. // Check that they are sorted alphabetically
  133. t = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.offline').find('a.open-chat').text();
  134. expect(t).toEqual(cur_names.slice(0,i+1).sort().join(''));
  135. }
  136. }, xmppchat));
  137. it("will have their own heading once they have been added", $.proxy(function () {
  138. expect(this.rosterview.$el.find('dt#xmpp-contacts').css('display')).toEqual('block');
  139. }, xmppchat));
  140. it("can change their status to online and be sorted alphabetically", $.proxy(function () {
  141. var item, view, jid, t;
  142. spyOn(this.rosterview, 'render').andCallThrough();
  143. for (i=0; i<3; i++) {
  144. jid = cur_names[i].replace(' ','.').toLowerCase() + '@localhost';
  145. view = this.rosterview.rosteritemviews[jid];
  146. spyOn(view, 'render').andCallThrough();
  147. item = view.model;
  148. item.set('chat_status', 'online');
  149. expect(view.render).toHaveBeenCalled();
  150. expect(this.rosterview.render).toHaveBeenCalled();
  151. // Check that they are sorted alphabetically
  152. t = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.online').find('a.open-chat').text();
  153. expect(t).toEqual(cur_names.slice(0,i+1).sort().join(''));
  154. }
  155. }, xmppchat));
  156. it("can change their status to busy and be sorted alphabetically", $.proxy(function () {
  157. var item, view, jid, t;
  158. spyOn(this.rosterview, 'render').andCallThrough();
  159. for (i=3; i<6; i++) {
  160. jid = cur_names[i].replace(' ','.').toLowerCase() + '@localhost';
  161. view = this.rosterview.rosteritemviews[jid];
  162. spyOn(view, 'render').andCallThrough();
  163. item = view.model;
  164. item.set('chat_status', 'dnd');
  165. expect(view.render).toHaveBeenCalled();
  166. expect(this.rosterview.render).toHaveBeenCalled();
  167. // Check that they are sorted alphabetically
  168. t = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.dnd').find('a.open-chat').text();
  169. expect(t).toEqual(cur_names.slice(3,i+1).sort().join(''));
  170. }
  171. }, xmppchat));
  172. it("can change their status to away and be sorted alphabetically", $.proxy(function () {
  173. var item, view, jid, t;
  174. spyOn(this.rosterview, 'render').andCallThrough();
  175. for (i=6; i<9; i++) {
  176. jid = cur_names[i].replace(' ','.').toLowerCase() + '@localhost';
  177. view = this.rosterview.rosteritemviews[jid];
  178. spyOn(view, 'render').andCallThrough();
  179. item = view.model;
  180. item.set('chat_status', 'away');
  181. expect(view.render).toHaveBeenCalled();
  182. expect(this.rosterview.render).toHaveBeenCalled();
  183. // Check that they are sorted alphabetically
  184. t = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.away').find('a.open-chat').text();
  185. expect(t).toEqual(cur_names.slice(6,i+1).sort().join(''));
  186. }
  187. }, xmppchat));
  188. it("can change their status to unavailable and be sorted alphabetically", $.proxy(function () {
  189. var item, view, jid, t;
  190. spyOn(this.rosterview, 'render').andCallThrough();
  191. for (i=9; i<12; i++) {
  192. jid = cur_names[i].replace(' ','.').toLowerCase() + '@localhost';
  193. view = this.rosterview.rosteritemviews[jid];
  194. spyOn(view, 'render').andCallThrough();
  195. item = view.model;
  196. item.set('chat_status', 'unavailable');
  197. expect(view.render).toHaveBeenCalled();
  198. expect(this.rosterview.render).toHaveBeenCalled();
  199. // Check that they are sorted alphabetically
  200. t = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.unavailable').find('a.open-chat').text();
  201. expect(t).toEqual(cur_names.slice(9, i+1).sort().join(''));
  202. }
  203. }, xmppchat));
  204. it("are ordered according to status: online, busy, away, unavailable, offline", $.proxy(function () {
  205. var contacts = this.rosterview.$el.find('dd.current-xmpp-contact');
  206. var i;
  207. // The first five contacts are online.
  208. for (i=0; i<3; i++) {
  209. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('online');
  210. }
  211. // The next five are busy
  212. for (i=3; i<6; i++) {
  213. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('dnd');
  214. }
  215. // The next five are away
  216. for (i=6; i<9; i++) {
  217. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('away');
  218. }
  219. // The next five are unavailable
  220. for (i=9; i<12; i++) {
  221. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('unavailable');
  222. }
  223. // The next 20 are offline
  224. for (i=12; i<cur_names.length; i++) {
  225. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('offline');
  226. }
  227. }, xmppchat));
  228. }, xmppchat));
  229. describe("Requesting Contacts", $.proxy(function () {
  230. // by default the dts are hidden from css class and only later they will be hidden
  231. // by jQuery therefore for the first check we will see if visible instead of none
  232. it("do not have a heading if there aren't any", $.proxy(function () {
  233. expect(this.rosterview.$el.find('dt#xmpp-contact-requests').is(':visible')).toEqual(false);
  234. }, xmppchat));
  235. it("can be added to the roster and they will be sorted alphabetically", $.proxy(function () {
  236. var i, t;
  237. spyOn(this.rosterview, 'render').andCallThrough();
  238. spyOn(this, 'showControlBox').andCallThrough();
  239. for (i=0; i<req_names.length; i++) {
  240. this.roster.create({
  241. jid: req_names[i].replace(' ','.').toLowerCase() + '@localhost',
  242. subscription: 'none',
  243. ask: 'request',
  244. fullname: req_names[i],
  245. is_last: i==(req_names.length-1)
  246. });
  247. expect(this.rosterview.render).toHaveBeenCalled();
  248. // Check that they are sorted alphabetically
  249. t = this.rosterview.$el.find('dt#xmpp-contact-requests').siblings('dd.requesting-xmpp-contact').text().replace(/AcceptDecline/g, '');
  250. expect(t).toEqual(req_names.slice(0,i+1).sort().join(''));
  251. // When a requesting contact is added, the controlbox must
  252. // be opened.
  253. expect(this.showControlBox).toHaveBeenCalled();
  254. }
  255. }, xmppchat));
  256. it("will have their own heading once they have been added", $.proxy(function () {
  257. expect(this.rosterview.$el.find('dt#xmpp-contact-requests').css('display')).toEqual('block');
  258. }, xmppchat));
  259. it("can have their requests accepted by the user", $.proxy(function () {
  260. // TODO: Testing can be more thorough here, the user is
  261. // actually not accepted/authorized because of
  262. // mock_connection.
  263. var jid = req_names.sort()[0].replace(' ','.').toLowerCase() + '@localhost';
  264. var view = this.rosterview.rosteritemviews[jid];
  265. spyOn(this.connection.roster, 'authorize');
  266. spyOn(view, 'acceptRequest').andCallThrough();
  267. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  268. var accept_button = view.$el.find('.accept-xmpp-request');
  269. accept_button.click();
  270. expect(view.acceptRequest).toHaveBeenCalled();
  271. expect(this.connection.roster.authorize).toHaveBeenCalled();
  272. }, xmppchat));
  273. it("can have their requests denied by the user", $.proxy(function () {
  274. var jid = req_names.sort()[1].replace(' ','.').toLowerCase() + '@localhost';
  275. var view = this.rosterview.rosteritemviews[jid];
  276. spyOn(this.connection.roster, 'unauthorize');
  277. spyOn(this.rosterview, 'removeRosterItem').andCallThrough();
  278. spyOn(view, 'declineRequest').andCallThrough();
  279. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  280. var accept_button = view.$el.find('.decline-xmpp-request');
  281. accept_button.click();
  282. expect(view.declineRequest).toHaveBeenCalled();
  283. expect(this.rosterview.removeRosterItem).toHaveBeenCalled();
  284. expect(this.connection.roster.unauthorize).toHaveBeenCalled();
  285. // There should now be one less contact
  286. expect(this.roster.length).toEqual(num_contacts-1);
  287. }, xmppchat));
  288. }, xmppchat));
  289. describe("All Contacts", $.proxy(function () {
  290. it("are saved to, and can be retrieved from, localStorage", $.proxy(function () {
  291. var new_attrs, old_attrs, attrs, old_roster;
  292. // One contact was declined, so we have 1 less contact then originally
  293. expect(this.roster.length).toEqual(num_contacts-1);
  294. new_roster = new this.RosterItems();
  295. // Roster items are yet to be fetched from localStorage
  296. expect(new_roster.length).toEqual(0);
  297. new_roster.localStorage = new Backbone.LocalStorage(
  298. hex_sha1('converse.rosteritems-dummy@localhost'));
  299. new_roster.fetch();
  300. expect(this.roster.length).toEqual(num_contacts-1);
  301. // Check that the roster items retrieved from localStorage
  302. // have the same attributes values as the original ones.
  303. attrs = ['jid', 'fullname', 'subscription', 'ask'];
  304. for (i=0; i<attrs.length; i++) {
  305. new_attrs = _.pluck(_.pluck(new_roster.models, 'attributes'), attrs[i]);
  306. old_attrs = _.pluck(_.pluck(this.roster.models, 'attributes'), attrs[i]);
  307. // Roster items in storage are not necessarily sorted,
  308. // so we have to sort them here to do a proper
  309. // comparison
  310. expect(_.isEqual(new_attrs.sort(), old_attrs.sort())).toEqual(true);
  311. }
  312. this.rosterview.render();
  313. }, xmppchat));
  314. afterEach($.proxy(function () {
  315. // Contacts retrieved from localStorage have chat_status of
  316. // "offline".
  317. // In the next test suite, we need some online contacts, so
  318. // we make some online now
  319. for (i=0; i<5; i++) {
  320. jid = cur_names[i].replace(' ','.').toLowerCase() + '@localhost';
  321. view = this.rosterview.rosteritemviews[jid];
  322. view.model.set('chat_status', 'online');
  323. }
  324. }, xmppchat));
  325. }, xmppchat));
  326. }, xmppchat));
  327. describe("A Chatbox", $.proxy(function () {
  328. it("is created when you click on a roster item", $.proxy(function () {
  329. var i, $el, click, jid, view;
  330. // showControlBox was called earlier, so the controlbox is
  331. // visible, but no other chat boxes have been created.
  332. expect(this.chatboxes.length).toEqual(1);
  333. var online_contacts = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.online').find('a.open-chat');
  334. for (i=0; i<online_contacts.length; i++) {
  335. $el = $(online_contacts[i]);
  336. jid = $el.text().replace(' ','.').toLowerCase() + '@localhost';
  337. view = this.rosterview.rosteritemviews[jid];
  338. spyOn(view, 'openChat').andCallThrough();
  339. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  340. $el.click();
  341. expect(view.openChat).toHaveBeenCalled();
  342. expect(this.chatboxes.length).toEqual(i+2);
  343. }
  344. }, xmppchat));
  345. it("can be saved to, and retrieved from, localStorage", $.proxy(function () {
  346. // We instantiate a new ChatBoxes collection, which by default
  347. // will be empty.
  348. this.newchatboxes = new this.ChatBoxes();
  349. expect(this.newchatboxes.length).toEqual(0);
  350. // The chatboxes will then be fetched from localStorage inside the
  351. // onConnected method
  352. this.newchatboxes.onConnected();
  353. expect(this.newchatboxes.length).toEqual(6);
  354. // Check that the roster items retrieved from localStorage
  355. // have the same attributes values as the original ones.
  356. attrs = ['id', 'box_id', 'visible'];
  357. for (i=0; i<attrs.length; i++) {
  358. new_attrs = _.pluck(_.pluck(this.newchatboxes.models, 'attributes'), attrs[i]);
  359. old_attrs = _.pluck(_.pluck(this.chatboxes.models, 'attributes'), attrs[i]);
  360. expect(_.isEqual(new_attrs, old_attrs)).toEqual(true);
  361. }
  362. this.rosterview.render();
  363. }, xmppchat));
  364. it("can be closed again by clicking a DOM element with class 'close-chatbox-button'", $.proxy(function () {
  365. var chatbox, view, $el,
  366. num_open_chats = this.chatboxes.length;
  367. for (i=0; i<num_open_chats; i++) {
  368. chatbox = this.chatboxes.models[0];
  369. view = this.chatboxesview.views[chatbox.get('id')];
  370. spyOn(view, 'closeChat').andCallThrough();
  371. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  372. view.$el.find('.close-chatbox-button').click();
  373. expect(view.closeChat).toHaveBeenCalled();
  374. }
  375. }, xmppchat));
  376. it("will be removed from localStorage when closed", $.proxy(function () {
  377. this.newchatboxes = new this.ChatBoxes();
  378. expect(this.newchatboxes.length).toEqual(0);
  379. // onConnected will fetch chatboxes in localStorage, but
  380. // because there aren't any open chatboxes, there won't be any
  381. // in localStorage either.
  382. this.chatboxes.onConnected();
  383. expect(this.chatboxes.length).toEqual(0);
  384. }, xmppchat));
  385. describe("A Chat Message", $.proxy(function () {
  386. it("can be received which will open a chatbox and be displayed inside it", $.proxy(function () {
  387. var message = 'This is a received message';
  388. var sender_jid = cur_names[0].replace(' ','.').toLowerCase() + '@localhost';
  389. msg = $msg({
  390. from: sender_jid,
  391. to: this.bare_jid,
  392. type: 'chat',
  393. id: (new Date()).getTime()
  394. }).c('body').t(message).up()
  395. .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
  396. spyOn(this, 'getVCard').andCallThrough();
  397. // We don't already have an open chatbox for this user
  398. expect(this.chatboxes.get(sender_jid)).not.toBeDefined();
  399. runs($.proxy(function () {
  400. // messageReceived is a handler for received XMPP
  401. // messages
  402. this.chatboxes.messageReceived(msg);
  403. }, xmppchat));
  404. waits(500);
  405. runs($.proxy(function () {
  406. // Since we didn't already have an open chatbox, one
  407. // will asynchronously created inside a callback to
  408. // getVCard
  409. expect(this.getVCard).toHaveBeenCalled();
  410. // Check that the chatbox and its view now exist
  411. var chatbox = this.chatboxes.get(sender_jid);
  412. var chatboxview = this.chatboxesview.views[sender_jid];
  413. expect(chatbox).toBeDefined();
  414. expect(chatboxview).toBeDefined();
  415. // Check that the message was received and check the
  416. // message parameters
  417. expect(chatbox.messages.length).toEqual(1);
  418. var msg_obj = chatbox.messages.models[0];
  419. expect(msg_obj.get('message')).toEqual(message);
  420. // XXX: This is stupid, fullname is actually only the
  421. // users first name
  422. expect(msg_obj.get('fullname')).toEqual(cur_names[0].split(' ')[0]);
  423. expect(msg_obj.get('sender')).toEqual('them');
  424. expect(msg_obj.get('delayed')).toEqual(false);
  425. // Now check that the message appears inside the
  426. // chatbox in the DOM
  427. var txt = chatboxview.$el.find('.chat-content').find('.chat-message').find('.chat-message-content').text();
  428. expect(txt).toEqual(message);
  429. }, xmppchat));
  430. }, xmppchat));
  431. it("can be sent from a chatbox, and will appear inside it", $.proxy(function () {
  432. var contact_jid = cur_names[0].replace(' ','.').toLowerCase() + '@localhost';
  433. var view = this.chatboxesview.views[contact_jid];
  434. var message = 'This is a message sent from the chatbox';
  435. spyOn(view, 'sendMessage').andCallThrough();
  436. view.$el.find('.chat-textarea').text(message);
  437. view.$el.find('textarea.chat-textarea').trigger($.Event('keypress', {keyCode: 13}));
  438. expect(view.sendMessage).toHaveBeenCalled();
  439. expect(view.model.messages.length, 2);
  440. var txt = view.$el.find('.chat-content').find('.chat-message').last().find('.chat-message-content').text();
  441. expect(txt).toEqual(message);
  442. }, xmppchat));
  443. }, xmppchat));
  444. }, xmppchat));
  445. }, xmppchat));
  446. }));