Skip to content

Commit af51726

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

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

Diff for: packages/react-zorm/__tests__/use-zorm.test.tsx

+43
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,49 @@ 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+
entry[1] = realFile;
35+
}
36+
}
37+
}
38+
39+
yield entry;
40+
}
41+
}
42+
}
43+
44+
const OrigFormData = globalThis.FormData;
45+
46+
beforeAll(() => {
47+
globalThis.FormData = WorkaroundFormData;
48+
});
49+
50+
afterAll(() => {
51+
globalThis.FormData = OrigFormData;
52+
});
53+
1154
test("single field validation", () => {
1255
const Schema = z.object({
1356
thing: z.string().min(1),

0 commit comments

Comments
 (0)