controlbox.js 39 KB

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