Skip to content

Commit

Permalink
Add tests for reset
Browse files Browse the repository at this point in the history
  • Loading branch information
rzane committed Aug 17, 2020
1 parent 20beb3e commit 23cf95e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
21 changes: 21 additions & 0 deletions test/useForm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,24 @@ test("returns the value when `validate` is called", async () => {
value: ""
});
});

test("re-initializes the form `reset` is called", async () => {
const { result } = setup();

act(() =>
result.current.reset({
value: "value",
error: "error",
touched: true
})
);

expect(result.current.initialValue).toEqual("value");
expect(result.current.value).toEqual("value");

expect(result.current.initialError).toEqual("error");
expect(result.current.error).toEqual("error");

expect(result.current.initialTouched).toEqual(true);
expect(result.current.touched).toEqual(true);
});
26 changes: 26 additions & 0 deletions test/useReset.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { renderHook } from "@testing-library/react-hooks";
import { useReset } from "../src";

test("resets the form", () => {
const reset = jest.fn();
const form: any = { reset };

const onSubmit = renderHook(() => useReset(form));
onSubmit.result.current();
expect(reset).toHaveBeenCalled();
});

test("prevents the default event", () => {
const reset = jest.fn();
const preventDefault = jest.fn();
const stopPropagation = jest.fn();
const form: any = { reset };
const event: any = { preventDefault, stopPropagation };

const onSubmit = renderHook(() => useReset(form));
onSubmit.result.current(event);

expect(reset).toHaveBeenCalled();
expect(preventDefault).toHaveBeenCalled();
expect(stopPropagation).toHaveBeenCalled();
});

0 comments on commit 23cf95e

Please sign in to comment.