controlbox.js 39 KB

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