controlbox.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. (function (root, factory) {
  2. define([
  3. "mock",
  4. "utils"
  5. ], function (mock, utils) {
  6. return factory(mock, utils);
  7. }
  8. );
  9. } (this, function (mock, utils) {
  10. var checkHeaderToggling = function ($header) {
  11. var $toggle = $header.find('a.group-toggle');
  12. expect($header.css('display')).toEqual('block');
  13. spyOn(this.rosterview, 'toggleGroup').andCallThrough();
  14. expect($header.nextUntil('dt', 'dd').length === $header.nextUntil('dt', 'dd:visible').length).toBeTruthy();
  15. this.rosterview.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  16. expect($toggle.hasClass('icon-closed')).toBeFalsy();
  17. expect($toggle.hasClass('icon-opened')).toBeTruthy();
  18. $toggle.click();
  19. expect(this.rosterview.toggleGroup).toHaveBeenCalled();
  20. expect($toggle.hasClass('icon-closed')).toBeTruthy();
  21. expect($toggle.hasClass('icon-opened')).toBeFalsy();
  22. expect($header.nextUntil('dt', 'dd').length === $header.nextUntil('dt', 'dd:hidden').length).toBeTruthy();
  23. $toggle.click();
  24. expect(this.rosterview.toggleGroup).toHaveBeenCalled();
  25. expect($toggle.hasClass('icon-closed')).toBeFalsy();
  26. expect($toggle.hasClass('icon-opened')).toBeTruthy();
  27. expect($header.nextUntil('dt', 'dd').length === $header.nextUntil('dt', 'dd:visible').length).toBeTruthy();
  28. };
  29. describe("The Control Box", $.proxy(function (mock, utils) {
  30. beforeEach(function () {
  31. runs(function () {
  32. utils.openControlBox();
  33. });
  34. });
  35. it("can be opened by clicking a DOM element with class 'toggle-controlbox'", $.proxy(function () {
  36. runs(function () {
  37. utils.closeControlBox();
  38. });
  39. waits(50);
  40. runs(function () {
  41. // This spec will only pass if the controlbox is not currently
  42. // open yet.
  43. expect($("div#controlbox").is(':visible')).toBe(false);
  44. spyOn(this.controlboxtoggle, 'onClick').andCallThrough();
  45. spyOn(this.controlboxtoggle, 'showControlBox').andCallThrough();
  46. spyOn(converse, 'emit');
  47. // Redelegate so that the spies are now registered as the event handlers (specifically for 'onClick')
  48. this.controlboxtoggle.delegateEvents();
  49. $('.toggle-controlbox').click();
  50. }.bind(converse));
  51. waits(50);
  52. runs(function () {
  53. expect(this.controlboxtoggle.onClick).toHaveBeenCalled();
  54. expect(this.controlboxtoggle.showControlBox).toHaveBeenCalled();
  55. expect(this.emit).toHaveBeenCalledWith('controlBoxOpened', jasmine.any(Object));
  56. expect($("div#controlbox").is(':visible')).toBe(true);
  57. }.bind(converse));
  58. }, converse));
  59. describe("The Status Widget", $.proxy(function () {
  60. beforeEach(function () {
  61. utils.openControlBox();
  62. });
  63. it("shows the user's chat status, which is online by default", $.proxy(function () {
  64. var view = this.xmppstatusview;
  65. expect(view.$el.find('a.choose-xmpp-status').hasClass('online')).toBe(true);
  66. expect(view.$el.find('a.choose-xmpp-status').attr('data-value')).toBe('I am online');
  67. }, converse));
  68. it("can be used to set the current user's chat status", $.proxy(function () {
  69. var view = this.xmppstatusview;
  70. spyOn(view, 'toggleOptions').andCallThrough();
  71. spyOn(view, 'setStatus').andCallThrough();
  72. spyOn(converse, 'emit');
  73. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  74. runs(function () {
  75. view.$el.find('a.choose-xmpp-status').click();
  76. expect(view.toggleOptions).toHaveBeenCalled();
  77. });
  78. waits(250);
  79. runs(function () {
  80. spyOn(view, 'updateStatusUI').andCallThrough();
  81. view.initialize(); // Rebind events for spy
  82. $(view.$el.find('.dropdown dd ul li a')[1]).click(); // Change status to "dnd"
  83. expect(view.setStatus).toHaveBeenCalled();
  84. expect(converse.emit).toHaveBeenCalledWith('statusChanged', 'dnd');
  85. });
  86. waits(250);
  87. runs($.proxy(function () {
  88. expect(view.updateStatusUI).toHaveBeenCalled();
  89. expect(view.$el.find('a.choose-xmpp-status').hasClass('online')).toBe(false);
  90. expect(view.$el.find('a.choose-xmpp-status').hasClass('dnd')).toBe(true);
  91. expect(view.$el.find('a.choose-xmpp-status').attr('data-value')).toBe('I am busy');
  92. }, converse));
  93. }, converse));
  94. it("can be used to set a custom status message", $.proxy(function () {
  95. var view = this.xmppstatusview;
  96. this.xmppstatus.save({'status': 'online'});
  97. spyOn(view, 'setStatusMessage').andCallThrough();
  98. spyOn(view, 'renderStatusChangeForm').andCallThrough();
  99. spyOn(converse, 'emit');
  100. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  101. view.$el.find('a.change-xmpp-status-message').click();
  102. expect(view.renderStatusChangeForm).toHaveBeenCalled();
  103. // The async testing here is used only to provide time for
  104. // visual feedback
  105. var msg = 'I am happy';
  106. runs (function () {
  107. view.$el.find('form input.custom-xmpp-status').val(msg);
  108. });
  109. waits(250);
  110. runs (function () {
  111. view.$el.find('form#set-custom-xmpp-status').submit();
  112. expect(view.setStatusMessage).toHaveBeenCalled();
  113. expect(converse.emit).toHaveBeenCalledWith('statusMessageChanged', msg);
  114. expect(view.$el.find('a.choose-xmpp-status').hasClass('online')).toBe(true);
  115. expect(view.$el.find('a.choose-xmpp-status').attr('data-value')).toBe(msg);
  116. });
  117. }, converse));
  118. }, converse));
  119. }, converse, mock, utils));
  120. describe("The Contacts Roster", $.proxy(function (mock, utils) {
  121. describe("Pending Contacts", $.proxy(function () {
  122. function _clearContacts () {
  123. utils.clearBrowserStorage();
  124. converse.rosterview.model.reset();
  125. converse.rosterview.initialize(); // Register new listeners and document fragment
  126. };
  127. function _addContacts () {
  128. _clearContacts();
  129. // Must be initialized, so that render is called and documentFragment set up.
  130. utils.createContacts('pending').openControlBox();
  131. utils.openContactsPanel();
  132. };
  133. it("do not have a header if there aren't any", $.proxy(function () {
  134. _addContacts();
  135. this.rosterview.model.reset();
  136. expect(this.rosterview.$el.find('dt#pending-xmpp-contacts').css('display')).toEqual('none');
  137. }, converse));
  138. it("can be collapsed under their own header", $.proxy(function () {
  139. _addContacts();
  140. checkHeaderToggling.apply(this, [this.rosterview.$el.find('dt#pending-xmpp-contacts')]);
  141. }, converse));
  142. it("can be added to the roster", $.proxy(function () {
  143. _clearContacts();
  144. spyOn(converse, 'emit');
  145. spyOn(this.rosterview, 'updateRoster').andCallThrough();
  146. runs($.proxy(function () {
  147. this.roster.create({
  148. jid: mock.pend_names[0].replace(/ /g,'.').toLowerCase() + '@localhost',
  149. subscription: 'none',
  150. ask: 'subscribe',
  151. fullname: mock.pend_names[0],
  152. is_last: true
  153. });
  154. }, converse));
  155. waits(300);
  156. runs($.proxy(function () {
  157. expect(converse.emit).toHaveBeenCalledWith('rosterViewUpdated');
  158. expect(this.rosterview.$el.is(':visible')).toEqual(true);
  159. expect(this.rosterview.updateRoster).toHaveBeenCalled();
  160. }, converse));
  161. }, converse));
  162. it("can be removed by the user", $.proxy(function () {
  163. _addContacts();
  164. var jid = mock.pend_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
  165. var view = this.rosterview.get(jid);
  166. spyOn(window, 'confirm').andReturn(true);
  167. spyOn(converse, 'emit');
  168. spyOn(this.connection.roster, 'remove').andCallThrough();
  169. spyOn(this.connection.roster, 'unauthorize');
  170. spyOn(this.rosterview.model, 'remove').andCallThrough();
  171. view.$el.find('.remove-xmpp-contact').click();
  172. expect(window.confirm).toHaveBeenCalled();
  173. expect(this.connection.roster.remove).toHaveBeenCalled();
  174. expect(this.connection.roster.unauthorize).toHaveBeenCalled();
  175. expect(this.rosterview.model.remove).toHaveBeenCalled();
  176. // The element must now be detached from the DOM.
  177. expect(view.$el.closest('html').length).toBeFalsy();
  178. expect(converse.emit).toHaveBeenCalledWith('rosterViewUpdated');
  179. }, converse));
  180. it("will lose their own header once the last one has been removed", $.proxy(function () {
  181. _addContacts();
  182. var view;
  183. spyOn(window, 'confirm').andReturn(true);
  184. for (i=0; i<mock.pend_names.length; i++) {
  185. view = this.rosterview.get(mock.pend_names[i].replace(/ /g,'.').toLowerCase() + '@localhost');
  186. view.$el.find('.remove-xmpp-contact').click();
  187. }
  188. expect(this.rosterview.$el.find('dt#pending-xmpp-contacts').is(':visible')).toBeFalsy();
  189. }, converse));
  190. it("can be added to the roster and they will be sorted alphabetically", $.proxy(function () {
  191. _clearContacts();
  192. var i, t, is_last;
  193. spyOn(converse, 'emit');
  194. spyOn(this.rosterview, 'updateRoster').andCallThrough();
  195. for (i=0; i<mock.pend_names.length; i++) {
  196. is_last = i===(mock.pend_names.length-1);
  197. this.roster.create({
  198. jid: mock.pend_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  199. subscription: 'none',
  200. ask: 'subscribe',
  201. fullname: mock.pend_names[i],
  202. is_last: is_last
  203. });
  204. expect(this.rosterview.updateRoster).toHaveBeenCalled();
  205. expect(converse.emit).toHaveBeenCalledWith('rosterViewUpdated');
  206. }
  207. // Check that they are sorted alphabetically
  208. t = this.rosterview.$el.find('dt#pending-xmpp-contacts').siblings('dd.pending-xmpp-contact').find('span').text();
  209. expect(t).toEqual(mock.pend_names.slice(0,i+1).sort().join(''));
  210. }, converse));
  211. }, converse));
  212. describe("Existing Contacts", $.proxy(function () {
  213. beforeEach($.proxy(function () {
  214. runs(function () {
  215. converse.rosterview.model.reset();
  216. utils.createContacts().openControlBox();
  217. });
  218. waits(50);
  219. runs(function () {
  220. utils.openContactsPanel();
  221. });
  222. }, converse));
  223. it("do not have a header if there aren't any", $.proxy(function () {
  224. converse.rosterview.model.reset();
  225. expect(this.rosterview.$el.find('dt.roster-group').css('display')).toEqual('none');
  226. }, converse));
  227. it("can be collapsed under their own header", $.proxy(function () {
  228. checkHeaderToggling.apply(this, [this.rosterview.$el.find('dt.roster-group')]);
  229. }, converse));
  230. it("can be added to the roster and they will be sorted alphabetically", $.proxy(function () {
  231. var i, t;
  232. converse.rosterview.model.reset();
  233. spyOn(converse, 'emit');
  234. spyOn(this.rosterview, 'updateRoster').andCallThrough();
  235. for (i=0; i<mock.cur_names.length; i++) {
  236. this.roster.create({
  237. jid: mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  238. subscription: 'both',
  239. ask: null,
  240. fullname: mock.cur_names[i],
  241. is_last: i===(mock.cur_names.length-1)
  242. });
  243. expect(this.rosterview.updateRoster).toHaveBeenCalled();
  244. expect(converse.emit).toHaveBeenCalledWith('rosterViewUpdated');
  245. }
  246. // Check that they are sorted alphabetically
  247. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.offline').find('a.open-chat').text();
  248. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  249. }, converse));
  250. it("can be assigned to groups inside the roster", $.proxy(function () {
  251. var i=0, j=0, t;
  252. converse.rosterview.model.reset();
  253. spyOn(converse, 'emit');
  254. spyOn(this.rosterview, 'updateRoster').andCallThrough();
  255. converse.roster_groups = true;
  256. converse.rosterview.render();
  257. var groups = {
  258. colleagues: 3,
  259. friends: 8,
  260. family: 4
  261. };
  262. _.each(_.keys(groups), $.proxy(function (name) {
  263. j = i;
  264. for (i=j; i<j+groups[name]; i++) {
  265. this.roster.create({
  266. jid: mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  267. subscription: 'both',
  268. ask: null,
  269. groups: [name],
  270. fullname: mock.cur_names[i],
  271. is_last: i===(mock.cur_names.length-1)
  272. });
  273. }
  274. }, converse));
  275. }, converse));
  276. it("can change their status to online and be sorted alphabetically", $.proxy(function () {
  277. var item, view, jid, t;
  278. spyOn(converse, 'emit');
  279. spyOn(this.rosterview, 'updateRoster').andCallThrough();
  280. for (i=0; i<mock.cur_names.length; i++) {
  281. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  282. view = this.rosterview.get(jid);
  283. spyOn(view, 'render').andCallThrough();
  284. item = view.model;
  285. item.set('chat_status', 'online');
  286. expect(view.render).toHaveBeenCalled();
  287. expect(this.rosterview.updateRoster).toHaveBeenCalled();
  288. expect(converse.emit).toHaveBeenCalledWith('rosterViewUpdated');
  289. // Check that they are sorted alphabetically
  290. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.online').find('a.open-chat').text();
  291. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  292. }
  293. }, converse));
  294. it("can change their status to busy and be sorted alphabetically", $.proxy(function () {
  295. var item, view, jid, t;
  296. spyOn(converse, 'emit');
  297. spyOn(this.rosterview, 'updateRoster').andCallThrough();
  298. for (i=0; i<mock.cur_names.length; i++) {
  299. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  300. view = this.rosterview.get(jid);
  301. spyOn(view, 'render').andCallThrough();
  302. item = view.model;
  303. item.set('chat_status', 'dnd');
  304. expect(view.render).toHaveBeenCalled();
  305. expect(this.rosterview.updateRoster).toHaveBeenCalled();
  306. expect(converse.emit).toHaveBeenCalledWith('rosterViewUpdated');
  307. // Check that they are sorted alphabetically
  308. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.dnd').find('a.open-chat').text();
  309. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  310. }
  311. }, converse));
  312. it("can change their status to away and be sorted alphabetically", $.proxy(function () {
  313. var item, view, jid, t;
  314. spyOn(converse, 'emit');
  315. spyOn(this.rosterview, 'updateRoster').andCallThrough();
  316. for (i=0; i<mock.cur_names.length; i++) {
  317. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  318. view = this.rosterview.get(jid);
  319. spyOn(view, 'render').andCallThrough();
  320. item = view.model;
  321. item.set('chat_status', 'away');
  322. expect(view.render).toHaveBeenCalled();
  323. expect(this.rosterview.updateRoster).toHaveBeenCalled();
  324. expect(converse.emit).toHaveBeenCalledWith('rosterViewUpdated');
  325. // Check that they are sorted alphabetically
  326. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.away').find('a.open-chat').text();
  327. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  328. }
  329. }, converse));
  330. it("can change their status to xa and be sorted alphabetically", $.proxy(function () {
  331. var item, view, jid, t;
  332. spyOn(converse, 'emit');
  333. spyOn(this.rosterview, 'updateRoster').andCallThrough();
  334. for (i=0; i<mock.cur_names.length; i++) {
  335. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  336. view = this.rosterview.get(jid);
  337. spyOn(view, 'render').andCallThrough();
  338. item = view.model;
  339. item.set('chat_status', 'xa');
  340. expect(view.render).toHaveBeenCalled();
  341. expect(this.rosterview.updateRoster).toHaveBeenCalled();
  342. expect(converse.emit).toHaveBeenCalledWith('rosterViewUpdated');
  343. // Check that they are sorted alphabetically
  344. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.xa').find('a.open-chat').text();
  345. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  346. }
  347. }, converse));
  348. it("can change their status to unavailable and be sorted alphabetically", $.proxy(function () {
  349. var item, view, jid, t;
  350. spyOn(converse, 'emit');
  351. spyOn(this.rosterview, 'updateRoster').andCallThrough();
  352. for (i=0; i<mock.cur_names.length; i++) {
  353. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  354. view = this.rosterview.get(jid);
  355. spyOn(view, 'render').andCallThrough();
  356. item = view.model;
  357. item.set('chat_status', 'unavailable');
  358. expect(view.render).toHaveBeenCalled();
  359. expect(this.rosterview.updateRoster).toHaveBeenCalled();
  360. expect(converse.emit).toHaveBeenCalledWith('rosterViewUpdated');
  361. // Check that they are sorted alphabetically
  362. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.unavailable').find('a.open-chat').text();
  363. expect(t).toEqual(mock.cur_names.slice(0, i+1).sort().join(''));
  364. }
  365. }, converse));
  366. it("are ordered according to status: online, busy, away, xa, unavailable, offline", $.proxy(function () {
  367. var i;
  368. for (i=0; i<3; i++) {
  369. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  370. view = this.rosterview.get(jid);
  371. view.model.set('chat_status', 'online');
  372. }
  373. for (i=3; i<6; i++) {
  374. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  375. view = this.rosterview.get(jid);
  376. view.model.set('chat_status', 'dnd');
  377. }
  378. for (i=6; i<9; i++) {
  379. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  380. view = this.rosterview.get(jid);
  381. view.model.set('chat_status', 'away');
  382. }
  383. for (i=9; i<12; i++) {
  384. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  385. view = this.rosterview.get(jid);
  386. view.model.set('chat_status', 'xa');
  387. }
  388. for (i=12; i<15; i++) {
  389. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  390. view = this.rosterview.get(jid);
  391. view.model.set('chat_status', 'unavailable');
  392. }
  393. var contacts = this.rosterview.$el.find('dd.current-xmpp-contact');
  394. for (i=0; i<3; i++) {
  395. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('online');
  396. }
  397. for (i=3; i<6; i++) {
  398. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('dnd');
  399. }
  400. for (i=6; i<9; i++) {
  401. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('away');
  402. }
  403. for (i=9; i<12; i++) {
  404. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('xa');
  405. }
  406. for (i=12; i<15; i++) {
  407. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('unavailable');
  408. }
  409. for (i=15; i<mock.cur_names.length; i++) {
  410. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('offline');
  411. }
  412. }, converse));
  413. }, converse));
  414. describe("Requesting Contacts", $.proxy(function () {
  415. beforeEach($.proxy(function () {
  416. runs(function () {
  417. utils.clearBrowserStorage();
  418. converse.rosterview.model.reset();
  419. utils.createContacts('requesting').openControlBox();
  420. });
  421. waits(50);
  422. runs(function () {
  423. utils.openContactsPanel();
  424. });
  425. }, converse));
  426. it("do not have a header if there aren't any", $.proxy(function () {
  427. // by default the dts are hidden from css class and only later they will be hidden
  428. // by jQuery therefore for the first check we will see if visible instead of none
  429. converse.rosterview.model.reset();
  430. expect(this.rosterview.$el.find('dt#xmpp-contact-requests').is(':visible')).toEqual(false);
  431. }, converse));
  432. it("can be added to the roster and they will be sorted alphabetically", $.proxy(function () {
  433. converse.rosterview.model.reset(); // We want to manually create users so that we can spy
  434. var i, children;
  435. var names = [];
  436. spyOn(converse, 'emit');
  437. spyOn(this.rosterview, 'updateRoster').andCallThrough();
  438. spyOn(this.controlboxtoggle, 'showControlBox').andCallThrough();
  439. var addName = function (idx, item) {
  440. if (!$(item).hasClass('request-actions')) {
  441. names.push($(item).text().replace(/^\s+|\s+$/g, ''));
  442. }
  443. };
  444. for (i=0; i<mock.req_names.length; i++) {
  445. this.roster.create({
  446. jid: mock.req_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  447. subscription: 'none',
  448. ask: null,
  449. requesting: true,
  450. fullname: mock.req_names[i],
  451. is_last: i===(mock.req_names.length-1)
  452. });
  453. expect(this.rosterview.updateRoster).toHaveBeenCalled();
  454. // When a requesting contact is added, the controlbox must
  455. // be opened.
  456. expect(this.controlboxtoggle.showControlBox).toHaveBeenCalled();
  457. expect(converse.emit).toHaveBeenCalledWith('rosterViewUpdated');
  458. }
  459. // Check that they are sorted alphabetically
  460. children = this.rosterview.$el.find('dt#xmpp-contact-requests').siblings('dd.requesting-xmpp-contact').children('span');
  461. names = [];
  462. children.each(addName);
  463. expect(names.join('')).toEqual(mock.req_names.slice(0,i+1).sort().join(''));
  464. }, converse));
  465. it("can be collapsed under their own header", $.proxy(function () {
  466. checkHeaderToggling.apply(this, [this.rosterview.$el.find('dt#xmpp-contact-requests')]);
  467. }, converse));
  468. it("can have their requests accepted by the user", $.proxy(function () {
  469. // TODO: Testing can be more thorough here, the user is
  470. // actually not accepted/authorized because of
  471. // mock_connection.
  472. var jid = mock.req_names.sort()[0].replace(/ /g,'.').toLowerCase() + '@localhost';
  473. var view = this.rosterview.get(jid);
  474. spyOn(this.connection.roster, 'authorize');
  475. spyOn(view, 'acceptRequest').andCallThrough();
  476. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  477. var accept_button = view.$el.find('.accept-xmpp-request');
  478. accept_button.click();
  479. expect(view.acceptRequest).toHaveBeenCalled();
  480. expect(this.connection.roster.authorize).toHaveBeenCalled();
  481. }, converse));
  482. it("can have their requests denied by the user", $.proxy(function () {
  483. this.rosterview.model.reset();
  484. spyOn(converse, 'emit');
  485. spyOn(this.connection.roster, 'unauthorize');
  486. spyOn(this.rosterview, 'removeRosterItemView').andCallThrough();
  487. spyOn(window, 'confirm').andReturn(true);
  488. this.rosterview.initialize(); // Must be initialized only after the spy has been called
  489. utils.createContacts('requesting').openControlBox();
  490. var jid = mock.req_names.sort()[1].replace(/ /g,'.').toLowerCase() + '@localhost';
  491. var view = this.rosterview.get(jid);
  492. spyOn(view, 'declineRequest').andCallThrough();
  493. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  494. var accept_button = view.$el.find('.decline-xmpp-request');
  495. accept_button.click();
  496. expect(view.declineRequest).toHaveBeenCalled();
  497. expect(window.confirm).toHaveBeenCalled();
  498. expect(this.rosterview.removeRosterItemView).toHaveBeenCalled();
  499. expect(this.connection.roster.unauthorize).toHaveBeenCalled();
  500. expect(converse.emit).toHaveBeenCalledWith('rosterViewUpdated');
  501. // There should now be one less contact
  502. expect(this.roster.length).toEqual(mock.req_names.length-1);
  503. }, converse));
  504. }, converse));
  505. describe("All Contacts", $.proxy(function () {
  506. beforeEach($.proxy(function () {
  507. runs(function () {
  508. utils.clearBrowserStorage();
  509. converse.rosterview.model.reset();
  510. converse.rosterview.model.browserStorage._clear();
  511. utils.createContacts('all').openControlBox();
  512. });
  513. waits(50);
  514. runs(function () {
  515. utils.openContactsPanel();
  516. });
  517. }, converse));
  518. it("are saved to, and can be retrieved from, browserStorage", $.proxy(function () {
  519. var new_attrs, old_attrs, attrs, old_roster;
  520. var num_contacts = this.roster.length;
  521. new_roster = new this.RosterItems();
  522. // Roster items are yet to be fetched from browserStorage
  523. expect(new_roster.length).toEqual(0);
  524. new_roster.browserStorage = new Backbone.BrowserStorage.session(
  525. b64_sha1('converse.rosteritems-dummy@localhost'));
  526. new_roster.fetch();
  527. expect(new_roster.length).toEqual(num_contacts);
  528. // Check that the roster items retrieved from browserStorage
  529. // have the same attributes values as the original ones.
  530. attrs = ['jid', 'fullname', 'subscription', 'ask'];
  531. for (i=0; i<attrs.length; i++) {
  532. new_attrs = _.pluck(_.pluck(new_roster.models, 'attributes'), attrs[i]);
  533. old_attrs = _.pluck(_.pluck(this.roster.models, 'attributes'), attrs[i]);
  534. // Roster items in storage are not necessarily sorted,
  535. // so we have to sort them here to do a proper
  536. // comparison
  537. expect(_.isEqual(new_attrs.sort(), old_attrs.sort())).toEqual(true);
  538. }
  539. this.rosterview.updateRoster();
  540. }, converse));
  541. afterEach($.proxy(function () {
  542. // Contacts retrieved from browserStorage have chat_status of
  543. // "offline".
  544. // In the next test suite, we need some online contacts, so
  545. // we make some online now
  546. for (i=0; i<5; i++) {
  547. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  548. view = this.rosterview.get(jid);
  549. view.model.set('chat_status', 'online');
  550. }
  551. }, converse));
  552. }, converse));
  553. }, converse, mock, utils));
  554. describe("The 'Add Contact' widget", $.proxy(function (mock, utils) {
  555. it("opens up an add form when you click on it", $.proxy(function () {
  556. var panel = this.chatboxviews.get('controlbox').contactspanel;
  557. spyOn(panel, 'toggleContactForm').andCallThrough();
  558. panel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  559. panel.$el.find('a.toggle-xmpp-contact-form').click();
  560. expect(panel.toggleContactForm).toHaveBeenCalled();
  561. // XXX: Awaiting more tests, close it again for now...
  562. panel.$el.find('a.toggle-xmpp-contact-form').click();
  563. }, converse));
  564. }, converse, mock, utils));
  565. describe("The Controlbox Tabs", $.proxy(function () {
  566. beforeEach($.proxy(function () {
  567. runs(function () {
  568. utils.closeAllChatBoxes();
  569. });
  570. waits(50);
  571. runs(function () {
  572. utils.openControlBox();
  573. });
  574. }, converse));
  575. it("contains two tabs, 'Contacts' and 'ChatRooms'", $.proxy(function () {
  576. var cbview = this.chatboxviews.get('controlbox');
  577. var $panels = cbview.$el.find('.controlbox-panes');
  578. expect($panels.children().length).toBe(2);
  579. expect($panels.children().first().attr('id')).toBe('users');
  580. expect($panels.children().first().is(':visible')).toBe(true);
  581. expect($panels.children().last().attr('id')).toBe('chatrooms');
  582. expect($panels.children().last().is(':visible')).toBe(false);
  583. }, converse));
  584. describe("chatrooms panel", $.proxy(function () {
  585. beforeEach($.proxy(function () {
  586. runs(function () {
  587. utils.closeAllChatBoxes();
  588. });
  589. waits(50);
  590. runs(function () {
  591. utils.openControlBox();
  592. });
  593. }, converse));
  594. it("is opened by clicking the 'Chatrooms' tab", $.proxy(function () {
  595. var cbview = this.chatboxviews.get('controlbox');
  596. var $tabs = cbview.$el.find('#controlbox-tabs');
  597. var $panels = cbview.$el.find('.controlbox-panes');
  598. var $contacts = $panels.children().first();
  599. var $chatrooms = $panels.children().last();
  600. spyOn(cbview, 'switchTab').andCallThrough();
  601. cbview.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  602. runs(function () {
  603. $tabs.find('li').last().find('a').click(); // Clicks the chatrooms tab
  604. });
  605. waits(250);
  606. runs(function () {
  607. expect($contacts.is(':visible')).toBe(false);
  608. expect($chatrooms.is(':visible')).toBe(true);
  609. expect(cbview.switchTab).toHaveBeenCalled();
  610. });
  611. }, converse));
  612. it("contains a form through which a new chatroom can be created", $.proxy(function () {
  613. var roomspanel = this.chatboxviews.get('controlbox').roomspanel;
  614. var $input = roomspanel.$el.find('input.new-chatroom-name');
  615. var $nick = roomspanel.$el.find('input.new-chatroom-nick');
  616. var $server = roomspanel.$el.find('input.new-chatroom-server');
  617. expect($input.length).toBe(1);
  618. expect($server.length).toBe(1);
  619. expect($('.chatroom:visible').length).toBe(0); // There shouldn't be any chatrooms open currently
  620. spyOn(roomspanel, 'createChatRoom').andCallThrough();
  621. roomspanel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  622. runs(function () {
  623. $input.val('Lounge');
  624. $nick.val('dummy');
  625. $server.val('muc.localhost');
  626. });
  627. waits('250');
  628. runs(function () {
  629. roomspanel.$el.find('form').submit();
  630. expect(roomspanel.createChatRoom).toHaveBeenCalled();
  631. });
  632. waits('250');
  633. runs($.proxy(function () {
  634. expect($('.chatroom:visible').length).toBe(1); // There should now be an open chatroom
  635. }, converse));
  636. }, converse));
  637. }, converse));
  638. }, converse, mock, utils));
  639. }));