controlbox.js 35 KB

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