Skip to content

Commit

Permalink
Add FormData workaround testing-library/user-event#1109
Browse files Browse the repository at this point in the history
  • Loading branch information
esamattis committed Mar 25, 2023
1 parent fc03d50 commit 9c116a8
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions packages/react-zorm/__tests__/use-zorm.test.tsx
Expand Up @@ -8,6 +8,49 @@ import { useZorm } from "../src";
import { assertNotAny } from "./test-helpers";
import { createCustomIssues } from "../src/chains";

/**
* For https://github.com/testing-library/user-event/pull/1109
*/
class WorkaroundFormData extends FormData {
#formRef?: HTMLFormElement;
constructor(...args: ConstructorParameters<typeof FormData>) {
super(...args);
this.#formRef = args[0];
}

// React Zorm only uses entries() so this is the only method we need to patch
override *entries() {
for (const [name, value] of super.entries()) {
const entry: [string, FormDataEntryValue] = [name, value];

if (value instanceof File && this.#formRef) {
const input = this.#formRef.querySelector(
`input[name="${name}"]`,
);

if (input instanceof HTMLInputElement) {
const realFile = input?.files?.[0];
if (realFile) {
entry[1] = realFile;
}
}
}

yield entry;
}
}
}

const OrigFormData = globalThis.FormData;

beforeAll(() => {
globalThis.FormData = WorkaroundFormData;
});

afterAll(() => {
globalThis.FormData = OrigFormData;
});

test("single field validation", () => {
const Schema = z.object({
thing: z.string().min(1),
Expand Down

0 comments on commit 9c116a8

Please sign in to comment.