controlbox.js 56 KB

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