You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

systemtagsinputfieldSpec.js 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. /**
  2. * ownCloud
  3. *
  4. * @author Vincent Petry
  5. * @copyright 2016 Vincent Petry <pvince81@owncloud.com>
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  9. * License as published by the Free Software Foundation; either
  10. * version 3 of the License, or any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public
  18. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. describe('OC.SystemTags.SystemTagsInputField tests', function() {
  22. var view, select2Stub, clock;
  23. beforeEach(function() {
  24. clock = sinon.useFakeTimers();
  25. var $container = $('<div class="testInputContainer"></div>');
  26. select2Stub = sinon.stub($.fn, 'select2');
  27. select2Stub.returnsThis();
  28. $('#testArea').append($container);
  29. });
  30. afterEach(function() {
  31. select2Stub.restore();
  32. OC.SystemTags.collection.reset();
  33. clock.restore();
  34. view.remove();
  35. view = undefined;
  36. });
  37. describe('general behavior', function() {
  38. var $dropdown;
  39. beforeEach(function() {
  40. view = new OC.SystemTags.SystemTagsInputField();
  41. $('.testInputContainer').append(view.$el);
  42. $dropdown = $('<div class="select2-dropdown"></div>');
  43. select2Stub.withArgs('dropdown').returns($dropdown);
  44. $('#testArea').append($dropdown);
  45. view.render();
  46. });
  47. describe('rendering', function() {
  48. it('calls select2 on rendering', function() {
  49. expect(view.$el.find('input[name=tags]').length).toEqual(1);
  50. expect(select2Stub.called).toEqual(true);
  51. });
  52. it('formatResult renders rename button', function() {
  53. var opts = select2Stub.getCall(0).args[0];
  54. var $el = $(opts.formatResult({id: '1', name: 'test'}));
  55. expect($el.find('.rename').length).toEqual(1);
  56. });
  57. });
  58. describe('tag selection', function() {
  59. beforeEach(function() {
  60. var $el = view.$el.find('input');
  61. $el.val('1');
  62. view.collection.add([
  63. new OC.SystemTags.SystemTagModel({id: '1', name: 'abc'}),
  64. new OC.SystemTags.SystemTagModel({id: '2', name: 'def'}),
  65. new OC.SystemTags.SystemTagModel({id: '3', name: 'abd', userAssignable: false, canAssign: false}),
  66. ]);
  67. });
  68. it('does not create dummy tag when user types non-matching name', function() {
  69. var opts = select2Stub.getCall(0).args[0];
  70. var result = opts.createSearchChoice('abc');
  71. expect(result).not.toBeDefined();
  72. });
  73. it('creates dummy tag when user types non-matching name', function() {
  74. var opts = select2Stub.getCall(0).args[0];
  75. var result = opts.createSearchChoice('abnew');
  76. expect(result.id).toEqual(-1);
  77. expect(result.name).toEqual('abnew');
  78. expect(result.isNew).toEqual(true);
  79. expect(result.userVisible).toEqual(true);
  80. expect(result.userAssignable).toEqual(true);
  81. expect(result.canAssign).toEqual(true);
  82. });
  83. it('creates dummy tag when user types non-matching name even with prefix of existing tag', function() {
  84. var opts = select2Stub.getCall(0).args[0];
  85. var result = opts.createSearchChoice('ab');
  86. expect(result.id).toEqual(-1);
  87. expect(result.name).toEqual('ab');
  88. expect(result.isNew).toEqual(true);
  89. expect(result.userVisible).toEqual(true);
  90. expect(result.userAssignable).toEqual(true);
  91. expect(result.canAssign).toEqual(true);
  92. });
  93. it('creates the real tag and fires select event after user selects the dummy tag', function() {
  94. var selectHandler = sinon.stub();
  95. view.on('select', selectHandler);
  96. var createStub = sinon.stub(OC.SystemTags.SystemTagsCollection.prototype, 'create');
  97. view.$el.find('input').trigger(new $.Event('select2-selecting', {
  98. object: {
  99. id: -1,
  100. name: 'newname',
  101. isNew: true
  102. }
  103. }));
  104. expect(createStub.calledOnce).toEqual(true);
  105. expect(createStub.getCall(0).args[0]).toEqual({
  106. name: 'newname',
  107. userVisible: true,
  108. userAssignable: true,
  109. canAssign: true
  110. });
  111. var newModel = new OC.SystemTags.SystemTagModel({
  112. id: '123',
  113. name: 'newname',
  114. userVisible: true,
  115. userAssignable: true,
  116. canAssign: true
  117. });
  118. // not called yet
  119. expect(selectHandler.notCalled).toEqual(true);
  120. select2Stub.withArgs('data').returns([{
  121. id: '1',
  122. name: 'abc'
  123. }]);
  124. createStub.yieldTo('success', newModel);
  125. expect(select2Stub.lastCall.args[0]).toEqual('data');
  126. expect(select2Stub.lastCall.args[1]).toEqual([{
  127. id: '1',
  128. name: 'abc'
  129. },
  130. newModel.toJSON()
  131. ]);
  132. expect(selectHandler.calledOnce).toEqual(true);
  133. expect(selectHandler.getCall(0).args[0]).toEqual(newModel);
  134. createStub.restore();
  135. });
  136. it('triggers select event after selecting an existing tag', function() {
  137. var selectHandler = sinon.stub();
  138. view.on('select', selectHandler);
  139. view.$el.find('input').trigger(new $.Event('select2-selecting', {
  140. object: {
  141. id: '2',
  142. name: 'def'
  143. }
  144. }));
  145. expect(selectHandler.calledOnce).toEqual(true);
  146. expect(selectHandler.getCall(0).args[0]).toEqual(view.collection.get('2'));
  147. });
  148. it('triggers deselect event after deselecting an existing tag', function() {
  149. var selectHandler = sinon.stub();
  150. view.on('deselect', selectHandler);
  151. view.$el.find('input').trigger(new $.Event('select2-removing', {
  152. choice: {
  153. id: '2',
  154. name: 'def'
  155. }
  156. }));
  157. expect(selectHandler.calledOnce).toEqual(true);
  158. expect(selectHandler.getCall(0).args[0]).toEqual('2');
  159. });
  160. it('triggers select event and still adds to list even in case of conflict', function() {
  161. var selectHandler = sinon.stub();
  162. view.on('select', selectHandler);
  163. var fetchStub = sinon.stub(OC.SystemTags.SystemTagsCollection.prototype, 'fetch');
  164. var createStub = sinon.stub(OC.SystemTags.SystemTagsCollection.prototype, 'create');
  165. view.$el.find('input').trigger(new $.Event('select2-selecting', {
  166. object: {
  167. id: -1,
  168. name: 'newname',
  169. isNew: true
  170. }
  171. }));
  172. expect(createStub.calledOnce).toEqual(true);
  173. expect(createStub.getCall(0).args[0]).toEqual({
  174. name: 'newname',
  175. userVisible: true,
  176. userAssignable: true,
  177. canAssign: true
  178. });
  179. var newModel = new OC.SystemTags.SystemTagModel({
  180. id: '123',
  181. name: 'newname',
  182. userVisible: true,
  183. userAssignable: true
  184. });
  185. // not called yet
  186. expect(selectHandler.notCalled).toEqual(true);
  187. select2Stub.withArgs('data').returns([{
  188. id: '1',
  189. name: 'abc'
  190. }]);
  191. // simulate conflict response for tag creation
  192. createStub.yieldTo('error', view.collection, {status: 409});
  193. // at this point it fetches from the server
  194. expect(fetchStub.calledOnce).toEqual(true);
  195. // simulate fetch result by adding model to the collection
  196. view.collection.add(newModel);
  197. fetchStub.yieldTo('success', view.collection);
  198. expect(select2Stub.lastCall.args[0]).toEqual('data');
  199. expect(select2Stub.lastCall.args[1]).toEqual([{
  200. id: '1',
  201. name: 'abc'
  202. },
  203. newModel.toJSON()
  204. ]);
  205. // select event still called
  206. expect(selectHandler.calledOnce).toEqual(true);
  207. expect(selectHandler.getCall(0).args[0]).toEqual(newModel);
  208. createStub.restore();
  209. fetchStub.restore();
  210. });
  211. });
  212. describe('tag actions', function() {
  213. var opts;
  214. beforeEach(function() {
  215. opts = select2Stub.getCall(0).args[0];
  216. view.collection.add([
  217. new OC.SystemTags.SystemTagModel({id: '1', name: 'abc'}),
  218. ]);
  219. $dropdown.append(opts.formatResult(view.collection.get('1').toJSON()));
  220. });
  221. it('displays rename form when clicking rename', function() {
  222. $dropdown.find('.rename').mouseup();
  223. expect($dropdown.find('form.systemtags-rename-form').length).toEqual(1);
  224. expect($dropdown.find('form.systemtags-rename-form input').val()).toEqual('abc');
  225. });
  226. it('renames model and submits change when submitting form', function() {
  227. var saveStub = sinon.stub(OC.SystemTags.SystemTagModel.prototype, 'save');
  228. $dropdown.find('.rename').mouseup();
  229. $dropdown.find('form input').val('abc_renamed');
  230. $dropdown.find('form').trigger(new $.Event('submit'));
  231. expect(saveStub.calledOnce).toEqual(true);
  232. expect(saveStub.getCall(0).args[0]).toEqual({'name': 'abc_renamed'});
  233. expect($dropdown.find('.label').text()).toEqual('abc_renamed');
  234. expect($dropdown.find('form').length).toEqual(0);
  235. saveStub.restore();
  236. });
  237. });
  238. describe('setting data', function() {
  239. it('sets value when calling setValues', function() {
  240. var vals = ['1', '2'];
  241. view.setValues(vals);
  242. expect(select2Stub.lastCall.args[0]).toEqual('val');
  243. expect(select2Stub.lastCall.args[1]).toEqual(vals);
  244. });
  245. it('sets data when calling setData', function() {
  246. var vals = [{id: '1', name: 'test1'}, {id: '2', name: 'test2'}];
  247. view.setData(vals);
  248. expect(select2Stub.lastCall.args[0]).toEqual('data');
  249. expect(select2Stub.lastCall.args[1]).toEqual(vals);
  250. });
  251. });
  252. });
  253. describe('as admin', function() {
  254. var $dropdown;
  255. beforeEach(function() {
  256. view = new OC.SystemTags.SystemTagsInputField({
  257. isAdmin: true
  258. });
  259. $('.testInputContainer').append(view.$el);
  260. $dropdown = $('<div class="select2-dropdown"></div>');
  261. select2Stub.withArgs('dropdown').returns($dropdown);
  262. $('#testArea').append($dropdown);
  263. view.render();
  264. });
  265. it('formatResult renders tag name with visibility', function() {
  266. var opts = select2Stub.getCall(0).args[0];
  267. var $el = $(opts.formatResult({id: '1', name: 'test', userVisible: false, userAssignable: false}));
  268. expect($el.find('.label').text()).toEqual('test (Invisible)');
  269. });
  270. it('formatSelection renders tag name with visibility', function() {
  271. var opts = select2Stub.getCall(0).args[0];
  272. var $el = $(opts.formatSelection({id: '1', name: 'test', userVisible: false, userAssignable: false}));
  273. expect($el.text().trim()).toEqual('test (Invisible)');
  274. });
  275. describe('initSelection', function() {
  276. var fetchStub;
  277. var testTags;
  278. beforeEach(function() {
  279. fetchStub = sinon.stub(OC.SystemTags.SystemTagsCollection.prototype, 'fetch');
  280. testTags = [
  281. new OC.SystemTags.SystemTagModel({id: '1', name: 'test1'}),
  282. new OC.SystemTags.SystemTagModel({id: '2', name: 'test2'}),
  283. new OC.SystemTags.SystemTagModel({id: '3', name: 'test3', userAssignable: false, canAssign: false}),
  284. new OC.SystemTags.SystemTagModel({id: '4', name: 'test4', userAssignable: false, canAssign: true})
  285. ];
  286. });
  287. afterEach(function() {
  288. fetchStub.restore();
  289. });
  290. it('grabs values from the full collection', function() {
  291. var $el = view.$el.find('input');
  292. $el.val('1,3,4');
  293. var opts = select2Stub.getCall(0).args[0];
  294. var callback = sinon.stub();
  295. opts.initSelection($el, callback);
  296. expect(fetchStub.calledOnce).toEqual(true);
  297. view.collection.add(testTags);
  298. fetchStub.yieldTo('success', view.collection);
  299. expect(callback.calledOnce).toEqual(true);
  300. var models = callback.getCall(0).args[0];
  301. expect(models.length).toEqual(3);
  302. expect(models[0].id).toEqual('1');
  303. expect(models[0].name).toEqual('test1');
  304. expect(models[0].locked).toBeFalsy();
  305. expect(models[1].id).toEqual('3');
  306. expect(models[1].name).toEqual('test3');
  307. expect(models[1].locked).toBeFalsy();
  308. expect(models[2].id).toEqual('4');
  309. expect(models[2].name).toEqual('test4');
  310. expect(models[2].locked).toBeFalsy();
  311. });
  312. });
  313. describe('autocomplete', function() {
  314. var fetchStub, opts;
  315. beforeEach(function() {
  316. fetchStub = sinon.stub(OC.SystemTags.SystemTagsCollection.prototype, 'fetch');
  317. opts = select2Stub.getCall(0).args[0];
  318. view.collection.add([
  319. new OC.SystemTags.SystemTagModel({id: '1', name: 'abc'}),
  320. new OC.SystemTags.SystemTagModel({id: '2', name: 'def'}),
  321. new OC.SystemTags.SystemTagModel({id: '3', name: 'abd', userAssignable: false, canAssign: false}),
  322. new OC.SystemTags.SystemTagModel({id: '4', name: 'Deg'}),
  323. ]);
  324. });
  325. afterEach(function() {
  326. fetchStub.restore();
  327. });
  328. it('completes results', function() {
  329. var callback = sinon.stub();
  330. opts.query({
  331. term: 'ab',
  332. callback: callback
  333. });
  334. expect(fetchStub.calledOnce).toEqual(true);
  335. fetchStub.yieldTo('success', view.collection);
  336. expect(callback.calledOnce).toEqual(true);
  337. expect(callback.getCall(0).args[0].results).toEqual([
  338. {
  339. id: '1',
  340. name: 'abc',
  341. userVisible: true,
  342. userAssignable: true,
  343. canAssign: true
  344. },
  345. {
  346. id: '3',
  347. name: 'abd',
  348. userVisible: true,
  349. userAssignable: false,
  350. canAssign: false
  351. }
  352. ]);
  353. });
  354. it('completes case insensitive', function() {
  355. var callback = sinon.stub();
  356. opts.query({
  357. term: 'de',
  358. callback: callback
  359. });
  360. expect(fetchStub.calledOnce).toEqual(true);
  361. fetchStub.yieldTo('success', view.collection);
  362. expect(callback.calledOnce).toEqual(true);
  363. expect(callback.getCall(0).args[0].results).toEqual([
  364. {
  365. id: '2',
  366. name: 'def',
  367. userVisible: true,
  368. userAssignable: true,
  369. canAssign: true
  370. },
  371. {
  372. id: '4',
  373. name: 'Deg',
  374. userVisible: true,
  375. userAssignable: true,
  376. canAssign: true
  377. }
  378. ]);
  379. });
  380. });
  381. describe('tag actions', function() {
  382. var opts;
  383. beforeEach(function() {
  384. opts = select2Stub.getCall(0).args[0];
  385. view.collection.add([
  386. new OC.SystemTags.SystemTagModel({id: '1', name: 'abc'}),
  387. ]);
  388. $dropdown.append(opts.formatResult(view.collection.get('1').toJSON()));
  389. });
  390. it('deletes model and submits change when clicking delete', function() {
  391. var destroyStub = sinon.stub(OC.SystemTags.SystemTagModel.prototype, 'destroy');
  392. expect($dropdown.find('.delete').length).toEqual(0);
  393. $dropdown.find('.rename').mouseup();
  394. // delete button appears
  395. expect($dropdown.find('.delete').length).toEqual(1);
  396. $dropdown.find('.delete').mouseup();
  397. expect(destroyStub.calledOnce).toEqual(true);
  398. expect(destroyStub.calledOn(view.collection.get('1')));
  399. destroyStub.restore();
  400. });
  401. });
  402. });
  403. describe('as user', function() {
  404. var $dropdown;
  405. beforeEach(function() {
  406. view = new OC.SystemTags.SystemTagsInputField({
  407. isAdmin: false
  408. });
  409. $('.testInputContainer').append(view.$el);
  410. $dropdown = $('<div class="select2-dropdown"></div>');
  411. select2Stub.withArgs('dropdown').returns($dropdown);
  412. $('#testArea').append($dropdown);
  413. view.render();
  414. });
  415. it('formatResult renders tag name only', function() {
  416. var opts = select2Stub.getCall(0).args[0];
  417. var $el = $(opts.formatResult({id: '1', name: 'test'}));
  418. expect($el.find('.label').text()).toEqual('test');
  419. });
  420. it('formatSelection renders tag name only', function() {
  421. var opts = select2Stub.getCall(0).args[0];
  422. var $el = $(opts.formatSelection({id: '1', name: 'test'}));
  423. expect($el.text().trim()).toEqual('test');
  424. });
  425. describe('initSelection', function() {
  426. var fetchStub;
  427. var testTags;
  428. beforeEach(function() {
  429. fetchStub = sinon.stub(OC.SystemTags.SystemTagsCollection.prototype, 'fetch');
  430. testTags = [
  431. new OC.SystemTags.SystemTagModel({id: '1', name: 'test1'}),
  432. new OC.SystemTags.SystemTagModel({id: '2', name: 'test2'}),
  433. new OC.SystemTags.SystemTagModel({id: '3', name: 'test3', userAssignable: false, canAssign: false}),
  434. new OC.SystemTags.SystemTagModel({id: '4', name: 'test4', userAssignable: false, canAssign: true})
  435. ];
  436. view.render();
  437. });
  438. afterEach(function() {
  439. fetchStub.restore();
  440. });
  441. it('grabs values from the full collection', function() {
  442. var $el = view.$el.find('input');
  443. $el.val('1,3,4');
  444. var opts = select2Stub.getCall(0).args[0];
  445. var callback = sinon.stub();
  446. opts.initSelection($el, callback);
  447. expect(fetchStub.calledOnce).toEqual(true);
  448. view.collection.add(testTags);
  449. fetchStub.yieldTo('success', view.collection);
  450. expect(callback.calledOnce).toEqual(true);
  451. var models = callback.getCall(0).args[0];
  452. expect(models.length).toEqual(3);
  453. expect(models[0].id).toEqual('1');
  454. expect(models[0].name).toEqual('test1');
  455. expect(models[0].locked).toBeFalsy();
  456. expect(models[1].id).toEqual('3');
  457. expect(models[1].name).toEqual('test3');
  458. // restricted / cannot assign locks the entry
  459. expect(models[1].locked).toEqual(true);
  460. expect(models[2].id).toEqual('4');
  461. expect(models[2].name).toEqual('test4');
  462. expect(models[2].locked).toBeFalsy();
  463. });
  464. });
  465. describe('autocomplete', function() {
  466. var fetchStub, opts;
  467. beforeEach(function() {
  468. fetchStub = sinon.stub(OC.SystemTags.SystemTagsCollection.prototype, 'fetch');
  469. view.render();
  470. opts = select2Stub.getCall(0).args[0];
  471. view.collection.add([
  472. new OC.SystemTags.SystemTagModel({id: '1', name: 'abc'}),
  473. new OC.SystemTags.SystemTagModel({id: '2', name: 'def'}),
  474. new OC.SystemTags.SystemTagModel({id: '3', name: 'abd', userAssignable: false, canAssign: false}),
  475. new OC.SystemTags.SystemTagModel({id: '4', name: 'Deg'}),
  476. new OC.SystemTags.SystemTagModel({id: '5', name: 'abe', userAssignable: false, canAssign: true})
  477. ]);
  478. });
  479. afterEach(function() {
  480. fetchStub.restore();
  481. });
  482. it('completes results excluding non-assignable tags', function() {
  483. var callback = sinon.stub();
  484. opts.query({
  485. term: 'ab',
  486. callback: callback
  487. });
  488. expect(fetchStub.calledOnce).toEqual(true);
  489. fetchStub.yieldTo('success', view.collection);
  490. expect(callback.calledOnce).toEqual(true);
  491. expect(callback.getCall(0).args[0].results).toEqual([
  492. {
  493. id: '1',
  494. name: 'abc',
  495. userVisible: true,
  496. userAssignable: true,
  497. canAssign: true
  498. },
  499. {
  500. id: '5',
  501. name: 'abe',
  502. userVisible: true,
  503. userAssignable: false,
  504. canAssign: true
  505. }
  506. ]);
  507. });
  508. it('completes case insensitive', function() {
  509. var callback = sinon.stub();
  510. opts.query({
  511. term: 'de',
  512. callback: callback
  513. });
  514. expect(fetchStub.calledOnce).toEqual(true);
  515. fetchStub.yieldTo('success', view.collection);
  516. expect(callback.calledOnce).toEqual(true);
  517. expect(callback.getCall(0).args[0].results).toEqual([
  518. {
  519. id: '2',
  520. name: 'def',
  521. userVisible: true,
  522. userAssignable: true,
  523. canAssign: true
  524. },
  525. {
  526. id: '4',
  527. name: 'Deg',
  528. userVisible: true,
  529. userAssignable: true,
  530. canAssign: true
  531. }
  532. ]);
  533. });
  534. });
  535. describe('tag actions', function() {
  536. var opts;
  537. beforeEach(function() {
  538. opts = select2Stub.getCall(0).args[0];
  539. view.collection.add([
  540. new OC.SystemTags.SystemTagModel({id: '1', name: 'abc'}),
  541. ]);
  542. $dropdown.append(opts.formatResult(view.collection.get('1').toJSON()));
  543. });
  544. it('deletes model and submits change when clicking delete', function() {
  545. var destroyStub = sinon.stub(OC.SystemTags.SystemTagModel.prototype, 'destroy');
  546. expect($dropdown.find('.delete').length).toEqual(0);
  547. $dropdown.find('.rename').mouseup();
  548. // delete button appears only for admins
  549. expect($dropdown.find('.delete').length).toEqual(0);
  550. $dropdown.find('.delete').mouseup();
  551. expect(destroyStub.notCalled).toEqual(true);
  552. destroyStub.restore();
  553. });
  554. });
  555. });
  556. });