2015-11-30 17:40:17 +03:00
|
|
|
/**
|
|
|
|
* ownCloud
|
|
|
|
*
|
|
|
|
* @author Vincent Petry
|
|
|
|
* @copyright 2016 Vincent Petry <pvince81@owncloud.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 3 of the License, or any later version.
|
|
|
|
*
|
|
|
|
* This library 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 library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
describe('OCA.SystemTags.SystemTagsInfoView tests', function() {
|
2016-01-21 17:23:49 +03:00
|
|
|
var isAdminStub;
|
2015-11-30 17:40:17 +03:00
|
|
|
var view;
|
2016-02-22 19:19:08 +03:00
|
|
|
var clock;
|
2015-11-30 17:40:17 +03:00
|
|
|
|
|
|
|
beforeEach(function() {
|
2016-02-22 19:19:08 +03:00
|
|
|
clock = sinon.useFakeTimers();
|
2015-11-30 17:40:17 +03:00
|
|
|
view = new OCA.SystemTags.SystemTagsInfoView();
|
|
|
|
$('#testArea').append(view.$el);
|
2016-01-21 17:23:49 +03:00
|
|
|
isAdminStub = sinon.stub(OC, 'isUserAdmin').returns(true);
|
2015-11-30 17:40:17 +03:00
|
|
|
});
|
|
|
|
afterEach(function() {
|
2016-01-21 17:23:49 +03:00
|
|
|
isAdminStub.restore();
|
2016-02-22 19:19:08 +03:00
|
|
|
clock.restore();
|
2015-11-30 17:40:17 +03:00
|
|
|
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');
|
|
|
|
|
2019-10-16 22:46:31 +03:00
|
|
|
expect(view.$el.hasClass('hidden')).toEqual(false);
|
2015-11-30 17:40:17 +03:00
|
|
|
|
|
|
|
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([{
|
2016-05-11 17:47:33 +03:00
|
|
|
id: '1', name: 'test1', userVisible: true, userAssignable: true, canAssign: true
|
2015-11-30 17:40:17 +03:00
|
|
|
}, {
|
2016-05-11 17:47:33 +03:00
|
|
|
id: '3', name: 'test3', userVisible: true, userAssignable: true, canAssign: true
|
2015-11-30 17:40:17 +03:00
|
|
|
}]);
|
|
|
|
|
|
|
|
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'},
|
2016-05-11 17:47:33 +03:00
|
|
|
{id: '3', name: 'test3', userVisible: false, userAssignable: false, canAssign: false}
|
2015-11-30 17:40:17 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
var callback = sinon.stub();
|
|
|
|
inputViewSpy.getCall(0).args[0].initSelection(element, callback);
|
|
|
|
|
|
|
|
expect(callback.calledOnce).toEqual(true);
|
|
|
|
expect(callback.getCall(0).args[0]).toEqual([{
|
2016-05-11 17:47:33 +03:00
|
|
|
id: '1', name: 'test1', userVisible: true, userAssignable: true, canAssign: true
|
2015-11-30 17:40:17 +03:00
|
|
|
}, {
|
2016-05-11 17:47:33 +03:00
|
|
|
id: '3', name: 'test3', userVisible: false, userAssignable: false, canAssign: false
|
2016-01-21 17:23:49 +03:00
|
|
|
}]);
|
|
|
|
|
|
|
|
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'},
|
2016-05-11 17:47:33 +03:00
|
|
|
{id: '3', name: 'test3', userAssignable: false, canAssign: false}
|
2016-01-21 17:23:49 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
var callback = sinon.stub();
|
|
|
|
inputViewSpy.getCall(0).args[0].initSelection(element, callback);
|
|
|
|
|
|
|
|
expect(callback.calledOnce).toEqual(true);
|
|
|
|
expect(callback.getCall(0).args[0]).toEqual([{
|
2016-05-11 17:47:33 +03:00
|
|
|
id: '1', name: 'test1', userVisible: true, userAssignable: true, canAssign: true
|
2016-01-21 17:23:49 +03:00
|
|
|
}, {
|
2016-05-11 17:47:33 +03:00
|
|
|
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
|
2015-11-30 17:40:17 +03:00
|
|
|
}]);
|
|
|
|
|
|
|
|
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,
|
2016-05-11 17:47:33 +03:00
|
|
|
userAssignable: true,
|
|
|
|
canAssign: true
|
2015-11-30 17:40:17 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
2017-06-09 04:34:56 +03:00
|
|
|
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();
|
|
|
|
});
|
2019-10-16 22:46:31 +03:00
|
|
|
it('is visible after rendering', function() {
|
2017-06-09 04:34:56 +03:00
|
|
|
view.render();
|
|
|
|
|
2019-10-16 22:46:31 +03:00
|
|
|
expect(view.isVisible()).toBeTruthy();
|
2017-06-09 04:34:56 +03:00
|
|
|
});
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
2015-11-30 17:40:17 +03:00
|
|
|
});
|