/
resource-upload-field.spec.js
290 lines (225 loc) · 8.77 KB
/
resource-upload-field.spec.js
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*globals describe beforeEach afterEach it assert sinon ckan jQuery */
describe('ckan.modules.ResourceUploadFieldModule()', function () {
var ResourceFileUploadModule = ckan.module.registry['resource-upload-field'];
beforeEach(function () {
jQuery.fn.fileupload = sinon.spy();
this.el = jQuery('<form />');
this.sandbox = ckan.sandbox();
this.module = new ResourceFileUploadModule(this.el, {}, this.sandbox);
this.module.initialize();
});
afterEach(function () {
this.module.teardown();
});
describe('.initialize()', function () {
beforeEach(function () {
// Create un-initialised module.
this.module.teardown();
this.module = new ResourceFileUploadModule(this.el, {}, this.sandbox);
});
it('should create the #upload field', function () {
this.module.initialize();
assert.ok(typeof this.module.upload === 'object');
});
it('should append the upload field to the module element', function () {
this.module.initialize();
assert.ok(jQuery.contains(this.el[0], this.module.upload[0]));
});
it('should call .setupFileUpload()', function () {
var target = sinon.stub(this.module, 'setupFileUpload');
this.module.initialize();
assert.called(target);
});
});
describe('.setupFileUpload()', function () {
it('should set the label text on the form input', function () {
this.module.initialize();
this.module.setupFileUpload();
assert.equal(this.module.upload.find('label').text(), 'Upload a file');
});
it('should setup the file upload with relevant options', function () {
this.module.initialize();
this.module.setupFileUpload();
assert.called(jQuery.fn.fileupload);
assert.calledWith(jQuery.fn.fileupload, {
type: 'POST',
paramName: 'file',
forceIframeTransport: true, // Required for XDomain request.
replaceFileInput: true,
autoUpload: false,
add: this.module._onUploadAdd,
send: this.module._onUploadSend,
done: this.module._onUploadDone,
fail: this.module._onUploadFail,
always: this.module._onUploadComplete
});
});
});
describe('.loading(show)', function () {
it('should add a loading class to the upload element', function () {
this.module.loading();
assert.ok(this.module.upload.hasClass('loading'));
});
it('should remove the loading class if false is passed as an argument', function () {
this.module.upload.addClass('loading');
this.module.loading();
assert.ok(!this.module.upload.hasClass('loading'));
});
});
describe('.authenticate(key, data)', function () {
beforeEach(function () {
this.fakeThen = sinon.spy();
this.fakeProxy = sinon.stub(jQuery, 'proxy').returns('onsuccess');
this.target = sinon.stub(this.sandbox.client, 'getStorageAuth');
this.target.returns({
then: this.fakeThen
});
});
afterEach(function () {
jQuery.proxy.restore();
});
it('should request authentication for the upload', function () {
this.module.authenticate('test', {});
assert.called(this.target);
assert.calledWith(this.target, 'test');
});
it('should register success and error callbacks', function () {
this.module.authenticate('test', {});
assert.called(this.fakeThen);
assert.calledWith(this.fakeThen, 'onsuccess', this.module._onAuthError);
});
it('should save the key on the data object', function () {
var data = {};
this.module.authenticate('test', data);
assert.equal(data.key, 'test');
});
});
describe('.lookupMetadata(key, data)', function () {
beforeEach(function () {
this.fakeThen = sinon.spy();
this.fakeProxy = sinon.stub(jQuery, 'proxy').returns('onsuccess');
this.target = sinon.stub(this.sandbox.client, 'getStorageMetadata');
this.target.returns({
then: this.fakeThen
});
});
afterEach(function () {
jQuery.proxy.restore();
});
it('should request metadata for the upload key', function () {
this.module.lookupMetadata('test', {});
assert.called(this.target);
assert.calledWith(this.target, 'test');
});
it('should register success and error callbacks', function () {
this.module.lookupMetadata('test', {});
assert.called(this.fakeThen);
assert.calledWith(this.fakeThen, 'onsuccess', this.module._onMetadataError);
});
});
describe('.notify(message, type)', function () {
it('should call the sandbox.notify() method', function () {
var target = sinon.stub(this.sandbox, 'notify');
this.module.notify('this is an example message', 'info');
assert.called(target);
assert.calledWith(target, 'An Error Occurred', 'this is an example message', 'info');
});
});
describe('.generateKey(file)', function () {
it('should generate a unique filename prefixed with a timestamp', function () {
var now = new Date();
var date = jQuery.date.toISOString(now);
var clock = sinon.useFakeTimers(now.getTime());
var target = this.module.generateKey('this is my file.png');
assert.equal(target, date + '/this-is-my-file.png');
clock.restore();
});
});
describe('._onUploadAdd(event, data)', function () {
beforeEach(function () {
this.target = sinon.stub(this.module, 'authenticate');
sinon.stub(this.module, 'generateKey').returns('stubbed');
});
it('should authenticate the upload if a file is provided', function () {
var data = {files: [{name: 'my_file.jpg'}]};
this.module._onUploadAdd({}, data);
assert.called(this.target);
assert.calledWith(this.target, 'stubbed', data);
});
it('should not authenticate the upload if no file is provided', function () {
var data = {files: []};
this.module._onUploadAdd({}, data);
assert.notCalled(this.target);
});
});
describe('._onUploadSend()', function () {
it('should display the loading spinner', function () {
var target = sinon.stub(this.module, 'loading');
this.module._onUploadSend({}, {});
assert.called(target);
});
});
describe('._onUploadDone()', function () {
it('should request the metadata for the file', function () {
var target = sinon.stub(this.module, 'lookupMetadata');
this.module._onUploadDone({}, {result: {}});
assert.called(target);
});
it('should call the fail handler if the "result" key in the data is undefined', function () {
var target = sinon.stub(this.module, '_onUploadFail');
this.module._onUploadDone({}, {result: undefined});
assert.called(target);
});
it('should call the fail handler if the "result" object has an "error" key', function () {
var target = sinon.stub(this.module, '_onUploadFail');
this.module._onUploadDone({}, {result: {error: 'failed'}});
assert.called(target);
});
});
describe('._onUploadComplete()', function () {
it('should hide the loading spinner', function () {
var target = sinon.stub(this.module, 'loading');
this.module._onUploadComplete({}, {});
assert.called(target);
assert.calledWith(target, false);
});
});
describe('._onAuthSuccess()', function () {
beforeEach(function () {
this.target = {
submit: sinon.spy()
};
this.response = {
action: 'action',
fields: [{name: 'name', value: 'value'}]
};
});
it('should set the data url', function () {
this.module._onAuthSuccess(this.target, this.response);
assert.equal(this.target.url, this.response.action);
});
it('should set the additional form data', function () {
this.module._onAuthSuccess(this.target, this.response);
assert.deepEqual(this.target.formData, this.response.fields);
});
it('should merge the form data with the options', function () {
this.module.options.form.params = [{name: 'option', value: 'option'}];
this.module._onAuthSuccess(this.target, this.response);
assert.deepEqual(this.target.formData, [{name: 'option', value: 'option'}, {name: 'name', value: 'value'}]);
});
it('should call data.submit()', function () {
this.module._onAuthSuccess(this.target, this.response);
assert.called(this.target.submit);
});
});
describe('._onMetadataSuccess()', function () {
it('should publish the "resource:uploaded" event', function () {
var resource = {url: 'http://', name: 'My File'};
var target = sinon.stub(this.sandbox, 'publish');
sinon.stub(this.sandbox.client, 'convertStorageMetadataToResource').returns(resource);
this.module._onMetadataSuccess();
assert.called(target);
assert.calledWith(target, "resource:uploaded", resource);
});
});
});