aboutsummaryrefslogtreecommitdiff
path: root/test/js
diff options
context:
space:
mode:
Diffstat (limited to 'test/js')
-rw-r--r--test/js/bun/globals.test.js75
-rw-r--r--test/js/node/buffer.test.js2
-rw-r--r--test/js/web/html/FormData.test.ts4
-rw-r--r--test/js/web/web-globals.test.js1
4 files changed, 79 insertions, 3 deletions
diff --git a/test/js/bun/globals.test.js b/test/js/bun/globals.test.js
index 6b004a5f7..fd291d8bc 100644
--- a/test/js/bun/globals.test.js
+++ b/test/js/bun/globals.test.js
@@ -1,4 +1,4 @@
-import { expect, it } from "bun:test";
+import { expect, it, describe } from "bun:test";
it("extendable", () => {
const classes = [Blob, TextDecoder, TextEncoder, Request, Response, Headers, HTMLRewriter, Bun.Transpiler, Buffer];
@@ -25,6 +25,7 @@ it("writable", () => {
["ErrorEvent", ErrorEvent],
["CustomEvent", CustomEvent],
["CloseEvent", CloseEvent],
+ ["File", File],
];
for (let [name, Class] of classes) {
globalThis[name] = 123;
@@ -45,8 +46,80 @@ it("name", () => {
["HTMLRewriter", HTMLRewriter],
["Transpiler", Bun.Transpiler],
["Buffer", Buffer],
+ ["File", File],
];
for (let [name, Class] of classes) {
expect(Class.name).toBe(name);
}
});
+
+describe("File", () => {
+ it("constructor", () => {
+ const file = new File(["foo"], "bar.txt", { type: "text/plain;charset=utf-8" });
+ expect(file.name).toBe("bar.txt");
+ expect(file.type).toBe("text/plain;charset=utf-8");
+ expect(file.size).toBe(3);
+ expect(file.lastModified).toBe(0);
+ });
+
+ it("constructor with lastModified", () => {
+ const file = new File(["foo"], "bar.txt", { type: "text/plain;charset=utf-8", lastModified: 123 });
+ expect(file.name).toBe("bar.txt");
+ expect(file.type).toBe("text/plain;charset=utf-8");
+ expect(file.size).toBe(3);
+ expect(file.lastModified).toBe(123);
+ });
+
+ it("constructor with undefined name", () => {
+ const file = new File(["foo"], undefined);
+ expect(file.name).toBe("undefined");
+ expect(file.type).toBe("");
+ expect(file.size).toBe(3);
+ expect(file.lastModified).toBe(0);
+ });
+
+ it("constructor throws invalid args", () => {
+ const invalid = [[], [undefined], [null], [Symbol(), "foo"], [Symbol(), Symbol(), Symbol()]];
+ for (let args of invalid) {
+ expect(() => new File(...args)).toThrow();
+ }
+ });
+
+ it("instanceof", () => {
+ const file = new File(["foo"], "bar.txt", { type: "text/plain;charset=utf-8" });
+ expect(file instanceof File).toBe(true);
+ expect(file instanceof Blob).toBe(true);
+ expect(file instanceof Object).toBe(true);
+ expect(file instanceof Function).toBe(false);
+ const blob = new Blob(["foo"], { type: "text/plain;charset=utf-8" });
+ expect(blob instanceof File).toBe(false);
+ });
+
+ it("extendable", async () => {
+ class Foo extends File {
+ constructor(...args) {
+ super(...args);
+ }
+
+ bar() {
+ return true;
+ }
+
+ text() {
+ return super.text();
+ }
+ }
+ const foo = new Foo(["foo"], "bar.txt", { type: "text/plain;charset=utf-8" });
+ expect(foo instanceof File).toBe(true);
+ expect(foo instanceof Blob).toBe(true);
+ expect(foo instanceof Object).toBe(true);
+ expect(foo instanceof Function).toBe(false);
+ expect(foo instanceof Foo).toBe(true);
+ expect(foo.bar()).toBe(true);
+ expect(foo.name).toBe("bar.txt");
+ expect(foo.type).toBe("text/plain;charset=utf-8");
+ expect(foo.size).toBe(3);
+ expect(foo.lastModified).toBe(0);
+ expect(await foo.text()).toBe("foo");
+ });
+});
diff --git a/test/js/node/buffer.test.js b/test/js/node/buffer.test.js
index 834536020..4040f5ce2 100644
--- a/test/js/node/buffer.test.js
+++ b/test/js/node/buffer.test.js
@@ -1748,7 +1748,7 @@ it("constants", () => {
});
it("File", () => {
- expect(BufferModule.File).toBe(Blob);
+ expect(BufferModule.File).toBe(File);
});
it("transcode", () => {
diff --git a/test/js/web/html/FormData.test.ts b/test/js/web/html/FormData.test.ts
index 8c66abb10..c742bd33e 100644
--- a/test/js/web/html/FormData.test.ts
+++ b/test/js/web/html/FormData.test.ts
@@ -15,9 +15,11 @@ describe("FormData", () => {
it("should be able to append a Blob", async () => {
const formData = new FormData();
- formData.append("foo", new Blob(["bar"]));
+ formData.append("foo", new Blob(["bar"]), "mynameis.txt");
expect(await ((await formData.get("foo")) as Blob)!.text()).toBe("bar");
expect(formData.getAll("foo")[0] instanceof Blob).toBe(true);
+ expect(formData.getAll("foo")[0] instanceof File).toBe(true);
+ expect((formData.getAll("foo")[0] as File).name).toBe("mynameis.txt");
});
it("should be able to set a Blob", async () => {
diff --git a/test/js/web/web-globals.test.js b/test/js/web/web-globals.test.js
index 46422c210..9b4c86006 100644
--- a/test/js/web/web-globals.test.js
+++ b/test/js/web/web-globals.test.js
@@ -20,6 +20,7 @@ test("exists", () => {
expect(typeof Blob !== "undefined").toBe(true);
expect(typeof FormData !== "undefined").toBe(true);
expect(typeof Worker !== "undefined").toBe(true);
+ expect(typeof File !== "undefined").toBe(true);
});
const globalSetters = [