controlbox.js 58 KB

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