Skip to content

Commit ce2c8f9

Browse files
committed
Add tests for different return conditions
1 parent c87b86d commit ce2c8f9

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

spec/FilesController.spec.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,111 @@ describe('FilesController', () => {
218218
expect(gridFSAdapter.validateFilename(fileName)).not.toBe(null);
219219
done();
220220
});
221+
222+
it('should return valid filename or url from createFile response when provided', async () => {
223+
const config = Config.get(Parse.applicationId);
224+
225+
// Test case 1: adapter returns new filename and url
226+
const adapterWithReturn = {
227+
createFile: () => {
228+
return Promise.resolve({
229+
name: 'newfilename.txt',
230+
url: 'http://new.url/newfilename.txt'
231+
});
232+
},
233+
getFileLocation: () => {
234+
return Promise.resolve('http://default.url/file.txt');
235+
},
236+
validateFilename: () => null
237+
};
238+
239+
const controllerWithReturn = new FilesController(adapterWithReturn);
240+
const result1 = await controllerWithReturn.createFile(
241+
config,
242+
'originalfile.txt',
243+
'data',
244+
'text/plain'
245+
);
246+
247+
expect(result1.name).toBe('newfilename.txt');
248+
expect(result1.url).toBe('http://new.url/newfilename.txt');
249+
250+
// Test case 2: adapter returns nothing, falling back to default behavior
251+
const adapterWithoutReturn = {
252+
createFile: () => {
253+
return Promise.resolve();
254+
},
255+
getFileLocation: (config, filename) => {
256+
return Promise.resolve(`http://default.url/${filename}`);
257+
},
258+
validateFilename: () => null
259+
};
260+
261+
const controllerWithoutReturn = new FilesController(adapterWithoutReturn);
262+
const result2 = await controllerWithoutReturn.createFile(
263+
config,
264+
'originalfile.txt',
265+
'data',
266+
'text/plain',
267+
{},
268+
{ preserveFileName: true } // To make filename predictable
269+
);
270+
271+
expect(result2.name).toBe('originalfile.txt');
272+
expect(result2.url).toBe('http://default.url/originalfile.txt');
273+
274+
// Test case 3: adapter returns partial info (only url)
275+
// This is a valid scenario, as the adapter may return a modified filename
276+
// but may result in a mismatch between the filename and the resource URL
277+
const adapterWithOnlyURL = {
278+
createFile: () => {
279+
return Promise.resolve({
280+
url: 'http://new.url/partialfile.txt'
281+
});
282+
},
283+
getFileLocation: () => {
284+
return Promise.resolve('http://default.url/file.txt');
285+
},
286+
validateFilename: () => null
287+
};
288+
289+
const controllerWithPartial = new FilesController(adapterWithOnlyURL);
290+
const result3 = await controllerWithPartial.createFile(
291+
config,
292+
'originalfile.txt',
293+
'data',
294+
'text/plain',
295+
{},
296+
{ preserveFileName: true } // To make filename predictable
297+
);
298+
299+
expect(result3.name).toBe('originalfile.txt');
300+
expect(result3.url).toBe('http://new.url/partialfile.txt'); // Technically, the resource does not need to match the filename
301+
302+
// Test case 4: adapter returns only filename
303+
const adapterWithOnlyFilename = {
304+
createFile: () => {
305+
return Promise.resolve({
306+
name: 'newname.txt'
307+
});
308+
},
309+
getFileLocation: (config, filename) => {
310+
return Promise.resolve(`http://default.url/${filename}`);
311+
},
312+
validateFilename: () => null
313+
};
314+
315+
const controllerWithOnlyFilename = new FilesController(adapterWithOnlyFilename);
316+
const result4 = await controllerWithOnlyFilename.createFile(
317+
config,
318+
'originalfile.txt',
319+
'data',
320+
'text/plain',
321+
{},
322+
{ preserveFileName: true }
323+
);
324+
325+
expect(result4.name).toBe('newname.txt');
326+
expect(result4.url).toBe('http://default.url/newname.txt');
327+
});
221328
});

0 commit comments

Comments
 (0)