controlbox.js 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  1. /*global converse */
  2. (function (root, factory) {
  3. define([
  4. "jquery",
  5. "underscore",
  6. "mock",
  7. "test_utils"
  8. ], function ($, _, mock, test_utils) {
  9. return factory($, _, mock, test_utils);
  10. }
  11. );
  12. } (this, function ($, _, mock, test_utils) {
  13. var $pres = converse_api.env.$pres;
  14. var $iq = converse_api.env.$iq;
  15. var checkHeaderToggling = function ($header) {
  16. var $toggle = $header.find('a.group-toggle');
  17. expect($header.css('display')).toEqual('block');
  18. expect($header.nextUntil('dt', 'dd').length === $header.nextUntil('dt', 'dd:visible').length).toBeTruthy();
  19. expect($toggle.hasClass('icon-closed')).toBeFalsy();
  20. expect($toggle.hasClass('icon-opened')).toBeTruthy();
  21. $toggle.click();
  22. expect($toggle.hasClass('icon-closed')).toBeTruthy();
  23. expect($toggle.hasClass('icon-opened')).toBeFalsy();
  24. expect($header.nextUntil('dt', 'dd').length === $header.nextUntil('dt', 'dd:hidden').length).toBeTruthy();
  25. $toggle.click();
  26. expect($toggle.hasClass('icon-closed')).toBeFalsy();
  27. expect($toggle.hasClass('icon-opened')).toBeTruthy();
  28. expect($header.nextUntil('dt', 'dd').length === $header.nextUntil('dt', 'dd:visible').length).toBeTruthy();
  29. };
  30. describe("The Control Box", $.proxy(function (mock, test_utils) {
  31. beforeEach(function () {
  32. runs(function () {
  33. test_utils.openControlBox();
  34. });
  35. });
  36. it("can be opened by clicking a DOM element with class 'toggle-controlbox'", $.proxy(function () {
  37. runs(function () {
  38. test_utils.closeControlBox();
  39. });
  40. waits(50);
  41. runs(function () {
  42. // This spec will only pass if the controlbox is not currently
  43. // open yet.
  44. expect($("div#controlbox").is(':visible')).toBe(false);
  45. spyOn(this.controlboxtoggle, 'onClick').andCallThrough();
  46. spyOn(this.controlboxtoggle, 'showControlBox').andCallThrough();
  47. spyOn(converse, 'emit');
  48. // Redelegate so that the spies are now registered as the event handlers (specifically for 'onClick')
  49. this.controlboxtoggle.delegateEvents();
  50. $('.toggle-controlbox').click();
  51. }.bind(converse));
  52. waits(50);
  53. runs(function () {
  54. expect(this.controlboxtoggle.onClick).toHaveBeenCalled();
  55. expect(this.controlboxtoggle.showControlBox).toHaveBeenCalled();
  56. expect(this.emit).toHaveBeenCalledWith('controlBoxOpened', jasmine.any(Object));
  57. expect($("div#controlbox").is(':visible')).toBe(true);
  58. }.bind(converse));
  59. }, converse));
  60. describe("The Status Widget", $.proxy(function () {
  61. beforeEach(function () {
  62. test_utils.openControlBox();
  63. });
  64. it("shows the user's chat status, which is online by default", $.proxy(function () {
  65. var view = this.xmppstatusview;
  66. expect(view.$el.find('a.choose-xmpp-status').hasClass('online')).toBe(true);
  67. expect(view.$el.find('a.choose-xmpp-status').attr('data-value')).toBe('I am online');
  68. }, converse));
  69. it("can be used to set the current user's chat status", $.proxy(function () {
  70. var view = this.xmppstatusview;
  71. spyOn(view, 'toggleOptions').andCallThrough();
  72. spyOn(view, 'setStatus').andCallThrough();
  73. spyOn(converse, 'emit');
  74. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  75. runs(function () {
  76. view.$el.find('a.choose-xmpp-status').click();
  77. expect(view.toggleOptions).toHaveBeenCalled();
  78. });
  79. waits(250);
  80. runs(function () {
  81. spyOn(view, 'updateStatusUI').andCallThrough();
  82. view.initialize(); // Rebind events for spy
  83. $(view.$el.find('.dropdown dd ul li a')[1]).click(); // Change status to "dnd"
  84. expect(view.setStatus).toHaveBeenCalled();
  85. expect(converse.emit).toHaveBeenCalledWith('statusChanged', 'dnd');
  86. });
  87. waits(250);
  88. runs($.proxy(function () {
  89. expect(view.updateStatusUI).toHaveBeenCalled();
  90. expect(view.$el.find('a.choose-xmpp-status').hasClass('online')).toBe(false);
  91. expect(view.$el.find('a.choose-xmpp-status').hasClass('dnd')).toBe(true);
  92. expect(view.$el.find('a.choose-xmpp-status').attr('data-value')).toBe('I am busy');
  93. }, converse));
  94. }, converse));
  95. it("can be used to set a custom status message", $.proxy(function () {
  96. var view = this.xmppstatusview;
  97. this.xmppstatus.save({'status': 'online'});
  98. spyOn(view, 'setStatusMessage').andCallThrough();
  99. spyOn(view, 'renderStatusChangeForm').andCallThrough();
  100. spyOn(converse, 'emit');
  101. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  102. view.$el.find('a.change-xmpp-status-message').click();
  103. expect(view.renderStatusChangeForm).toHaveBeenCalled();
  104. // The async testing here is used only to provide time for
  105. // visual feedback
  106. var msg = 'I am happy';
  107. runs (function () {
  108. view.$el.find('form input.custom-xmpp-status').val(msg);
  109. });
  110. waits(250);
  111. runs (function () {
  112. view.$el.find('form#set-custom-xmpp-status').submit();
  113. expect(view.setStatusMessage).toHaveBeenCalled();
  114. expect(converse.emit).toHaveBeenCalledWith('statusMessageChanged', msg);
  115. expect(view.$el.find('a.choose-xmpp-status').hasClass('online')).toBe(true);
  116. expect(view.$el.find('a.choose-xmpp-status').attr('data-value')).toBe(msg);
  117. });
  118. }, converse));
  119. }, converse));
  120. }, converse, mock, test_utils));
  121. describe("The Contacts Roster", $.proxy(function (mock, utils) {
  122. function _clearContacts () {
  123. utils.clearBrowserStorage();
  124. converse.rosterview.model.reset();
  125. }
  126. describe("The live filter", $.proxy(function () {
  127. beforeEach(function () {
  128. test_utils.openControlBox();
  129. });
  130. it("will only appear when roster contacts flow over the visible area", $.proxy(function () {
  131. _clearContacts();
  132. var $filter = converse.rosterview.$('.roster-filter');
  133. var names = mock.cur_names;
  134. expect($filter.length).toBe(1);
  135. expect($filter.is(':visible')).toBeFalsy();
  136. for (var i=0; i<names.length; i++) {
  137. converse.roster.create({
  138. ask: null,
  139. fullname: names[i],
  140. jid: names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  141. requesting: 'false',
  142. subscription: 'both'
  143. });
  144. converse.rosterview.update(); // XXX: Will normally called as event handler
  145. if (converse.rosterview.$roster.hasScrollBar()) {
  146. expect($filter.is(':visible')).toBeTruthy();
  147. } else {
  148. expect($filter.is(':visible')).toBeFalsy();
  149. }
  150. }
  151. }, converse));
  152. it("can be used to filter the contacts shown", function () {
  153. var $filter;
  154. var $roster;
  155. runs(function () {
  156. _clearContacts();
  157. converse.roster_groups = true;
  158. utils.createGroupedContacts();
  159. $filter = converse.rosterview.$('.roster-filter');
  160. $roster = converse.rosterview.$roster;
  161. });
  162. waits(350); // Needed, due to debounce
  163. runs(function () {
  164. expect($roster.find('dd:visible').length).toBe(15);
  165. expect($roster.find('dt:visible').length).toBe(5);
  166. $filter.val("candice");
  167. expect($roster.find('dd:visible').length).toBe(15); // because no keydown event
  168. expect($roster.find('dt:visible').length).toBe(5); // ditto
  169. $filter.trigger('keydown');
  170. });
  171. waits(350); // Needed, due to debounce
  172. runs (function () {
  173. expect($roster.find('dd:visible').length).toBe(1);
  174. expect($roster.find('dd:visible').eq(0).text().trim()).toBe('Candice van der Knijff');
  175. expect($roster.find('dt:visible').length).toBe(1);
  176. expect($roster.find('dt:visible').eq(0).text()).toBe('colleagues');
  177. $filter.val("an");
  178. $filter.trigger('keydown');
  179. });
  180. waits(350); // Needed, due to debounce
  181. runs (function () {
  182. expect($roster.find('dd:visible').length).toBe(5);
  183. expect($roster.find('dt:visible').length).toBe(4);
  184. $filter.val("xxx");
  185. $filter.trigger('keydown');
  186. });
  187. waits(350); // Needed, due to debounce
  188. runs (function () {
  189. expect($roster.find('dd:visible').length).toBe(0);
  190. expect($roster.find('dt:visible').length).toBe(0);
  191. $filter.val(""); // Check that contacts are shown again, when the filter string is cleared.
  192. $filter.trigger('keydown');
  193. });
  194. waits(350); // Needed, due to debounce
  195. runs(function () {
  196. expect($roster.find('dd:visible').length).toBe(15);
  197. expect($roster.find('dt:visible').length).toBe(5);
  198. });
  199. converse.roster_groups = false;
  200. });
  201. it("can be used to filter the groups shown", function () {
  202. var $filter;
  203. var $roster;
  204. var $type;
  205. runs(function () {
  206. converse.roster_groups = true;
  207. _clearContacts();
  208. utils.createGroupedContacts();
  209. $filter = converse.rosterview.$('.roster-filter');
  210. $roster = converse.rosterview.$roster;
  211. $type = converse.rosterview.$('.filter-type');
  212. $type.val('groups');
  213. });
  214. waits(350); // Needed, due to debounce
  215. runs(function () {
  216. expect($roster.find('dd:visible').length).toBe(15);
  217. expect($roster.find('dt:visible').length).toBe(5);
  218. $filter.val("colleagues");
  219. expect($roster.find('dd:visible').length).toBe(15); // because no keydown event
  220. expect($roster.find('dt:visible').length).toBe(5); // ditto
  221. $filter.trigger('keydown');
  222. });
  223. waits(350); // Needed, due to debounce
  224. runs (function () {
  225. expect($roster.find('dt:visible').length).toBe(1);
  226. expect($roster.find('dt:visible').eq(0).text()).toBe('colleagues');
  227. // Check that all contacts under the group are shown
  228. expect($roster.find('dt:visible').nextUntil('dt', 'dd:hidden').length).toBe(0);
  229. $filter.val("xxx");
  230. $filter.trigger('keydown');
  231. });
  232. waits(350); // Needed, due to debounce
  233. runs (function () {
  234. expect($roster.find('dt:visible').length).toBe(0);
  235. $filter.val(""); // Check that groups are shown again, when the filter string is cleared.
  236. $filter.trigger('keydown');
  237. });
  238. waits(350); // Needed, due to debounce
  239. runs(function () {
  240. expect($roster.find('dd:visible').length).toBe(15);
  241. expect($roster.find('dt:visible').length).toBe(5);
  242. });
  243. converse.roster_groups = false;
  244. });
  245. it("has a button with which its contents can be cleared", function () {
  246. converse.roster_groups = true;
  247. _clearContacts();
  248. utils.createGroupedContacts();
  249. var $filter = converse.rosterview.$('.roster-filter');
  250. runs (function () {
  251. $filter.val("xxx");
  252. $filter.trigger('keydown');
  253. expect($filter.hasClass("x")).toBeFalsy();
  254. });
  255. waits(350); // Needed, due to debounce
  256. runs (function () {
  257. expect($filter.hasClass("x")).toBeTruthy();
  258. $filter.addClass("onX").click();
  259. expect($filter.val()).toBe("");
  260. });
  261. converse.roster_groups = false;
  262. });
  263. }, converse));
  264. describe("A Roster Group", $.proxy(function () {
  265. beforeEach(function () {
  266. converse.roster_groups = true;
  267. });
  268. afterEach(function () {
  269. converse.roster_groups = false;
  270. });
  271. it("can be used to organize existing contacts", $.proxy(function () {
  272. runs($.proxy(function () {
  273. _clearContacts();
  274. spyOn(converse, 'emit');
  275. spyOn(this.rosterview, 'update').andCallThrough();
  276. converse.rosterview.render();
  277. utils.createContacts('pending');
  278. utils.createContacts('requesting');
  279. utils.createGroupedContacts();
  280. }, this));
  281. waits(50); // Needed, due to debounce
  282. runs($.proxy(function () {
  283. // Check that the groups appear alphabetically and that
  284. // requesting and pending contacts are last.
  285. var group_titles = $.map(this.rosterview.$el.find('dt'), function (o) { return $(o).text().trim(); });
  286. expect(group_titles).toEqual([
  287. "colleagues",
  288. "Family",
  289. "friends & acquaintences",
  290. "ænemies",
  291. "Ungrouped",
  292. "Contact requests",
  293. "Pending contacts"
  294. ]);
  295. // Check that usernames appear alphabetically per group
  296. _.each(_.keys(mock.groups), $.proxy(function (name) {
  297. var $contacts = this.rosterview.$('dt.roster-group[data-group="'+name+'"]').nextUntil('dt', 'dd');
  298. var names = $.map($contacts, function (o) { return $(o).text().trim(); });
  299. expect(names).toEqual(_.clone(names).sort());
  300. }, converse));
  301. }, this));
  302. }, converse));
  303. it("can share contacts with other roster groups", $.proxy(function () {
  304. var groups = ['colleagues', 'friends'];
  305. runs($.proxy(function () {
  306. _clearContacts();
  307. var i=0;
  308. spyOn(converse, 'emit');
  309. spyOn(this.rosterview, 'update').andCallThrough();
  310. converse.rosterview.render();
  311. for (i=0; i<mock.cur_names.length; i++) {
  312. this.roster.create({
  313. jid: mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  314. subscription: 'both',
  315. ask: null,
  316. groups: groups,
  317. fullname: mock.cur_names[i]
  318. });
  319. }
  320. }, this));
  321. waits(50); // Needed, due to debounce
  322. runs($.proxy(function () {
  323. // Check that usernames appear alphabetically per group
  324. _.each(groups, $.proxy(function (name) {
  325. var $contacts = this.rosterview.$('dt.roster-group[data-group="'+name+'"]').nextUntil('dt', 'dd');
  326. var names = $.map($contacts, function (o) { return $(o).text().trim(); });
  327. expect(names).toEqual(_.clone(names).sort());
  328. expect(names.length).toEqual(mock.cur_names.length);
  329. }, this));
  330. }, this));
  331. }, converse));
  332. it("remembers whether it is closed or opened", $.proxy(function () {
  333. var i=0, j=0;
  334. var groups = {
  335. 'colleagues': 3,
  336. 'friends & acquaintences': 3,
  337. 'Ungrouped': 2
  338. };
  339. _.each(_.keys(groups), $.proxy(function (name) {
  340. j = i;
  341. for (i=j; i<j+groups[name]; i++) {
  342. this.roster.create({
  343. jid: mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  344. subscription: 'both',
  345. ask: null,
  346. groups: name === 'ungrouped'? [] : [name],
  347. fullname: mock.cur_names[i]
  348. });
  349. }
  350. }, converse));
  351. var view = this.rosterview.get('colleagues');
  352. var $toggle = view.$el.find('a.group-toggle');
  353. expect(view.model.get('state')).toBe('opened');
  354. $toggle.click();
  355. expect(view.model.get('state')).toBe('closed');
  356. $toggle.click();
  357. expect(view.model.get('state')).toBe('opened');
  358. }, converse));
  359. }, converse));
  360. describe("Pending Contacts", $.proxy(function () {
  361. function _clearContacts () {
  362. utils.clearBrowserStorage();
  363. converse.rosterview.model.reset();
  364. }
  365. function _addContacts () {
  366. _clearContacts();
  367. // Must be initialized, so that render is called and documentFragment set up.
  368. utils.createContacts('pending').openControlBox().openContactsPanel();
  369. }
  370. it("can be collapsed under their own header", $.proxy(function () {
  371. runs(function () {
  372. _addContacts();
  373. });
  374. waits(50);
  375. runs($.proxy(function () {
  376. checkHeaderToggling.apply(this, [this.rosterview.get('Pending contacts').$el]);
  377. }, this));
  378. }, converse));
  379. it("can be added to the roster", $.proxy(function () {
  380. _clearContacts();
  381. spyOn(converse, 'emit');
  382. spyOn(this.rosterview, 'update').andCallThrough();
  383. runs($.proxy(function () {
  384. this.roster.create({
  385. jid: mock.pend_names[0].replace(/ /g,'.').toLowerCase() + '@localhost',
  386. subscription: 'none',
  387. ask: 'subscribe',
  388. fullname: mock.pend_names[0]
  389. });
  390. }, converse));
  391. waits(300);
  392. runs($.proxy(function () {
  393. expect(this.rosterview.$el.is(':visible')).toEqual(true);
  394. expect(this.rosterview.update).toHaveBeenCalled();
  395. }, converse));
  396. }, converse));
  397. it("are shown in the roster when show_only_online_users", $.proxy(function () {
  398. converse.show_only_online_users = true;
  399. runs(function () {
  400. _addContacts();
  401. });
  402. waits(50);
  403. spyOn(this.rosterview, 'update').andCallThrough();
  404. runs($.proxy(function () {
  405. expect(this.rosterview.$el.is(':visible')).toEqual(true);
  406. expect(this.rosterview.update).toHaveBeenCalled();
  407. }, converse));
  408. waits(300); // Needed, due to debounce
  409. runs ($.proxy(function () {
  410. expect(this.rosterview.$el.find('dd:visible').length).toBe(3);
  411. expect(this.rosterview.$el.find('dt:visible').length).toBe(1);
  412. }, converse));
  413. converse.show_only_online_users = false;
  414. }, converse));
  415. it("are shown in the roster when hide_offline_users", $.proxy(function () {
  416. converse.hide_offline_users = true;
  417. runs(function () {
  418. _addContacts();
  419. });
  420. waits(50);
  421. spyOn(this.rosterview, 'update').andCallThrough();
  422. runs($.proxy(function () {
  423. expect(this.rosterview.$el.is(':visible')).toEqual(true);
  424. expect(this.rosterview.update).toHaveBeenCalled();
  425. }, converse));
  426. waits(300); // Needed, due to debounce
  427. runs ($.proxy(function () {
  428. expect(this.rosterview.$el.find('dd:visible').length).toBe(3);
  429. expect(this.rosterview.$el.find('dt:visible').length).toBe(1);
  430. }, converse));
  431. converse.hide_offline_users = false;
  432. }, converse));
  433. it("can be removed by the user", $.proxy(function () {
  434. runs($.proxy(function () {
  435. _addContacts();
  436. }, this));
  437. waits(50);
  438. runs($.proxy(function () {
  439. var name = mock.pend_names[0];
  440. var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  441. var contact = this.roster.get(jid);
  442. spyOn(window, 'confirm').andReturn(true);
  443. spyOn(contact, 'unauthorize').andCallFake(function () { return contact; });
  444. spyOn(contact, 'removeFromRoster');
  445. spyOn(this.connection, 'sendIQ').andCallFake(function (iq, callback) {
  446. if (typeof callback === "function") { return callback(); }
  447. });
  448. converse.rosterview.$el.find(".pending-contact-name:contains('"+name+"')")
  449. .siblings('.remove-xmpp-contact').click();
  450. expect(window.confirm).toHaveBeenCalled();
  451. expect(converse.connection.sendIQ).toHaveBeenCalled();
  452. expect(contact.removeFromRoster).toHaveBeenCalled();
  453. expect(this.connection.sendIQ).toHaveBeenCalled();
  454. expect(converse.rosterview.$el.find(".pending-contact-name:contains('"+name+"')").length).toEqual(0);
  455. }, this));
  456. }, converse));
  457. it("do not have a header if there aren't any", $.proxy(function () {
  458. var name = mock.pend_names[0];
  459. runs($.proxy(function () {
  460. _clearContacts();
  461. }, this));
  462. waits(50);
  463. runs($.proxy(function () {
  464. this.roster.create({
  465. jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
  466. subscription: 'none',
  467. ask: 'subscribe',
  468. fullname: name
  469. });
  470. spyOn(window, 'confirm').andReturn(true);
  471. spyOn(this.connection, 'sendIQ').andCallFake(function (iq, callback) {
  472. if (typeof callback === "function") { return callback(); }
  473. });
  474. expect(this.rosterview.get('Pending contacts').$el.is(':visible')).toEqual(true);
  475. converse.rosterview.$el.find(".pending-contact-name:contains('"+name+"')")
  476. .siblings('.remove-xmpp-contact').click();
  477. expect(window.confirm).toHaveBeenCalled();
  478. expect(this.connection.sendIQ).toHaveBeenCalled();
  479. expect(this.rosterview.get('Pending contacts').$el.is(':visible')).toEqual(false);
  480. }, this));
  481. }, converse));
  482. it("will lose their own header once the last one has been removed", $.proxy(function () {
  483. _addContacts();
  484. var name;
  485. spyOn(window, 'confirm').andReturn(true);
  486. for (var i=0; i<mock.pend_names.length; i++) {
  487. name = mock.pend_names[i];
  488. converse.rosterview.$el.find(".pending-contact-name:contains('"+name+"')")
  489. .siblings('.remove-xmpp-contact').click();
  490. }
  491. expect(this.rosterview.$el.find('dt#pending-xmpp-contacts').is(':visible')).toBeFalsy();
  492. }, converse));
  493. it("can be added to the roster and they will be sorted alphabetically", $.proxy(function () {
  494. _clearContacts();
  495. var i, t;
  496. spyOn(converse, 'emit');
  497. spyOn(this.rosterview, 'update').andCallThrough();
  498. for (i=0; i<mock.pend_names.length; i++) {
  499. this.roster.create({
  500. jid: mock.pend_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  501. subscription: 'none',
  502. ask: 'subscribe',
  503. fullname: mock.pend_names[i]
  504. });
  505. expect(this.rosterview.update).toHaveBeenCalled();
  506. }
  507. // Check that they are sorted alphabetically
  508. t = this.rosterview.get('Pending contacts').$el.siblings('dd.pending-xmpp-contact').find('span').text();
  509. expect(t).toEqual(mock.pend_names.slice(0,i+1).sort().join(''));
  510. }, converse));
  511. }, converse));
  512. describe("Existing Contacts", $.proxy(function () {
  513. function _clearContacts () {
  514. utils.clearBrowserStorage();
  515. converse.rosterview.model.reset();
  516. }
  517. var _addContacts = function () {
  518. _clearContacts();
  519. utils.createContacts('current').openControlBox().openContactsPanel();
  520. };
  521. it("can be collapsed under their own header", $.proxy(function () {
  522. runs(function () {
  523. _addContacts();
  524. });
  525. waits(50);
  526. runs($.proxy(function () {
  527. checkHeaderToggling.apply(this, [this.rosterview.$el.find('dt.roster-group')]);
  528. }, this));
  529. }, converse));
  530. it("will be hidden when appearing under a collapsed group", $.proxy(function () {
  531. _addContacts();
  532. this.rosterview.$el.find('dt.roster-group').find('a.group-toggle').click();
  533. var name = "Max Mustermann";
  534. var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  535. converse.roster.create({
  536. ask: null,
  537. fullname: name,
  538. jid: jid,
  539. requesting: false,
  540. subscription: 'both'
  541. });
  542. var view = this.rosterview.get('My contacts').get(jid);
  543. expect(view.$el.is(':visible')).toBe(false);
  544. }, converse));
  545. it("can be added to the roster and they will be sorted alphabetically", $.proxy(function () {
  546. runs(function () {
  547. _clearContacts();
  548. });
  549. waits(50);
  550. runs($.proxy(function () {
  551. var i, t;
  552. spyOn(this.rosterview, 'update').andCallThrough();
  553. for (i=0; i<mock.cur_names.length; i++) {
  554. this.roster.create({
  555. jid: mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  556. subscription: 'both',
  557. ask: null,
  558. fullname: mock.cur_names[i]
  559. });
  560. expect(this.rosterview.update).toHaveBeenCalled();
  561. }
  562. // Check that they are sorted alphabetically
  563. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.offline').find('a.open-chat').text();
  564. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  565. }, this));
  566. }, converse));
  567. it("can be removed by the user", $.proxy(function () {
  568. runs(function () {
  569. _addContacts();
  570. });
  571. waits(50);
  572. runs($.proxy(function () {
  573. var name = mock.cur_names[0];
  574. var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  575. var contact = this.roster.get(jid);
  576. spyOn(window, 'confirm').andReturn(true);
  577. spyOn(contact, 'removeFromRoster');
  578. spyOn(this.connection, 'sendIQ').andCallFake(function (iq, callback) {
  579. if (typeof callback === "function") { return callback(); }
  580. });
  581. converse.rosterview.$el.find(".open-chat:contains('"+name+"')")
  582. .siblings('.remove-xmpp-contact').click();
  583. expect(window.confirm).toHaveBeenCalled();
  584. expect(converse.connection.sendIQ).toHaveBeenCalled();
  585. expect(contact.removeFromRoster).toHaveBeenCalled();
  586. expect(converse.rosterview.$el.find(".open-chat:contains('"+name+"')").length).toEqual(0);
  587. }, this));
  588. }, converse));
  589. it("do not have a header if there aren't any", $.proxy(function () {
  590. var name = mock.cur_names[0];
  591. runs(function () {
  592. _clearContacts();
  593. });
  594. waits(50);
  595. runs($.proxy(function () {
  596. var contact = this.roster.create({
  597. jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
  598. subscription: 'both',
  599. ask: null,
  600. fullname: name
  601. });
  602. spyOn(window, 'confirm').andReturn(true);
  603. spyOn(contact, 'removeFromRoster');
  604. spyOn(this.connection, 'sendIQ').andCallFake(function (iq, callback) {
  605. if (typeof callback === "function") { return callback(); }
  606. });
  607. expect(this.rosterview.$el.find('dt.roster-group').css('display')).toEqual('block');
  608. converse.rosterview.$el.find(".open-chat:contains('"+name+"')")
  609. .siblings('.remove-xmpp-contact').click();
  610. expect(window.confirm).toHaveBeenCalled();
  611. expect(this.connection.sendIQ).toHaveBeenCalled();
  612. expect(contact.removeFromRoster).toHaveBeenCalled();
  613. expect(this.rosterview.$el.find('dt.roster-group').css('display')).toEqual('none');
  614. }, this));
  615. }, converse));
  616. it("can change their status to online and be sorted alphabetically", $.proxy(function () {
  617. runs(function () {
  618. _addContacts();
  619. });
  620. waits(50);
  621. runs($.proxy(function () {
  622. var jid, t;
  623. spyOn(converse, 'emit');
  624. spyOn(this.rosterview, 'update').andCallThrough();
  625. for (var i=0; i<mock.cur_names.length; i++) {
  626. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  627. this.roster.get(jid).set('chat_status', 'online');
  628. expect(this.rosterview.update).toHaveBeenCalled();
  629. // Check that they are sorted alphabetically
  630. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.online').find('a.open-chat').text();
  631. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  632. }
  633. }, this));
  634. }, converse));
  635. it("can change their status to busy and be sorted alphabetically", $.proxy(function () {
  636. runs(function () {
  637. _addContacts();
  638. });
  639. waits(50);
  640. runs($.proxy(function () {
  641. var jid, t;
  642. spyOn(converse, 'emit');
  643. spyOn(this.rosterview, 'update').andCallThrough();
  644. for (var i=0; i<mock.cur_names.length; i++) {
  645. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  646. this.roster.get(jid).set('chat_status', 'dnd');
  647. expect(this.rosterview.update).toHaveBeenCalled();
  648. // Check that they are sorted alphabetically
  649. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.dnd').find('a.open-chat').text();
  650. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  651. }
  652. }, this));
  653. }, converse));
  654. it("can change their status to away and be sorted alphabetically", $.proxy(function () {
  655. runs(function () {
  656. _addContacts();
  657. });
  658. waits(50);
  659. runs($.proxy(function () {
  660. var jid, t;
  661. spyOn(converse, 'emit');
  662. spyOn(this.rosterview, 'update').andCallThrough();
  663. for (var i=0; i<mock.cur_names.length; i++) {
  664. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  665. this.roster.get(jid).set('chat_status', 'away');
  666. expect(this.rosterview.update).toHaveBeenCalled();
  667. // Check that they are sorted alphabetically
  668. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.away').find('a.open-chat').text();
  669. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  670. }
  671. }, this));
  672. }, converse));
  673. it("can change their status to xa and be sorted alphabetically", $.proxy(function () {
  674. runs(function () {
  675. _addContacts();
  676. });
  677. waits(50);
  678. runs($.proxy(function () {
  679. var jid, t;
  680. spyOn(converse, 'emit');
  681. spyOn(this.rosterview, 'update').andCallThrough();
  682. for (var i=0; i<mock.cur_names.length; i++) {
  683. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  684. this.roster.get(jid).set('chat_status', 'xa');
  685. expect(this.rosterview.update).toHaveBeenCalled();
  686. // Check that they are sorted alphabetically
  687. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.xa').find('a.open-chat').text();
  688. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  689. }
  690. }, this));
  691. }, converse));
  692. it("can change their status to unavailable and be sorted alphabetically", $.proxy(function () {
  693. runs(function () {
  694. _addContacts();
  695. });
  696. waits(50);
  697. runs($.proxy(function () {
  698. var jid, t;
  699. spyOn(converse, 'emit');
  700. spyOn(this.rosterview, 'update').andCallThrough();
  701. for (var i=0; i<mock.cur_names.length; i++) {
  702. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  703. this.roster.get(jid).set('chat_status', 'unavailable');
  704. expect(this.rosterview.update).toHaveBeenCalled();
  705. // Check that they are sorted alphabetically
  706. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.unavailable').find('a.open-chat').text();
  707. expect(t).toEqual(mock.cur_names.slice(0, i+1).sort().join(''));
  708. }
  709. }, this));
  710. }, converse));
  711. it("are ordered according to status: online, busy, away, xa, unavailable, offline", $.proxy(function () {
  712. runs(function () {
  713. _addContacts();
  714. });
  715. waits(50);
  716. runs($.proxy(function () {
  717. var i, jid;
  718. for (i=0; i<3; i++) {
  719. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  720. this.roster.get(jid).set('chat_status', 'online');
  721. }
  722. for (i=3; i<6; i++) {
  723. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  724. this.roster.get(jid).set('chat_status', 'dnd');
  725. }
  726. for (i=6; i<9; i++) {
  727. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  728. this.roster.get(jid).set('chat_status', 'away');
  729. }
  730. for (i=9; i<12; i++) {
  731. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  732. this.roster.get(jid).set('chat_status', 'xa');
  733. }
  734. for (i=12; i<15; i++) {
  735. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  736. this.roster.get(jid).set('chat_status', 'unavailable');
  737. }
  738. var contacts = this.rosterview.$el.find('dd.current-xmpp-contact');
  739. for (i=0; i<3; i++) {
  740. expect($(contacts[i]).hasClass('online')).toBeTruthy();
  741. expect($(contacts[i]).hasClass('both')).toBeTruthy();
  742. expect($(contacts[i]).hasClass('dnd')).toBeFalsy();
  743. expect($(contacts[i]).hasClass('away')).toBeFalsy();
  744. expect($(contacts[i]).hasClass('xa')).toBeFalsy();
  745. expect($(contacts[i]).hasClass('unavailable')).toBeFalsy();
  746. expect($(contacts[i]).hasClass('offline')).toBeFalsy();
  747. }
  748. for (i=3; i<6; i++) {
  749. expect($(contacts[i]).hasClass('dnd')).toBeTruthy();
  750. expect($(contacts[i]).hasClass('both')).toBeTruthy();
  751. expect($(contacts[i]).hasClass('online')).toBeFalsy();
  752. expect($(contacts[i]).hasClass('away')).toBeFalsy();
  753. expect($(contacts[i]).hasClass('xa')).toBeFalsy();
  754. expect($(contacts[i]).hasClass('unavailable')).toBeFalsy();
  755. expect($(contacts[i]).hasClass('offline')).toBeFalsy();
  756. }
  757. for (i=6; i<9; i++) {
  758. expect($(contacts[i]).hasClass('away')).toBeTruthy();
  759. expect($(contacts[i]).hasClass('both')).toBeTruthy();
  760. expect($(contacts[i]).hasClass('online')).toBeFalsy();
  761. expect($(contacts[i]).hasClass('dnd')).toBeFalsy();
  762. expect($(contacts[i]).hasClass('xa')).toBeFalsy();
  763. expect($(contacts[i]).hasClass('unavailable')).toBeFalsy();
  764. expect($(contacts[i]).hasClass('offline')).toBeFalsy();
  765. }
  766. for (i=9; i<12; i++) {
  767. expect($(contacts[i]).hasClass('xa')).toBeTruthy();
  768. expect($(contacts[i]).hasClass('both')).toBeTruthy();
  769. expect($(contacts[i]).hasClass('online')).toBeFalsy();
  770. expect($(contacts[i]).hasClass('dnd')).toBeFalsy();
  771. expect($(contacts[i]).hasClass('away')).toBeFalsy();
  772. expect($(contacts[i]).hasClass('unavailable')).toBeFalsy();
  773. expect($(contacts[i]).hasClass('offline')).toBeFalsy();
  774. }
  775. for (i=12; i<15; i++) {
  776. expect($(contacts[i]).hasClass('unavailable')).toBeTruthy();
  777. expect($(contacts[i]).hasClass('both')).toBeTruthy();
  778. expect($(contacts[i]).hasClass('online')).toBeFalsy();
  779. expect($(contacts[i]).hasClass('dnd')).toBeFalsy();
  780. expect($(contacts[i]).hasClass('away')).toBeFalsy();
  781. expect($(contacts[i]).hasClass('xa')).toBeFalsy();
  782. expect($(contacts[i]).hasClass('offline')).toBeFalsy();
  783. }
  784. for (i=15; i<mock.cur_names.length; i++) {
  785. expect($(contacts[i]).hasClass('offline')).toBeTruthy();
  786. expect($(contacts[i]).hasClass('both')).toBeTruthy();
  787. expect($(contacts[i]).hasClass('online')).toBeFalsy();
  788. expect($(contacts[i]).hasClass('dnd')).toBeFalsy();
  789. expect($(contacts[i]).hasClass('away')).toBeFalsy();
  790. expect($(contacts[i]).hasClass('xa')).toBeFalsy();
  791. expect($(contacts[i]).hasClass('unavailable')).toBeFalsy();
  792. }
  793. }, this));
  794. }, converse));
  795. }, converse));
  796. describe("Requesting Contacts", $.proxy(function () {
  797. beforeEach($.proxy(function () {
  798. runs(function () {
  799. utils.clearBrowserStorage();
  800. converse.rosterview.model.reset();
  801. utils.createContacts('requesting').openControlBox();
  802. });
  803. waits(50);
  804. runs(function () {
  805. utils.openContactsPanel();
  806. });
  807. }, converse));
  808. it("can be added to the roster and they will be sorted alphabetically", $.proxy(function () {
  809. converse.rosterview.model.reset(); // We want to manually create users so that we can spy
  810. var i, children;
  811. var names = [];
  812. spyOn(converse, 'emit');
  813. spyOn(this.rosterview, 'update').andCallThrough();
  814. spyOn(this.controlboxtoggle, 'showControlBox').andCallThrough();
  815. var addName = function (idx, item) {
  816. if (!$(item).hasClass('request-actions')) {
  817. names.push($(item).text().replace(/^\s+|\s+$/g, ''));
  818. }
  819. };
  820. for (i=0; i<mock.req_names.length; i++) {
  821. this.roster.create({
  822. jid: mock.req_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  823. subscription: 'none',
  824. ask: null,
  825. requesting: true,
  826. fullname: mock.req_names[i]
  827. });
  828. expect(this.rosterview.update).toHaveBeenCalled();
  829. // When a requesting contact is added, the controlbox must
  830. // be opened.
  831. expect(this.controlboxtoggle.showControlBox).toHaveBeenCalled();
  832. }
  833. // Check that they are sorted alphabetically
  834. children = this.rosterview.get('Contact requests').$el.siblings('dd.requesting-xmpp-contact').children('span');
  835. names = [];
  836. children.each(addName);
  837. expect(names.join('')).toEqual(mock.req_names.slice(0,i+1).sort().join(''));
  838. }, converse));
  839. it("do not have a header if there aren't any", $.proxy(function () {
  840. converse.rosterview.model.reset(); // We want to manually create users so that we can spy
  841. var name = mock.req_names[0];
  842. runs($.proxy(function () {
  843. spyOn(window, 'confirm').andReturn(true);
  844. this.roster.create({
  845. jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
  846. subscription: 'none',
  847. ask: null,
  848. requesting: true,
  849. fullname: name
  850. });
  851. }, this));
  852. waits(50);
  853. runs($.proxy(function () {
  854. expect(this.rosterview.get('Contact requests').$el.is(':visible')).toEqual(true);
  855. converse.rosterview.$el.find(".req-contact-name:contains('"+name+"')")
  856. .siblings('.request-actions')
  857. .find('.decline-xmpp-request').click();
  858. expect(window.confirm).toHaveBeenCalled();
  859. expect(this.rosterview.get('Contact requests').$el.is(':visible')).toEqual(false);
  860. }, this));
  861. }, converse));
  862. it("can be collapsed under their own header", $.proxy(function () {
  863. checkHeaderToggling.apply(this, [this.rosterview.get('Contact requests').$el]);
  864. }, converse));
  865. it("can have their requests accepted by the user", $.proxy(function () {
  866. // TODO: Testing can be more thorough here, the user is
  867. // actually not accepted/authorized because of
  868. // mock_connection.
  869. var name = mock.req_names.sort()[0];
  870. var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  871. var contact = this.roster.get(jid);
  872. spyOn(converse.roster, 'sendContactAddIQ').andCallFake(function (jid, fullname, groups, callback) {
  873. callback();
  874. });
  875. spyOn(contact, 'authorize').andCallFake(function () { return contact; });
  876. converse.rosterview.$el.find(".req-contact-name:contains('"+name+"')")
  877. .siblings('.request-actions')
  878. .find('.accept-xmpp-request').click();
  879. expect(converse.roster.sendContactAddIQ).toHaveBeenCalled();
  880. expect(contact.authorize).toHaveBeenCalled();
  881. }, converse));
  882. it("can have their requests denied by the user", $.proxy(function () {
  883. this.rosterview.model.reset();
  884. runs($.proxy(function () {
  885. utils.createContacts('requesting').openControlBox();
  886. converse.rosterview.update(); // XXX: Hack to make sure $roster element is attaced.
  887. }, this));
  888. waits(50);
  889. runs($.proxy(function () {
  890. var name = mock.req_names.sort()[1];
  891. var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  892. var contact = this.roster.get(jid);
  893. spyOn(window, 'confirm').andReturn(true);
  894. spyOn(contact, 'unauthorize').andCallFake(function () { return contact; });
  895. converse.rosterview.$el.find(".req-contact-name:contains('"+name+"')")
  896. .siblings('.request-actions')
  897. .find('.decline-xmpp-request').click();
  898. expect(window.confirm).toHaveBeenCalled();
  899. expect(contact.unauthorize).toHaveBeenCalled();
  900. // There should now be one less contact
  901. expect(this.roster.length).toEqual(mock.req_names.length-1);
  902. }, this));
  903. }, converse));
  904. it("are persisted even if other contacts' change their presence ", $.proxy(function() {
  905. /* This is a regression test.
  906. * https://github.com/jcbrand/converse.js/issues/262
  907. */
  908. this.rosterview.model.reset();
  909. expect(this.roster.pluck('jid').length).toBe(0);
  910. var stanza = $pres({from: 'data@enterprise/resource', type: 'subscribe'});
  911. this.connection._dataRecv(test_utils.createRequest(stanza));
  912. expect(this.roster.pluck('jid').length).toBe(1);
  913. expect(_.contains(this.roster.pluck('jid'), 'data@enterprise')).toBeTruthy();
  914. // Taken from the spec
  915. // http://xmpp.org/rfcs/rfc3921.html#rfc.section.7.3
  916. stanza = $iq({
  917. to: this.connection.jid,
  918. type: 'result',
  919. id: 'roster_1'
  920. }).c('query', {
  921. xmlns: 'jabber:iq:roster',
  922. }).c('item', {
  923. jid: 'romeo@example.net',
  924. name: 'Romeo',
  925. subscription:'both'
  926. }).c('group').t('Friends').up().up()
  927. .c('item', {
  928. jid: 'mercutio@example.org',
  929. name: 'Mercutio',
  930. subscription:'from'
  931. }).c('group').t('Friends').up().up()
  932. .c('item', {
  933. jid: 'benvolio@example.org',
  934. name: 'Benvolio',
  935. subscription:'both'
  936. }).c('group').t('Friends');
  937. this.roster.onReceivedFromServer(stanza.tree());
  938. expect(_.contains(this.roster.pluck('jid'), 'data@enterprise')).toBeTruthy();
  939. }, converse));
  940. }, converse));
  941. describe("All Contacts", $.proxy(function () {
  942. beforeEach($.proxy(function () {
  943. runs(function () {
  944. utils.clearBrowserStorage();
  945. converse.rosterview.model.reset();
  946. utils.createContacts('all').openControlBox();
  947. });
  948. waits(50);
  949. runs(function () {
  950. utils.openContactsPanel();
  951. });
  952. }, converse));
  953. it("are saved to, and can be retrieved from, browserStorage", $.proxy(function () {
  954. var new_attrs, old_attrs, attrs;
  955. var num_contacts = this.roster.length;
  956. var new_roster = new this.RosterContacts();
  957. // Roster items are yet to be fetched from browserStorage
  958. expect(new_roster.length).toEqual(0);
  959. new_roster.browserStorage = this.roster.browserStorage;
  960. new_roster.fetch();
  961. expect(new_roster.length).toEqual(num_contacts);
  962. // Check that the roster items retrieved from browserStorage
  963. // have the same attributes values as the original ones.
  964. attrs = ['jid', 'fullname', 'subscription', 'ask'];
  965. for (var i=0; i<attrs.length; i++) {
  966. new_attrs = _.pluck(_.pluck(new_roster.models, 'attributes'), attrs[i]);
  967. old_attrs = _.pluck(_.pluck(this.roster.models, 'attributes'), attrs[i]);
  968. // Roster items in storage are not necessarily sorted,
  969. // so we have to sort them here to do a proper
  970. // comparison
  971. expect(_.isEqual(new_attrs.sort(), old_attrs.sort())).toEqual(true);
  972. }
  973. }, converse));
  974. it("will show fullname and jid properties on tooltip", $.proxy(function () {
  975. var jid, name, i;
  976. for (i=0; i<mock.cur_names.length; i++) {
  977. name = mock.cur_names[i];
  978. jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  979. var $dd = this.rosterview.$el.find("dd:contains('"+name+"')").children().first();
  980. var dd_text = $dd.text();
  981. var dd_title = $dd.attr('title');
  982. expect(dd_text).toBe(name);
  983. expect(dd_title).toContain(name);
  984. expect(dd_title).toContain(jid);
  985. }
  986. }, converse));
  987. }, converse));
  988. }, converse, mock, test_utils));
  989. describe("The 'Add Contact' widget", $.proxy(function (mock, test_utils) {
  990. it("opens up an add form when you click on it", $.proxy(function () {
  991. var panel = this.chatboxviews.get('controlbox').contactspanel;
  992. spyOn(panel, 'toggleContactForm').andCallThrough();
  993. panel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  994. panel.$el.find('a.toggle-xmpp-contact-form').click();
  995. expect(panel.toggleContactForm).toHaveBeenCalled();
  996. // XXX: Awaiting more tests, close it again for now...
  997. panel.$el.find('a.toggle-xmpp-contact-form').click();
  998. }, converse));
  999. }, converse, mock, test_utils));
  1000. describe("The Controlbox Tabs", $.proxy(function () {
  1001. beforeEach($.proxy(function () {
  1002. runs(function () {
  1003. test_utils.closeAllChatBoxes();
  1004. });
  1005. waits(50);
  1006. runs(function () {
  1007. test_utils.openControlBox();
  1008. });
  1009. }, converse));
  1010. it("contains two tabs, 'Contacts' and 'ChatRooms'", $.proxy(function () {
  1011. var cbview = this.chatboxviews.get('controlbox');
  1012. var $panels = cbview.$el.find('.controlbox-panes');
  1013. expect($panels.children().length).toBe(2);
  1014. expect($panels.children().first().attr('id')).toBe('users');
  1015. expect($panels.children().first().is(':visible')).toBe(true);
  1016. expect($panels.children().last().attr('id')).toBe('chatrooms');
  1017. expect($panels.children().last().is(':visible')).toBe(false);
  1018. }, converse));
  1019. describe("chatrooms panel", $.proxy(function () {
  1020. beforeEach($.proxy(function () {
  1021. runs(function () {
  1022. test_utils.closeAllChatBoxes();
  1023. });
  1024. waits(50);
  1025. runs(function () {
  1026. test_utils.openControlBox();
  1027. });
  1028. }, converse));
  1029. it("is opened by clicking the 'Chatrooms' tab", $.proxy(function () {
  1030. var cbview = this.chatboxviews.get('controlbox');
  1031. var $tabs = cbview.$el.find('#controlbox-tabs');
  1032. var $panels = cbview.$el.find('.controlbox-panes');
  1033. var $contacts = $panels.children().first();
  1034. var $chatrooms = $panels.children().last();
  1035. spyOn(cbview, 'switchTab').andCallThrough();
  1036. cbview.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  1037. $tabs.find('li').last().find('a').click(); // Clicks the chatrooms tab
  1038. expect($contacts.is(':visible')).toBe(false);
  1039. expect($chatrooms.is(':visible')).toBe(true);
  1040. expect(cbview.switchTab).toHaveBeenCalled();
  1041. }, converse));
  1042. it("contains a form through which a new chatroom can be created", $.proxy(function () {
  1043. var roomspanel = this.chatboxviews.get('controlbox').roomspanel;
  1044. var $input = roomspanel.$el.find('input.new-chatroom-name');
  1045. var $nick = roomspanel.$el.find('input.new-chatroom-nick');
  1046. var $server = roomspanel.$el.find('input.new-chatroom-server');
  1047. expect($input.length).toBe(1);
  1048. expect($server.length).toBe(1);
  1049. expect($('.chatroom:visible').length).toBe(0); // There shouldn't be any chatrooms open currently
  1050. spyOn(roomspanel, 'createChatRoom').andCallThrough();
  1051. roomspanel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  1052. runs(function () {
  1053. $input.val('Lounge');
  1054. $nick.val('dummy');
  1055. $server.val('muc.localhost');
  1056. });
  1057. waits('250');
  1058. runs(function () {
  1059. roomspanel.$el.find('form').submit();
  1060. expect(roomspanel.createChatRoom).toHaveBeenCalled();
  1061. });
  1062. waits('250');
  1063. runs($.proxy(function () {
  1064. expect($('.chatroom:visible').length).toBe(1); // There should now be an open chatroom
  1065. }, converse));
  1066. }, converse));
  1067. it("can list rooms publically available on the server", $.proxy(function () {
  1068. var panel = this.chatboxviews.get('controlbox').roomspanel;
  1069. panel.$tabs.find('li').last().find('a').click(); // Click the chatrooms tab
  1070. panel.model.set({'muc_domain': 'muc.localhost'}); // Make sure the domain is set
  1071. // See: http://xmpp.org/extensions/xep-0045.html#disco-rooms
  1072. expect($('#available-chatrooms').children('dt').length).toBe(0);
  1073. expect($('#available-chatrooms').children('dd').length).toBe(0);
  1074. var iq = $iq({
  1075. from:'muc.localhost',
  1076. to:'dummy@localhost/pda',
  1077. type:'result'
  1078. }).c('query')
  1079. .c('item', { jid:'heath@chat.shakespeare.lit', name:'A Lonely Heath'}).up()
  1080. .c('item', { jid:'coven@chat.shakespeare.lit', name:'A Dark Cave'}).up()
  1081. .c('item', { jid:'forres@chat.shakespeare.lit', name:'The Palace'}).up()
  1082. .c('item', { jid:'inverness@chat.shakespeare.lit', name:'Macbeth&apos;s Castle'}).nodeTree;
  1083. panel.onRoomsFound(iq);
  1084. expect(panel.$('#available-chatrooms').children('dt').length).toBe(1);
  1085. expect(panel.$('#available-chatrooms').children('dt').first().text()).toBe("Rooms on muc.localhost");
  1086. expect(panel.$('#available-chatrooms').children('dd').length).toBe(4);
  1087. }, converse));
  1088. }, converse));
  1089. }, converse, mock, test_utils));
  1090. }));