systemtagsinfoviewSpec.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /**
  2. * @copyright 2016 Vincent Petry <pvince81@owncloud.com>
  3. *
  4. * @author Daniel Calviño Sánchez <danxuliu@gmail.com>
  5. * @author John Molakvoæ <skjnldsv@protonmail.com>
  6. * @author Vincent Petry <vincent@nextcloud.com>
  7. *
  8. * @license AGPL-3.0-or-later
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. describe('OCA.SystemTags.SystemTagsInfoView tests', function() {
  25. var isAdminStub;
  26. var view;
  27. var clock;
  28. beforeEach(function() {
  29. clock = sinon.useFakeTimers();
  30. view = new OCA.SystemTags.SystemTagsInfoView();
  31. $('#testArea').append(view.$el);
  32. isAdminStub = sinon.stub(OC, 'isUserAdmin').returns(true);
  33. });
  34. afterEach(function() {
  35. isAdminStub.restore();
  36. clock.restore();
  37. view.remove();
  38. view = undefined;
  39. });
  40. describe('rendering', function() {
  41. it('renders input field view', function() {
  42. view.render();
  43. expect(view.$el.find('input[name=tags]').length).toEqual(1);
  44. });
  45. it('fetches selected tags then renders when setting file info', function() {
  46. var fetchStub = sinon.stub(OC.SystemTags.SystemTagsMappingCollection.prototype, 'fetch');
  47. var setDataStub = sinon.stub(OC.SystemTags.SystemTagsInputField.prototype, 'setData');
  48. expect(view.$el.hasClass('hidden')).toEqual(false);
  49. view.setFileInfo({id: '123'});
  50. expect(view.$el.find('input[name=tags]').length).toEqual(1);
  51. expect(fetchStub.calledOnce).toEqual(true);
  52. expect(view.selectedTagsCollection.url())
  53. .toEqual(OC.linkToRemote('dav') + '/systemtags-relations/files/123');
  54. view.selectedTagsCollection.add([
  55. {id: '1', name: 'test1'},
  56. {id: '3', name: 'test3'}
  57. ]);
  58. fetchStub.yieldTo('success', view.selectedTagsCollection);
  59. expect(setDataStub.calledOnce).toEqual(true);
  60. expect(setDataStub.getCall(0).args[0]).toEqual([{
  61. id: '1', name: 'test1', userVisible: true, userAssignable: true, canAssign: true
  62. }, {
  63. id: '3', name: 'test3', userVisible: true, userAssignable: true, canAssign: true
  64. }]);
  65. expect(view.$el.hasClass('hidden')).toEqual(false);
  66. fetchStub.restore();
  67. setDataStub.restore();
  68. });
  69. it('overrides initSelection to use the local collection', function() {
  70. var inputViewSpy = sinon.spy(OC.SystemTags, 'SystemTagsInputField');
  71. var element = $('<input type="hidden" val="1,3"/>');
  72. view.remove();
  73. view = new OCA.SystemTags.SystemTagsInfoView();
  74. view.selectedTagsCollection.add([
  75. {id: '1', name: 'test1'},
  76. {id: '3', name: 'test3', userVisible: false, userAssignable: false, canAssign: false}
  77. ]);
  78. var callback = sinon.stub();
  79. inputViewSpy.getCall(0).args[0].initSelection(element, callback);
  80. expect(callback.calledOnce).toEqual(true);
  81. expect(callback.getCall(0).args[0]).toEqual([{
  82. id: '1', name: 'test1', userVisible: true, userAssignable: true, canAssign: true
  83. }, {
  84. id: '3', name: 'test3', userVisible: false, userAssignable: false, canAssign: false
  85. }]);
  86. inputViewSpy.restore();
  87. });
  88. it('sets locked flag on non-assignable tags when user is not an admin', function() {
  89. isAdminStub.returns(false);
  90. var inputViewSpy = sinon.spy(OC.SystemTags, 'SystemTagsInputField');
  91. var element = $('<input type="hidden" val="1,3"/>');
  92. view.remove();
  93. view = new OCA.SystemTags.SystemTagsInfoView();
  94. view.selectedTagsCollection.add([
  95. {id: '1', name: 'test1'},
  96. {id: '3', name: 'test3', userAssignable: false, canAssign: false}
  97. ]);
  98. var callback = sinon.stub();
  99. inputViewSpy.getCall(0).args[0].initSelection(element, callback);
  100. expect(callback.calledOnce).toEqual(true);
  101. expect(callback.getCall(0).args[0]).toEqual([{
  102. id: '1', name: 'test1', userVisible: true, userAssignable: true, canAssign: true
  103. }, {
  104. id: '3', name: 'test3', userVisible: true, userAssignable: false, canAssign: false, locked: true
  105. }]);
  106. inputViewSpy.restore();
  107. });
  108. it('does not set locked flag on non-assignable tags when canAssign overrides it with true', function() {
  109. isAdminStub.returns(false);
  110. var inputViewSpy = sinon.spy(OC.SystemTags, 'SystemTagsInputField');
  111. var element = $('<input type="hidden" val="1,4"/>');
  112. view.remove();
  113. view = new OCA.SystemTags.SystemTagsInfoView();
  114. view.selectedTagsCollection.add([
  115. {id: '1', name: 'test1'},
  116. {id: '4', name: 'test4', userAssignable: false, canAssign: true}
  117. ]);
  118. var callback = sinon.stub();
  119. inputViewSpy.getCall(0).args[0].initSelection(element, callback);
  120. expect(callback.calledOnce).toEqual(true);
  121. expect(callback.getCall(0).args[0]).toEqual([{
  122. id: '1', name: 'test1', userVisible: true, userAssignable: true, canAssign: true
  123. }, {
  124. id: '4', name: 'test4', userVisible: true, userAssignable: false, canAssign: true
  125. }]);
  126. inputViewSpy.restore();
  127. });
  128. });
  129. describe('events', function() {
  130. var allTagsCollection;
  131. beforeEach(function() {
  132. allTagsCollection = view._inputView.collection;
  133. allTagsCollection.add([
  134. {id: '1', name: 'test1'},
  135. {id: '2', name: 'test2'},
  136. {id: '3', name: 'test3'}
  137. ]);
  138. view.selectedTagsCollection.add([
  139. {id: '1', name: 'test1'},
  140. {id: '3', name: 'test3'}
  141. ]);
  142. view.render();
  143. });
  144. it('renames model in selection collection on rename', function() {
  145. allTagsCollection.get('3').set('name', 'test3_renamed');
  146. expect(view.selectedTagsCollection.get('3').get('name')).toEqual('test3_renamed');
  147. });
  148. it('adds tag to selection collection when selected by input', function() {
  149. var createStub = sinon.stub(OC.SystemTags.SystemTagsMappingCollection.prototype, 'create');
  150. view._inputView.trigger('select', allTagsCollection.get('2'));
  151. expect(createStub.calledOnce).toEqual(true);
  152. expect(createStub.getCall(0).args[0]).toEqual({
  153. id: '2',
  154. name: 'test2',
  155. userVisible: true,
  156. userAssignable: true,
  157. canAssign: true
  158. });
  159. createStub.restore();
  160. });
  161. it('removes tag from selection collection when deselected by input', function() {
  162. var destroyStub = sinon.stub(OC.SystemTags.SystemTagModel.prototype, 'destroy');
  163. view._inputView.trigger('deselect', '3');
  164. expect(destroyStub.calledOnce).toEqual(true);
  165. expect(destroyStub.calledOn(view.selectedTagsCollection.get('3'))).toEqual(true);
  166. destroyStub.restore();
  167. });
  168. it('removes tag from selection whenever the tag was deleted globally', function() {
  169. expect(view.selectedTagsCollection.get('3')).not.toBeFalsy();
  170. allTagsCollection.remove('3');
  171. expect(view.selectedTagsCollection.get('3')).toBeFalsy();
  172. });
  173. });
  174. describe('visibility', function() {
  175. it('reports visibility based on the "hidden" class name', function() {
  176. view.$el.addClass('hidden');
  177. expect(view.isVisible()).toBeFalsy();
  178. view.$el.removeClass('hidden');
  179. expect(view.isVisible()).toBeTruthy();
  180. });
  181. it('is visible after rendering', function() {
  182. view.render();
  183. expect(view.isVisible()).toBeTruthy();
  184. });
  185. it('shows and hides the element', function() {
  186. view.show();
  187. expect(view.isVisible()).toBeTruthy();
  188. view.hide();
  189. expect(view.isVisible()).toBeFalsy();
  190. view.show();
  191. expect(view.isVisible()).toBeTruthy();
  192. });
  193. });
  194. describe('select2', function() {
  195. var select2Stub;
  196. beforeEach(function() {
  197. select2Stub = sinon.stub($.fn, 'select2');
  198. });
  199. afterEach(function() {
  200. select2Stub.restore();
  201. });
  202. it('opens dropdown', function() {
  203. view.openDropdown();
  204. expect(select2Stub.calledOnce).toBeTruthy();
  205. expect(select2Stub.withArgs('open')).toBeTruthy();
  206. });
  207. });
  208. });