Skip to content

Commit 3fee23a

Browse files
committed
Split into separate test
1 parent 59ed527 commit 3fee23a

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

spec/FilesController.spec.js

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,8 @@ describe('FilesController', () => {
219219
done();
220220
});
221221

222-
it('should return valid filename or url from createFile response when provided', async () => {
222+
it('should return filename and url when adapter returns both', async () => {
223223
const config = Config.get(Parse.applicationId);
224-
225-
// Test case 1: adapter returns new filename and url
226224
const adapterWithReturn = { ...mockAdapter };
227225
adapterWithReturn.createFile = () => {
228226
return Promise.resolve({
@@ -234,17 +232,20 @@ describe('FilesController', () => {
234232
return Promise.resolve('http://default.url/file.txt');
235233
};
236234
const controllerWithReturn = new FilesController(adapterWithReturn, null, { preserveFileName: true });
237-
// preserveFileName is true to make filename behaviors predictable
238-
const result1 = await controllerWithReturn.createFile(
235+
236+
const result = await controllerWithReturn.createFile(
239237
config,
240238
'originalFile.txt',
241239
'data',
242240
'text/plain'
243241
);
244-
expect(result1.name).toBe('newFilename.txt');
245-
expect(result1.url).toBe('http://new.url/newFilename.txt');
246242

247-
// Test case 2: adapter returns nothing, falling back to default behavior
243+
expect(result.name).toBe('newFilename.txt');
244+
expect(result.url).toBe('http://new.url/newFilename.txt');
245+
});
246+
247+
it('should use original filename and generate url when adapter returns nothing', async () => {
248+
const config = Config.get(Parse.applicationId);
248249
const adapterWithoutReturn = { ...mockAdapter };
249250
adapterWithoutReturn.createFile = () => {
250251
return Promise.resolve();
@@ -254,20 +255,20 @@ describe('FilesController', () => {
254255
};
255256

256257
const controllerWithoutReturn = new FilesController(adapterWithoutReturn, null, { preserveFileName: true });
257-
const result2 = await controllerWithoutReturn.createFile(
258+
const result = await controllerWithoutReturn.createFile(
258259
config,
259260
'originalFile.txt',
260261
'data',
261262
'text/plain',
262263
{}
263264
);
264-
265-
expect(result2.name).toBe('originalFile.txt');
266-
expect(result2.url).toBe('http://default.url/originalFile.txt');
267265

268-
// Test case 3: adapter returns partial info (only url)
269-
// This is a valid scenario, as the adapter may return a modified filename
270-
// but may result in a mismatch between the filename and the resource URL
266+
expect(result.name).toBe('originalFile.txt');
267+
expect(result.url).toBe('http://default.url/originalFile.txt');
268+
});
269+
270+
it('should use original filename when adapter returns only url', async () => {
271+
const config = Config.get(Parse.applicationId);
271272
const adapterWithOnlyURL = { ...mockAdapter };
272273
adapterWithOnlyURL.createFile = () => {
273274
return Promise.resolve({
@@ -277,20 +278,22 @@ describe('FilesController', () => {
277278
adapterWithOnlyURL.getFileLocation = () => {
278279
return Promise.resolve('http://default.url/file.txt');
279280
};
280-
281+
281282
const controllerWithPartial = new FilesController(adapterWithOnlyURL, null, { preserveFileName: true });
282-
const result3 = await controllerWithPartial.createFile(
283+
const result = await controllerWithPartial.createFile(
283284
config,
284285
'originalFile.txt',
285286
'data',
286287
'text/plain',
287288
{}
288289
);
289-
290-
expect(result3.name).toBe('originalFile.txt');
291-
expect(result3.url).toBe('http://new.url/partialFile.txt'); // Technically, the resource does not need to match the filename
292290

293-
// Test case 4: adapter returns only filename
291+
expect(result.name).toBe('originalFile.txt');
292+
expect(result.url).toBe('http://new.url/partialFile.txt');
293+
});
294+
295+
it('should use adapter filename and generate url when adapter returns only filename', async () => {
296+
const config = Config.get(Parse.applicationId);
294297
const adapterWithOnlyFilename = { ...mockAdapter };
295298
adapterWithOnlyFilename.createFile = () => {
296299
return Promise.resolve({
@@ -300,17 +303,17 @@ describe('FilesController', () => {
300303
adapterWithOnlyFilename.getFileLocation = (config, filename) => {
301304
return Promise.resolve(`http://default.url/${filename}`);
302305
};
303-
306+
304307
const controllerWithOnlyFilename = new FilesController(adapterWithOnlyFilename, null, { preserveFileName: true });
305-
const result4 = await controllerWithOnlyFilename.createFile(
308+
const result = await controllerWithOnlyFilename.createFile(
306309
config,
307310
'originalFile.txt',
308311
'data',
309312
'text/plain',
310313
{}
311314
);
312-
313-
expect(result4.name).toBe('newname.txt');
314-
expect(result4.url).toBe('http://default.url/newname.txt');
315+
316+
expect(result.name).toBe('newname.txt');
317+
expect(result.url).toBe('http://default.url/newname.txt');
315318
});
316319
});

0 commit comments

Comments
 (0)