-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
useError.test.tsx
48 lines (40 loc) · 1.21 KB
/
useError.test.tsx
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
import * as React from 'react';
import { act, cleanup, render } from '@testing-library/react';
import { HookedForm, useFormConnect, useError } from '../../src';
const ErrorDisplay = () => {
const error = useError('name');
return <p data-testid="error">{error}</p>;
}
const makeHookedForm = (HookedFormOptions?: object, props?: object) => {
let injectedProps: any;
const TestHookedForm = () => {
injectedProps = useFormConnect();
return <ErrorDisplay />
}
return {
getProps: () => injectedProps,
...render(
<HookedForm onSubmit={() => null} {...HookedFormOptions}>
<TestHookedForm {...props} />
</HookedForm>
),
};
};
describe('useError', () => {
afterEach(() => {
cleanup();
});
describe('functionality', () => {
it('should render the correct error', () => {
const { getProps, getByTestId } =
makeHookedForm({ validate: () => ({ name: 'bad' }), validateOnChange: true });
const { setFieldValue, errors } = getProps();
act(() => {
setFieldValue('name', 'jovi');
});
const errorPTag = getByTestId('error');
expect(errors.name).toEqual('bad');
expect(errorPTag.textContent).toEqual('bad');
});
});
});