Welcome to mirror list, hosted at ThFree Co, Russian Federation.

systemtagsfilelistSpec.js « js « tests « systemtags « apps - github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 068eb0cbf5c1d8f4c6bad1491fa194abbbb4fb62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/**
 * Copyright (c) 2016 Vincent Petry <pvince81@owncloud.com>
 *
 * @author Christoph Wurst <christoph@winzerhof-wurst.at>
 * @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.FileList tests', function() {
	var FileInfo = OC.Files.FileInfo;
	var fileList;

	beforeEach(function() {
		// init parameters and test table elements
		$('#testArea').append(
			'<div id="app-content-container">' +
			// init horrible parameters
			'<input type="hidden" id="dir" value="/"></input>' +
			'<input type="hidden" id="permissions" value="31"></input>' +
			'<div class="files-controls"></div>' +
			// dummy table
			// TODO: at some point this will be rendered by the fileList class itself!
			'<table class="files-filestable">' +
			'<thead><tr>' +
			'<th class="hidden column-name">' +
			'<input type="checkbox" id="select_all_files" class="select-all">' +
			'<a class="name columntitle" data-sort="name"><span>Name</span><span class="sort-indicator"></span></a>' +
			'<span class="selectedActions hidden">' +
			'</th>' +
			'<th class="hidden column-mtime">' +
			'<a class="columntitle" data-sort="mtime"><span class="sort-indicator"></span></a>' +
			'</th>' +
			'</tr></thead>' +
			'<tbody class="files-fileList"></tbody>' +
			'<tfoot></tfoot>' +
			'</table>' +
			'<div class="emptyfilelist emptycontent">Empty content message</div>' +
			'</div>'
		);
	});
	afterEach(function() {
		fileList.destroy();
		fileList = undefined;
	});

	describe('filter field', function() {
		var select2Stub, oldCollection, fetchTagsStub;
		var $tagsField;

		beforeEach(function() {
			fetchTagsStub = sinon.stub(OC.SystemTags.SystemTagsCollection.prototype, 'fetch');
			select2Stub = sinon.stub($.fn, 'select2');
			oldCollection = OC.SystemTags.collection;
			OC.SystemTags.collection = new OC.SystemTags.SystemTagsCollection([
				{
					id: '123',
					name: 'abc'
				},
				{
					id: '456',
					name: 'def'
				}
			]);

			fileList = new OCA.SystemTags.FileList(
				$('#app-content-container'), {
					systemTagIds: []
				}
			);
			$tagsField = fileList.$el.find('[name=tags]');
		});
		afterEach(function() {
			select2Stub.restore();
			fetchTagsStub.restore();
			OC.SystemTags.collection = oldCollection;
		});
		it('inits select2 on filter field', function() {
			expect(select2Stub.calledOnce).toEqual(true);
		});
		it('uses global system tags collection', function() {
			var callback = sinon.stub();
			var opts = select2Stub.firstCall.args[0];

			$tagsField.val('123');

			opts.initSelection($tagsField, callback);

			expect(callback.notCalled).toEqual(true);
			expect(fetchTagsStub.calledOnce).toEqual(true);

			fetchTagsStub.yieldTo('success', fetchTagsStub.thisValues[0]);

			expect(callback.calledOnce).toEqual(true);
			expect(callback.lastCall.args[0]).toEqual([
				OC.SystemTags.collection.get('123').toJSON()
			]);
		});
		it('fetches tag list from the global collection', function() {
			var callback = sinon.stub();
			var opts = select2Stub.firstCall.args[0];

			$tagsField.val('123');

			opts.query({
				term: 'de',
				callback: callback
			});

			expect(fetchTagsStub.calledOnce).toEqual(true);
			expect(callback.notCalled).toEqual(true);
			fetchTagsStub.yieldTo('success', fetchTagsStub.thisValues[0]);

			expect(callback.calledOnce).toEqual(true);
			expect(callback.lastCall.args[0]).toEqual({
				results: [
					OC.SystemTags.collection.get('456').toJSON()
				]
			});
		});
		it('reloads file list after selection', function() {
			var reloadStub = sinon.stub(fileList, 'reload');
			$tagsField.val('456,123').change();
			expect(reloadStub.calledOnce).toEqual(true);
			reloadStub.restore();
		});
		it('updates URL after selection', function() {
			var handler = sinon.stub();
			fileList.$el.on('changeDirectory', handler);
			$tagsField.val('456,123').change();

			expect(handler.calledOnce).toEqual(true);
			expect(handler.lastCall.args[0].dir).toEqual('456/123');
		});
		it('updates tag selection when url changed', function() {
			fileList.$el.trigger(new $.Event('urlChanged', {dir: '456/123'}));

			expect(select2Stub.lastCall.args[0]).toEqual('val');
			expect(select2Stub.lastCall.args[1]).toEqual(['456', '123']);
		});
	});

	describe('loading results', function() {
		var getFilteredFilesSpec, requestDeferred;

		beforeEach(function() {
			requestDeferred = new $.Deferred();
			getFilteredFilesSpec = sinon.stub(OC.Files.Client.prototype, 'getFilteredFiles')
				.returns(requestDeferred.promise());
		});
		afterEach(function() {
			getFilteredFilesSpec.restore();
		});

		it('renders empty message when no tags were set', function() {
			fileList = new OCA.SystemTags.FileList(
				$('#app-content-container'), {
					systemTagIds: []
				}
			);

			fileList.reload();

			expect(fileList.$el.find('.emptyfilelist.emptycontent').hasClass('hidden')).toEqual(false);

			expect(getFilteredFilesSpec.notCalled).toEqual(true);
		});

		it('render files', function(done) {
			fileList = new OCA.SystemTags.FileList(
				$('#app-content-container'), {
					systemTagIds: ['123', '456']
				}
			);

			var reloading = fileList.reload();

			expect(getFilteredFilesSpec.calledOnce).toEqual(true);
			expect(getFilteredFilesSpec.lastCall.args[0].systemTagIds).toEqual(['123', '456']);

			var testFiles = [new FileInfo({
				id: 1,
				type: 'file',
				name: 'One.txt',
				mimetype: 'text/plain',
				mtime: 123456789,
				size: 12,
				etag: 'abc',
				permissions: OC.PERMISSION_ALL
			}), new FileInfo({
				id: 2,
				type: 'file',
				name: 'Two.jpg',
				mimetype: 'image/jpeg',
				mtime: 234567890,
				size: 12049,
				etag: 'def',
				permissions: OC.PERMISSION_ALL
			}), new FileInfo({
				id: 3,
				type: 'file',
				name: 'Three.pdf',
				mimetype: 'application/pdf',
				mtime: 234560000,
				size: 58009,
				etag: '123',
				permissions: OC.PERMISSION_ALL
			}), new FileInfo({
				id: 4,
				type: 'dir',
				name: 'somedir',
				mimetype: 'httpd/unix-directory',
				mtime: 134560000,
				size: 250,
				etag: '456',
				permissions: OC.PERMISSION_ALL
			})];

			requestDeferred.resolve(207, testFiles);

			return reloading.then(function() {
				expect(fileList.$el.find('.emptyfilelist.emptycontent').hasClass('hidden')).toEqual(true);
				expect(fileList.$el.find('tbody>tr').length).toEqual(4);
			}).then(done, done);
		});
	});
});