Skip to content

Commit e1a12fd

Browse files
committed
Made UploadEndpoint.upload() extract file name from File object passed as blob instead of separate fileName parameter
1 parent 0839487 commit e1a12fd

2 files changed

Lines changed: 18 additions & 5 deletions

File tree

endpoints/raw/UploadEndpoint.test.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ test('uploadRaw', async () => {
2525

2626
test('uploadForm', async () => {
2727
const endpoint = new UploadEndpoint(new EntryEndpoint('http://localhost/'), 'endpoint', 'data');
28-
const data = new Blob([new Uint8Array([1, 2, 3])], { type: 'mock/type' });
28+
const file = new File([new Uint8Array([1, 2, 3])], 'file.dat', { type: 'mock/type' });
29+
30+
// Spy on FormData.prototype.set to verify what's being set
31+
const setSpy = jest.spyOn(FormData.prototype, 'set');
2932

3033
fetchMock.mockOnceIf(
3134
req => req.method === HttpMethod.Post && req.url === 'http://localhost/endpoint',
@@ -34,5 +37,14 @@ test('uploadForm', async () => {
3437
return {};
3538
}
3639
);
37-
await endpoint.upload(data, 'file.dat');
40+
41+
await endpoint.upload(file);
42+
43+
// Verify the FormData was populated correctly with file name and type
44+
expect(setSpy).toHaveBeenCalledWith('data', file, 'file.dat');
45+
const capturedFile = setSpy.mock.calls[0][1] as File;
46+
expect(capturedFile.name).toBe('file.dat');
47+
expect(capturedFile.type).toBe('mock/type');
48+
49+
setSpy.mockRestore();
3850
});

endpoints/raw/UploadEndpoint.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,17 @@ export class UploadEndpoint extends Endpoint {
1818

1919
/**
2020
* Uploads data to the endpoint.
21-
* @param blob The blob to read the upload data from.
22-
* @param fileName The name of the uploaded file.
21+
* @param blob The blob or file to read the upload data from.
2322
* @param signal Used to cancel the request.
2423
* @throws {@link errors!BadRequestError}: {@link http!HttpStatusCode.BadRequest}
2524
* @throws {@link errors!AuthenticationError}: {@link http!HttpStatusCode.Unauthorized}
2625
* @throws {@link errors!AuthorizationError}: {@link http!HttpStatusCode.Forbidden}
2726
* @throws {@link errors!NotFoundError}: {@link http!HttpStatusCode.NotFound} or {@link http!HttpStatusCode.Gone}
2827
* @throws {@link errors!HttpError}: Other non-success status code
2928
*/
30-
async upload(blob: Blob, fileName?: string, signal?: AbortSignal) {
29+
async upload(blob: Blob | File, signal?: AbortSignal) {
30+
const fileName = blob instanceof File ? blob.name : undefined;
31+
3132
if (this.formField) {
3233
const formData = new FormData();
3334
formData.set(this.formField, blob, fileName);

0 commit comments

Comments
 (0)