123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- /**
- * @copyright 2016 Vincent Petry <pvince81@owncloud.com>
- *
- * @author Daniel Calviño Sánchez <danxuliu@gmail.com>
- * @author John Molakvoæ <skjnldsv@protonmail.com>
- * @author Vincent Petry <vincent@nextcloud.com>
- *
- * @license AGPL-3.0-or-later
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
- describe('OCA.SystemTags.SystemTagsInfoView tests', function() {
- var isAdminStub;
- var view;
- var clock;
- beforeEach(function() {
- clock = sinon.useFakeTimers();
- view = new OCA.SystemTags.SystemTagsInfoView();
- $('#testArea').append(view.$el);
- isAdminStub = sinon.stub(OC, 'isUserAdmin').returns(true);
- });
- afterEach(function() {
- isAdminStub.restore();
- clock.restore();
- view.remove();
- view = undefined;
- });
- describe('rendering', function() {
- it('renders input field view', function() {
- view.render();
- expect(view.$el.find('input[name=tags]').length).toEqual(1);
- });
- it('fetches selected tags then renders when setting file info', function() {
- var fetchStub = sinon.stub(OC.SystemTags.SystemTagsMappingCollection.prototype, 'fetch');
- var setDataStub = sinon.stub(OC.SystemTags.SystemTagsInputField.prototype, 'setData');
- expect(view.$el.hasClass('hidden')).toEqual(false);
- view.setFileInfo({id: '123'});
- expect(view.$el.find('input[name=tags]').length).toEqual(1);
- expect(fetchStub.calledOnce).toEqual(true);
- expect(view.selectedTagsCollection.url())
- .toEqual(OC.linkToRemote('dav') + '/systemtags-relations/files/123');
- view.selectedTagsCollection.add([
- {id: '1', name: 'test1'},
- {id: '3', name: 'test3'}
- ]);
- fetchStub.yieldTo('success', view.selectedTagsCollection);
- expect(setDataStub.calledOnce).toEqual(true);
- expect(setDataStub.getCall(0).args[0]).toEqual([{
- id: '1', name: 'test1', userVisible: true, userAssignable: true, canAssign: true
- }, {
- id: '3', name: 'test3', userVisible: true, userAssignable: true, canAssign: true
- }]);
- expect(view.$el.hasClass('hidden')).toEqual(false);
- fetchStub.restore();
- setDataStub.restore();
- });
- it('overrides initSelection to use the local collection', function() {
- var inputViewSpy = sinon.spy(OC.SystemTags, 'SystemTagsInputField');
- var element = $('<input type="hidden" val="1,3"/>');
- view.remove();
- view = new OCA.SystemTags.SystemTagsInfoView();
- view.selectedTagsCollection.add([
- {id: '1', name: 'test1'},
- {id: '3', name: 'test3', userVisible: false, userAssignable: false, canAssign: false}
- ]);
- var callback = sinon.stub();
- inputViewSpy.getCall(0).args[0].initSelection(element, callback);
- expect(callback.calledOnce).toEqual(true);
- expect(callback.getCall(0).args[0]).toEqual([{
- id: '1', name: 'test1', userVisible: true, userAssignable: true, canAssign: true
- }, {
- id: '3', name: 'test3', userVisible: false, userAssignable: false, canAssign: false
- }]);
- inputViewSpy.restore();
- });
- it('sets locked flag on non-assignable tags when user is not an admin', function() {
- isAdminStub.returns(false);
- var inputViewSpy = sinon.spy(OC.SystemTags, 'SystemTagsInputField');
- var element = $('<input type="hidden" val="1,3"/>');
- view.remove();
- view = new OCA.SystemTags.SystemTagsInfoView();
- view.selectedTagsCollection.add([
- {id: '1', name: 'test1'},
- {id: '3', name: 'test3', userAssignable: false, canAssign: false}
- ]);
- var callback = sinon.stub();
- inputViewSpy.getCall(0).args[0].initSelection(element, callback);
- expect(callback.calledOnce).toEqual(true);
- expect(callback.getCall(0).args[0]).toEqual([{
- id: '1', name: 'test1', userVisible: true, userAssignable: true, canAssign: true
- }, {
- id: '3', name: 'test3', userVisible: true, userAssignable: false, canAssign: false, locked: true
- }]);
- inputViewSpy.restore();
- });
- it('does not set locked flag on non-assignable tags when canAssign overrides it with true', function() {
- isAdminStub.returns(false);
- var inputViewSpy = sinon.spy(OC.SystemTags, 'SystemTagsInputField');
- var element = $('<input type="hidden" val="1,4"/>');
- view.remove();
- view = new OCA.SystemTags.SystemTagsInfoView();
- view.selectedTagsCollection.add([
- {id: '1', name: 'test1'},
- {id: '4', name: 'test4', userAssignable: false, canAssign: true}
- ]);
- var callback = sinon.stub();
- inputViewSpy.getCall(0).args[0].initSelection(element, callback);
- expect(callback.calledOnce).toEqual(true);
- expect(callback.getCall(0).args[0]).toEqual([{
- id: '1', name: 'test1', userVisible: true, userAssignable: true, canAssign: true
- }, {
- id: '4', name: 'test4', userVisible: true, userAssignable: false, canAssign: true
- }]);
- inputViewSpy.restore();
- });
- });
- describe('events', function() {
- var allTagsCollection;
- beforeEach(function() {
- allTagsCollection = view._inputView.collection;
- allTagsCollection.add([
- {id: '1', name: 'test1'},
- {id: '2', name: 'test2'},
- {id: '3', name: 'test3'}
- ]);
- view.selectedTagsCollection.add([
- {id: '1', name: 'test1'},
- {id: '3', name: 'test3'}
- ]);
- view.render();
- });
- it('renames model in selection collection on rename', function() {
- allTagsCollection.get('3').set('name', 'test3_renamed');
- expect(view.selectedTagsCollection.get('3').get('name')).toEqual('test3_renamed');
- });
- it('adds tag to selection collection when selected by input', function() {
- var createStub = sinon.stub(OC.SystemTags.SystemTagsMappingCollection.prototype, 'create');
- view._inputView.trigger('select', allTagsCollection.get('2'));
- expect(createStub.calledOnce).toEqual(true);
- expect(createStub.getCall(0).args[0]).toEqual({
- id: '2',
- name: 'test2',
- userVisible: true,
- userAssignable: true,
- canAssign: true
- });
- createStub.restore();
- });
- it('removes tag from selection collection when deselected by input', function() {
- var destroyStub = sinon.stub(OC.SystemTags.SystemTagModel.prototype, 'destroy');
- view._inputView.trigger('deselect', '3');
- expect(destroyStub.calledOnce).toEqual(true);
- expect(destroyStub.calledOn(view.selectedTagsCollection.get('3'))).toEqual(true);
- destroyStub.restore();
- });
- it('removes tag from selection whenever the tag was deleted globally', function() {
- expect(view.selectedTagsCollection.get('3')).not.toBeFalsy();
- allTagsCollection.remove('3');
- expect(view.selectedTagsCollection.get('3')).toBeFalsy();
- });
- });
- describe('visibility', function() {
- it('reports visibility based on the "hidden" class name', function() {
- view.$el.addClass('hidden');
- expect(view.isVisible()).toBeFalsy();
- view.$el.removeClass('hidden');
- expect(view.isVisible()).toBeTruthy();
- });
- it('is visible after rendering', function() {
- view.render();
- expect(view.isVisible()).toBeTruthy();
- });
- it('shows and hides the element', function() {
- view.show();
- expect(view.isVisible()).toBeTruthy();
- view.hide();
- expect(view.isVisible()).toBeFalsy();
- view.show();
- expect(view.isVisible()).toBeTruthy();
- });
- });
- describe('select2', function() {
- var select2Stub;
- beforeEach(function() {
- select2Stub = sinon.stub($.fn, 'select2');
- });
- afterEach(function() {
- select2Stub.restore();
- });
- it('opens dropdown', function() {
- view.openDropdown();
- expect(select2Stub.calledOnce).toBeTruthy();
- expect(select2Stub.withArgs('open')).toBeTruthy();
- });
- });
- });
|