controlbox.js 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  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. /* FIXME: Monkepatch
  436. * After refactoring the mock connection to use a
  437. * Strophe.Connection object, these tests fail because "remove"
  438. * function in strophe.roster (line 292) gets called and it
  439. * then tries to actually remove the user which is not in the roster...
  440. */
  441. var old_remove = this.connection.roster.remove;
  442. this.connection.roster.remove = function (jid, callback) { callback(); };
  443. var name = mock.pend_names[0];
  444. var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  445. spyOn(window, 'confirm').andReturn(true);
  446. spyOn(converse, 'emit');
  447. spyOn(this.connection.roster, 'remove').andCallThrough();
  448. spyOn(this.connection.roster, 'unauthorize');
  449. spyOn(this.rosterview.model, 'remove').andCallThrough();
  450. converse.rosterview.$el.find(".pending-contact-name:contains('"+name+"')")
  451. .siblings('.remove-xmpp-contact').click();
  452. expect(window.confirm).toHaveBeenCalled();
  453. expect(this.connection.roster.remove).toHaveBeenCalled();
  454. expect(this.connection.roster.unauthorize).toHaveBeenCalled();
  455. expect(this.rosterview.model.remove).toHaveBeenCalled();
  456. expect(converse.rosterview.$el.find(".pending-contact-name:contains('"+name+"')").length).toEqual(0);
  457. /* XXX Restore Monkeypatch */
  458. this.connection.roster.remove = old_remove;
  459. }, this));
  460. }, converse));
  461. it("do not have a header if there aren't any", $.proxy(function () {
  462. var name = mock.pend_names[0];
  463. runs($.proxy(function () {
  464. _clearContacts();
  465. }, this));
  466. waits(50);
  467. runs($.proxy(function () {
  468. spyOn(window, 'confirm').andReturn(true);
  469. this.roster.create({
  470. jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
  471. subscription: 'none',
  472. ask: 'subscribe',
  473. fullname: name
  474. });
  475. expect(this.rosterview.get('Pending contacts').$el.is(':visible')).toEqual(true);
  476. converse.rosterview.$el.find(".pending-contact-name:contains('"+name+"')")
  477. .siblings('.remove-xmpp-contact').click();
  478. expect(window.confirm).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 (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(converse, 'emit');
  553. spyOn(this.rosterview, 'update').andCallThrough();
  554. for (i=0; i<mock.cur_names.length; i++) {
  555. this.roster.create({
  556. jid: mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  557. subscription: 'both',
  558. ask: null,
  559. fullname: mock.cur_names[i]
  560. });
  561. expect(this.rosterview.update).toHaveBeenCalled();
  562. }
  563. // Check that they are sorted alphabetically
  564. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.offline').find('a.open-chat').text();
  565. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  566. }, this));
  567. }, converse));
  568. it("can be removed by the user", $.proxy(function () {
  569. runs(function () {
  570. _addContacts();
  571. });
  572. waits(50);
  573. runs($.proxy(function () {
  574. /* FIXME: Monkepatch
  575. * After refactoring the mock connection to use a
  576. * Strophe.Connection object, these tests fail because "remove"
  577. * function in strophe.roster (line 292) gets called and it
  578. * then tries to actually remove the user which is not in the roster...
  579. */
  580. var old_remove = this.connection.roster.remove;
  581. this.connection.roster.remove = function (jid, callback) { callback(); };
  582. var name = mock.cur_names[0];
  583. var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  584. spyOn(window, 'confirm').andReturn(true);
  585. spyOn(converse, 'emit');
  586. spyOn(this.connection.roster, 'remove').andCallThrough();
  587. spyOn(this.connection.roster, 'unauthorize');
  588. spyOn(this.rosterview.model, 'remove').andCallThrough();
  589. converse.rosterview.$el.find(".open-chat:contains('"+name+"')")
  590. .siblings('.remove-xmpp-contact').click();
  591. expect(window.confirm).toHaveBeenCalled();
  592. expect(this.connection.roster.remove).toHaveBeenCalled();
  593. expect(this.connection.roster.unauthorize).toHaveBeenCalled();
  594. expect(this.rosterview.model.remove).toHaveBeenCalled();
  595. expect(converse.rosterview.$el.find(".open-chat:contains('"+name+"')").length).toEqual(0);
  596. /* XXX Restore Monkeypatch */
  597. this.connection.roster.remove = old_remove;
  598. }, this));
  599. }, converse));
  600. it("do not have a header if there aren't any", $.proxy(function () {
  601. var name = mock.cur_names[0];
  602. runs(function () {
  603. _clearContacts();
  604. });
  605. waits(50);
  606. runs($.proxy(function () {
  607. spyOn(window, 'confirm').andReturn(true);
  608. this.roster.create({
  609. jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
  610. subscription: 'both',
  611. ask: null,
  612. fullname: name
  613. });
  614. expect(this.rosterview.$el.find('dt.roster-group').css('display')).toEqual('block');
  615. converse.rosterview.$el.find(".open-chat:contains('"+name+"')")
  616. .siblings('.remove-xmpp-contact').click();
  617. expect(window.confirm).toHaveBeenCalled();
  618. expect(this.rosterview.$el.find('dt.roster-group').css('display')).toEqual('none');
  619. }, this));
  620. }, converse));
  621. it("can change their status to online and be sorted alphabetically", $.proxy(function () {
  622. runs(function () {
  623. _addContacts();
  624. });
  625. waits(50);
  626. runs($.proxy(function () {
  627. var jid, t;
  628. spyOn(converse, 'emit');
  629. spyOn(this.rosterview, 'update').andCallThrough();
  630. for (i=0; i<mock.cur_names.length; i++) {
  631. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  632. this.roster.get(jid).set('chat_status', 'online');
  633. expect(this.rosterview.update).toHaveBeenCalled();
  634. // Check that they are sorted alphabetically
  635. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.online').find('a.open-chat').text();
  636. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  637. }
  638. }, this));
  639. }, converse));
  640. it("can change their status to busy and be sorted alphabetically", $.proxy(function () {
  641. runs(function () {
  642. _addContacts();
  643. });
  644. waits(50);
  645. runs($.proxy(function () {
  646. var jid, t;
  647. spyOn(converse, 'emit');
  648. spyOn(this.rosterview, 'update').andCallThrough();
  649. for (i=0; i<mock.cur_names.length; i++) {
  650. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  651. this.roster.get(jid).set('chat_status', 'dnd');
  652. expect(this.rosterview.update).toHaveBeenCalled();
  653. // Check that they are sorted alphabetically
  654. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.dnd').find('a.open-chat').text();
  655. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  656. }
  657. }, this));
  658. }, converse));
  659. it("can change their status to away and be sorted alphabetically", $.proxy(function () {
  660. runs(function () {
  661. _addContacts();
  662. });
  663. waits(50);
  664. runs($.proxy(function () {
  665. var jid, t;
  666. spyOn(converse, 'emit');
  667. spyOn(this.rosterview, 'update').andCallThrough();
  668. for (i=0; i<mock.cur_names.length; i++) {
  669. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  670. this.roster.get(jid).set('chat_status', 'away');
  671. expect(this.rosterview.update).toHaveBeenCalled();
  672. // Check that they are sorted alphabetically
  673. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.away').find('a.open-chat').text();
  674. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  675. }
  676. }, this));
  677. }, converse));
  678. it("can change their status to xa and be sorted alphabetically", $.proxy(function () {
  679. runs(function () {
  680. _addContacts();
  681. });
  682. waits(50);
  683. runs($.proxy(function () {
  684. var jid, t;
  685. spyOn(converse, 'emit');
  686. spyOn(this.rosterview, 'update').andCallThrough();
  687. for (i=0; i<mock.cur_names.length; i++) {
  688. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  689. this.roster.get(jid).set('chat_status', 'xa');
  690. expect(this.rosterview.update).toHaveBeenCalled();
  691. // Check that they are sorted alphabetically
  692. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.xa').find('a.open-chat').text();
  693. expect(t).toEqual(mock.cur_names.slice(0,i+1).sort().join(''));
  694. }
  695. }, this));
  696. }, converse));
  697. it("can change their status to unavailable and be sorted alphabetically", $.proxy(function () {
  698. runs(function () {
  699. _addContacts();
  700. });
  701. waits(50);
  702. runs($.proxy(function () {
  703. var jid, t;
  704. spyOn(converse, 'emit');
  705. spyOn(this.rosterview, 'update').andCallThrough();
  706. for (i=0; i<mock.cur_names.length; i++) {
  707. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  708. this.roster.get(jid).set('chat_status', 'unavailable');
  709. expect(this.rosterview.update).toHaveBeenCalled();
  710. // Check that they are sorted alphabetically
  711. t = this.rosterview.$el.find('dt.roster-group').siblings('dd.current-xmpp-contact.unavailable').find('a.open-chat').text();
  712. expect(t).toEqual(mock.cur_names.slice(0, i+1).sort().join(''));
  713. }
  714. }, this));
  715. }, converse));
  716. it("are ordered according to status: online, busy, away, xa, unavailable, offline", $.proxy(function () {
  717. runs(function () {
  718. _addContacts();
  719. });
  720. waits(50);
  721. runs($.proxy(function () {
  722. var i;
  723. for (i=0; i<3; i++) {
  724. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  725. this.roster.get(jid).set('chat_status', 'online');
  726. }
  727. for (i=3; i<6; i++) {
  728. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  729. this.roster.get(jid).set('chat_status', 'dnd');
  730. }
  731. for (i=6; i<9; i++) {
  732. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  733. this.roster.get(jid).set('chat_status', 'away');
  734. }
  735. for (i=9; i<12; i++) {
  736. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  737. this.roster.get(jid).set('chat_status', 'xa');
  738. }
  739. for (i=12; i<15; i++) {
  740. jid = mock.cur_names[i].replace(/ /g,'.').toLowerCase() + '@localhost';
  741. this.roster.get(jid).set('chat_status', 'unavailable');
  742. }
  743. var contacts = this.rosterview.$el.find('dd.current-xmpp-contact');
  744. for (i=0; i<3; i++) {
  745. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('online');
  746. }
  747. for (i=3; i<6; i++) {
  748. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('dnd');
  749. }
  750. for (i=6; i<9; i++) {
  751. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('away');
  752. }
  753. for (i=9; i<12; i++) {
  754. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('xa');
  755. }
  756. for (i=12; i<15; i++) {
  757. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('unavailable');
  758. }
  759. for (i=15; i<mock.cur_names.length; i++) {
  760. expect($(contacts[i]).attr('class').split(' ',1)[0]).toEqual('offline');
  761. }
  762. }, this));
  763. }, converse));
  764. }, converse));
  765. describe("Requesting Contacts", $.proxy(function () {
  766. beforeEach($.proxy(function () {
  767. runs(function () {
  768. utils.clearBrowserStorage();
  769. converse.rosterview.model.reset();
  770. utils.createContacts('requesting').openControlBox();
  771. });
  772. waits(50);
  773. runs(function () {
  774. utils.openContactsPanel();
  775. });
  776. }, converse));
  777. it("can be added to the roster and they will be sorted alphabetically", $.proxy(function () {
  778. converse.rosterview.model.reset(); // We want to manually create users so that we can spy
  779. var i, children;
  780. var names = [];
  781. spyOn(converse, 'emit');
  782. spyOn(this.rosterview, 'update').andCallThrough();
  783. spyOn(this.controlboxtoggle, 'showControlBox').andCallThrough();
  784. var addName = function (idx, item) {
  785. if (!$(item).hasClass('request-actions')) {
  786. names.push($(item).text().replace(/^\s+|\s+$/g, ''));
  787. }
  788. };
  789. for (i=0; i<mock.req_names.length; i++) {
  790. this.roster.create({
  791. jid: mock.req_names[i].replace(/ /g,'.').toLowerCase() + '@localhost',
  792. subscription: 'none',
  793. ask: null,
  794. requesting: true,
  795. fullname: mock.req_names[i]
  796. });
  797. expect(this.rosterview.update).toHaveBeenCalled();
  798. // When a requesting contact is added, the controlbox must
  799. // be opened.
  800. expect(this.controlboxtoggle.showControlBox).toHaveBeenCalled();
  801. }
  802. // Check that they are sorted alphabetically
  803. children = this.rosterview.get('Contact requests').$el.siblings('dd.requesting-xmpp-contact').children('span');
  804. names = [];
  805. children.each(addName);
  806. expect(names.join('')).toEqual(mock.req_names.slice(0,i+1).sort().join(''));
  807. }, converse));
  808. it("do not have a header if there aren't any", $.proxy(function () {
  809. converse.rosterview.model.reset(); // We want to manually create users so that we can spy
  810. var name = mock.req_names[0];
  811. runs($.proxy(function () {
  812. spyOn(window, 'confirm').andReturn(true);
  813. this.roster.create({
  814. jid: name.replace(/ /g,'.').toLowerCase() + '@localhost',
  815. subscription: 'none',
  816. ask: null,
  817. requesting: true,
  818. fullname: name
  819. });
  820. }, this));
  821. waits(50);
  822. runs($.proxy(function () {
  823. expect(this.rosterview.get('Contact requests').$el.is(':visible')).toEqual(true);
  824. converse.rosterview.$el.find(".req-contact-name:contains('"+name+"')")
  825. .siblings('.request-actions')
  826. .find('.decline-xmpp-request').click();
  827. expect(window.confirm).toHaveBeenCalled();
  828. expect(this.rosterview.get('Contact requests').$el.is(':visible')).toEqual(false);
  829. }, this));
  830. }, converse));
  831. it("can be collapsed under their own header", $.proxy(function () {
  832. checkHeaderToggling.apply(this, [this.rosterview.get('Contact requests').$el]);
  833. }, converse));
  834. it("can have their requests accepted by the user", $.proxy(function () {
  835. // TODO: Testing can be more thorough here, the user is
  836. // actually not accepted/authorized because of
  837. // mock_connection.
  838. var name = mock.req_names.sort()[0];
  839. var jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  840. spyOn(this.connection.roster, 'authorize');
  841. converse.rosterview.$el.find(".req-contact-name:contains('"+name+"')")
  842. .siblings('.request-actions')
  843. .find('.accept-xmpp-request').click();
  844. expect(this.connection.roster.authorize).toHaveBeenCalled();
  845. }, converse));
  846. it("can have their requests denied by the user", $.proxy(function () {
  847. this.rosterview.model.reset();
  848. runs($.proxy(function () {
  849. spyOn(converse, 'emit');
  850. spyOn(this.connection.roster, 'unauthorize');
  851. spyOn(window, 'confirm').andReturn(true);
  852. utils.createContacts('requesting').openControlBox();
  853. converse.rosterview.update(); // XXX: Hack to make sure $roster element is attaced.
  854. }, this));
  855. waits(50);
  856. runs($.proxy(function () {
  857. var name = mock.req_names.sort()[1];
  858. converse.rosterview.$el.find(".req-contact-name:contains('"+name+"')")
  859. .siblings('.request-actions')
  860. .find('.decline-xmpp-request').click();
  861. }, this));
  862. waits(50);
  863. runs($.proxy(function () {
  864. expect(window.confirm).toHaveBeenCalled();
  865. expect(this.connection.roster.unauthorize).toHaveBeenCalled();
  866. // There should now be one less contact
  867. expect(this.roster.length).toEqual(mock.req_names.length-1);
  868. }, this));
  869. }, converse));
  870. it("are persisted even if other contacts' change their presence ", $.proxy(function() {
  871. /* This is a regression test.
  872. * https://github.com/jcbrand/converse.js/issues/262
  873. */
  874. this.rosterview.model.reset();
  875. spyOn(this.roster, 'clearCache').andCallThrough();
  876. expect(this.roster.pluck('jid').length).toBe(0);
  877. var stanza = $pres({from: 'data@enterprise/resource', type: 'subscribe'});
  878. this.connection._dataRecv(test_utils.createRequest(stanza));
  879. expect(this.roster.pluck('jid').length).toBe(1);
  880. expect(_.contains(this.roster.pluck('jid'), 'data@enterprise')).toBeTruthy();
  881. // Taken from the spec
  882. // http://xmpp.org/rfcs/rfc3921.html#rfc.section.7.3
  883. stanza = $iq({
  884. to: this.connection.jid,
  885. type: 'result',
  886. id: 'roster_1'
  887. }).c('query', {
  888. xmlns: 'jabber:iq:roster',
  889. }).c('item', {
  890. jid: 'romeo@example.net',
  891. name: 'Romeo',
  892. subscription:'both'
  893. }).c('group').t('Friends').up().up()
  894. .c('item', {
  895. jid: 'mercutio@example.org',
  896. name: 'Mercutio',
  897. subscription:'from'
  898. }).c('group').t('Friends').up().up()
  899. .c('item', {
  900. jid: 'benvolio@example.org',
  901. name: 'Benvolio',
  902. subscription:'both'
  903. }).c('group').t('Friends');
  904. this.connection.roster._onReceiveRosterSuccess(null, stanza.tree());
  905. expect(this.roster.clearCache).toHaveBeenCalled();
  906. expect(_.contains(this.roster.pluck('jid'), 'data@enterprise')).toBeTruthy();
  907. }, converse));
  908. }, converse));
  909. describe("All Contacts", $.proxy(function () {
  910. beforeEach($.proxy(function () {
  911. runs(function () {
  912. utils.clearBrowserStorage();
  913. converse.rosterview.model.reset();
  914. utils.createContacts('all').openControlBox();
  915. });
  916. waits(50);
  917. runs(function () {
  918. utils.openContactsPanel();
  919. });
  920. }, converse));
  921. it("are saved to, and can be retrieved from, browserStorage", $.proxy(function () {
  922. var new_attrs, old_attrs, attrs;
  923. var num_contacts = this.roster.length;
  924. new_roster = new this.RosterContacts();
  925. // Roster items are yet to be fetched from browserStorage
  926. expect(new_roster.length).toEqual(0);
  927. new_roster.browserStorage = this.roster.browserStorage;
  928. new_roster.fetch();
  929. expect(new_roster.length).toEqual(num_contacts);
  930. // Check that the roster items retrieved from browserStorage
  931. // have the same attributes values as the original ones.
  932. attrs = ['jid', 'fullname', 'subscription', 'ask'];
  933. for (i=0; i<attrs.length; i++) {
  934. new_attrs = _.pluck(_.pluck(new_roster.models, 'attributes'), attrs[i]);
  935. old_attrs = _.pluck(_.pluck(this.roster.models, 'attributes'), attrs[i]);
  936. // Roster items in storage are not necessarily sorted,
  937. // so we have to sort them here to do a proper
  938. // comparison
  939. expect(_.isEqual(new_attrs.sort(), old_attrs.sort())).toEqual(true);
  940. }
  941. }, converse));
  942. it("will show fullname and jid properties on tooltip", $.proxy(function () {
  943. var jid, name, i, t;
  944. for (i=0; i<mock.cur_names.length; i++) {
  945. name = mock.cur_names[i];
  946. jid = name.replace(/ /g,'.').toLowerCase() + '@localhost';
  947. var $dd = this.rosterview.$el.find("dd:contains('"+name+"')").children().first();
  948. var dd_text = $dd.text();
  949. var dd_title = $dd.attr('title');
  950. expect(dd_text).toBe(name);
  951. expect(dd_title).toContain(name);
  952. expect(dd_title).toContain(jid);
  953. }
  954. }, converse));
  955. }, converse));
  956. }, converse, mock, test_utils));
  957. describe("The 'Add Contact' widget", $.proxy(function (mock, test_utils) {
  958. it("opens up an add form when you click on it", $.proxy(function () {
  959. var panel = this.chatboxviews.get('controlbox').contactspanel;
  960. spyOn(panel, 'toggleContactForm').andCallThrough();
  961. panel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  962. panel.$el.find('a.toggle-xmpp-contact-form').click();
  963. expect(panel.toggleContactForm).toHaveBeenCalled();
  964. // XXX: Awaiting more tests, close it again for now...
  965. panel.$el.find('a.toggle-xmpp-contact-form').click();
  966. }, converse));
  967. }, converse, mock, test_utils));
  968. describe("The Controlbox Tabs", $.proxy(function () {
  969. beforeEach($.proxy(function () {
  970. runs(function () {
  971. test_utils.closeAllChatBoxes();
  972. });
  973. waits(50);
  974. runs(function () {
  975. test_utils.openControlBox();
  976. });
  977. }, converse));
  978. it("contains two tabs, 'Contacts' and 'ChatRooms'", $.proxy(function () {
  979. var cbview = this.chatboxviews.get('controlbox');
  980. var $panels = cbview.$el.find('.controlbox-panes');
  981. expect($panels.children().length).toBe(2);
  982. expect($panels.children().first().attr('id')).toBe('users');
  983. expect($panels.children().first().is(':visible')).toBe(true);
  984. expect($panels.children().last().attr('id')).toBe('chatrooms');
  985. expect($panels.children().last().is(':visible')).toBe(false);
  986. }, converse));
  987. describe("chatrooms panel", $.proxy(function () {
  988. beforeEach($.proxy(function () {
  989. runs(function () {
  990. test_utils.closeAllChatBoxes();
  991. });
  992. waits(50);
  993. runs(function () {
  994. test_utils.openControlBox();
  995. });
  996. }, converse));
  997. it("is opened by clicking the 'Chatrooms' tab", $.proxy(function () {
  998. var cbview = this.chatboxviews.get('controlbox');
  999. var $tabs = cbview.$el.find('#controlbox-tabs');
  1000. var $panels = cbview.$el.find('.controlbox-panes');
  1001. var $contacts = $panels.children().first();
  1002. var $chatrooms = $panels.children().last();
  1003. spyOn(cbview, 'switchTab').andCallThrough();
  1004. cbview.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  1005. $tabs.find('li').last().find('a').click(); // Clicks the chatrooms tab
  1006. expect($contacts.is(':visible')).toBe(false);
  1007. expect($chatrooms.is(':visible')).toBe(true);
  1008. expect(cbview.switchTab).toHaveBeenCalled();
  1009. }, converse));
  1010. it("contains a form through which a new chatroom can be created", $.proxy(function () {
  1011. var roomspanel = this.chatboxviews.get('controlbox').roomspanel;
  1012. var $input = roomspanel.$el.find('input.new-chatroom-name');
  1013. var $nick = roomspanel.$el.find('input.new-chatroom-nick');
  1014. var $server = roomspanel.$el.find('input.new-chatroom-server');
  1015. expect($input.length).toBe(1);
  1016. expect($server.length).toBe(1);
  1017. expect($('.chatroom:visible').length).toBe(0); // There shouldn't be any chatrooms open currently
  1018. spyOn(roomspanel, 'createChatRoom').andCallThrough();
  1019. roomspanel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  1020. runs(function () {
  1021. $input.val('Lounge');
  1022. $nick.val('dummy');
  1023. $server.val('muc.localhost');
  1024. });
  1025. waits('250');
  1026. runs(function () {
  1027. roomspanel.$el.find('form').submit();
  1028. expect(roomspanel.createChatRoom).toHaveBeenCalled();
  1029. });
  1030. waits('250');
  1031. runs($.proxy(function () {
  1032. expect($('.chatroom:visible').length).toBe(1); // There should now be an open chatroom
  1033. }, converse));
  1034. }, converse));
  1035. it("can list rooms publically available on the server", $.proxy(function () {
  1036. var panel = this.chatboxviews.get('controlbox').roomspanel;
  1037. panel.$tabs.find('li').last().find('a').click(); // Click the chatrooms tab
  1038. panel.model.set({'muc_domain': 'muc.localhost'}); // Make sure the domain is set
  1039. // See: http://xmpp.org/extensions/xep-0045.html#disco-rooms
  1040. expect($('#available-chatrooms').children('dt').length).toBe(0);
  1041. expect($('#available-chatrooms').children('dd').length).toBe(0);
  1042. var iq = $iq({
  1043. from:'muc.localhost',
  1044. to:'dummy@localhost/pda',
  1045. type:'result'
  1046. }).c('query')
  1047. .c('item', { jid:'heath@chat.shakespeare.lit', name:'A Lonely Heath'}).up()
  1048. .c('item', { jid:'coven@chat.shakespeare.lit', name:'A Dark Cave'}).up()
  1049. .c('item', { jid:'forres@chat.shakespeare.lit', name:'The Palace'}).up()
  1050. .c('item', { jid:'inverness@chat.shakespeare.lit', name:'Macbeth&apos;s Castle'}).nodeTree;
  1051. panel.onRoomsFound(iq);
  1052. expect(panel.$('#available-chatrooms').children('dt').length).toBe(1);
  1053. expect(panel.$('#available-chatrooms').children('dt').first().text()).toBe("Rooms on muc.localhost");
  1054. expect(panel.$('#available-chatrooms').children('dd').length).toBe(4);
  1055. }, converse));
  1056. }, converse));
  1057. }, converse, mock, test_utils));
  1058. }));