-
-
Notifications
You must be signed in to change notification settings - Fork 482
/
Copy pathuseFormState.test.js
50 lines (45 loc) · 1.36 KB
/
useFormState.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// @flow
import React from "react";
import { render, cleanup } from "@testing-library/react";
import { ErrorBoundary } from "./testUtils";
import { useFormState, Form } from "./index";
describe("useField", () => {
afterEach(cleanup);
// Most of the functionality of useFormState is tested in FormSpy.test.js
// This file is only for testing its use as a hook in other components
it("should warn if not used inside a form", () => {
jest.spyOn(console, "error").mockImplementation(() => {});
const errorSpy = jest.fn();
const MyFormStateComponent = () => {
useFormState();
return <div />;
};
render(
<ErrorBoundary spy={errorSpy}>
<MyFormStateComponent />
</ErrorBoundary>,
);
expect(errorSpy).toHaveBeenCalled();
expect(errorSpy).toHaveBeenCalledTimes(1);
expect(errorSpy.mock.calls[0][0].message).toBe(
"useFormState must be used inside of a <Form> component",
);
console.error.mockRestore();
});
it("state should be enumerable", () => {
const Test = () => {
const state = useFormState();
expect(Object.keys(state).length > 0).toBe(true);
return <div>It worked</div>;
};
render(
<Form onSubmit={() => {}}>
{({ handleSubmit }) => (
<form onSubmit={handleSubmit}>
<Test />
</form>
)}
</Form>,
);
});
});