Fix test for Blob to File conversion

The Response.blob() method in JavaScript returns a Blob, not a File.
This means that file-specific metadata like `name` and `lastModified` is
lost. The tests were updated to reflect this by manually constructing a
File object from the Blob, mimicking how client code would handle this
conversion. The MIME type is now correctly preserved from the response
headers when creating the File.
This commit is contained in:
cameronapak
2025-12-30 07:33:55 -06:00
parent 19e51fae63
commit 6faf0d421f

View File

@@ -99,10 +99,13 @@ describe("MediaApi", () => {
expect(isReadableStream(res.res.body)).toBe(true); expect(isReadableStream(res.res.body)).toBe(true);
const blob = await res.res.blob(); const blob = await res.res.blob();
expect(isFile(blob)).toBe(true); // Response.blob() always returns Blob, not File - File metadata (name, lastModified) is lost
expect(blob.size).toBeGreaterThan(0); // Client code must manually construct File from Blob (see MediaApi.download() for reference)
expect(blob.type).toBe("image/png"); const file = new File([blob], name, { type: blob.type });
expect(blob.name).toContain(name); expect(isFile(file)).toBe(true);
expect(file.size).toBeGreaterThan(0);
expect(file.type).toBe("image/png");
expect(file.name).toContain(name);
}); });
it("getFileStream", async () => { it("getFileStream", async () => {
@@ -110,14 +113,19 @@ describe("MediaApi", () => {
const api = new MediaApi({}, mockedBackend.request); const api = new MediaApi({}, mockedBackend.request);
const name = "image.png"; const name = "image.png";
const res = await api.getFileStream(name); const { res: originalRes } = await api.getFile(name);
expect(isReadableStream(res)).toBe(true); const stream = await api.getFileStream(name);
expect(isReadableStream(stream)).toBe(true);
const blob = await new Response(res).blob(); const blob = await new Response(stream).blob();
expect(isFile(blob)).toBe(true); // Response.blob() always returns Blob, not File - File metadata (name, lastModified) is lost
expect(blob.size).toBeGreaterThan(0); // Client code must manually construct File from Blob (see MediaApi.download() for reference)
expect(blob.type).toBe("image/png"); // Use originalRes.headers.get("Content-Type") to preserve MIME type from response
expect(blob.name).toContain(name); const file = new File([blob], name, { type: originalRes.headers.get("Content-Type") || blob.type });
expect(isFile(file)).toBe(true);
expect(file.size).toBeGreaterThan(0);
expect(file.type).toBe("image/png");
expect(file.name).toContain(name);
}); });
it("should upload file in various ways", async () => { it("should upload file in various ways", async () => {