Skip to content

Commit 77d22e2

Browse files
committed
Add FormData workaround testing-library/user-event#1109
1 parent f830fa1 commit 77d22e2

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

packages/react-zorm/__tests__/use-zorm.test.tsx

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,50 @@ import { useZorm } from "../src";
88
import { assertNotAny } from "./test-helpers";
99
import { createCustomIssues } from "../src/chains";
1010

11+
/**
12+
* For https://github.com/testing-library/user-event/pull/1109
13+
*/
14+
class WorkaroundFormData extends FormData {
15+
#formRef?: HTMLFormElement;
16+
constructor(...args: ConstructorParameters<typeof FormData>) {
17+
super(...args);
18+
this.#formRef = args[0];
19+
}
20+
21+
// React Zorm only uses entries() so this is the only method we need to patch
22+
override *entries() {
23+
for (const [name, value] of super.entries()) {
24+
const entry: [string, FormDataEntryValue] = [name, value];
25+
26+
if (value instanceof File && this.#formRef) {
27+
const input = this.#formRef.querySelector(
28+
`input[name="${name}"]`,
29+
);
30+
31+
if (input instanceof HTMLInputElement) {
32+
const realFile = input?.files?.[0];
33+
if (realFile) {
34+
console.log("file", realFile.name);
35+
entry[1] = realFile;
36+
}
37+
}
38+
}
39+
40+
yield entry;
41+
}
42+
}
43+
}
44+
45+
const OrigFormData = globalThis.FormData;
46+
47+
beforeAll(() => {
48+
globalThis.FormData = WorkaroundFormData;
49+
});
50+
51+
afterAll(() => {
52+
globalThis.FormData = OrigFormData;
53+
});
54+
1155
test("single field validation", () => {
1256
const Schema = z.object({
1357
thing: z.string().min(1),

0 commit comments

Comments
 (0)