Skip to content

Commit

Permalink
test: async validating
Browse files Browse the repository at this point in the history
  • Loading branch information
xxleyi committed Mar 4, 2023
1 parent d20c44b commit 1367ca0
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/FinalForm.validating.test.js
Expand Up @@ -1612,4 +1612,41 @@ describe("Field.validation", () => {
expect(twoArg.mock.calls[0][2]).toBeUndefined();
expect(threeArg.mock.calls[0][2]).toEqual(meta);
});

it("should notify form listener when one field async validator runs before unregister and ends after unregister, even other field registers silently", async () => {
const form = createForm({
onSubmit: onSubmitMock,
});
const formSub = jest.fn(() => {});
form.subscribe(formSub, {
validating: true,
});
expect(formSub).toHaveBeenCalledTimes(1);
const asyncFn = async () => {
return await sleep(100);
};
const syncFn = jest.fn(() => {});

const unregisterAsync = form.registerField(
"fieldWithAsyncValidator",
() => {},
{ error: true },
{ initialValue: "value", getValidator: () => asyncFn },
);
await sleep(20);
expect(formSub).toHaveBeenCalledTimes(2);
await sleep(130);
expect(formSub).toHaveBeenCalledTimes(3);
form.registerField(
"bar",
() => {},
{ error: true },
{ initialValue: "value", silent: true, getValidator: () => syncFn },
);
unregisterAsync();
await sleep(20);
expect(formSub).toHaveBeenCalledTimes(4);
await sleep(130);
expect(formSub).toHaveBeenCalledTimes(5);
});
});

0 comments on commit 1367ca0

Please sign in to comment.