|
| 1 | +import * as React from 'react' |
| 2 | +import ReactDOM from 'react-dom' |
| 3 | +import { act } from 'react-dom/test-utils' |
1 | 4 | import { renderHook, cleanup } from 'react-hooks-testing-library'
|
2 | 5 | import useField, { all } from './useField'
|
| 6 | +import useForm from './useForm' |
3 | 7 |
|
4 | 8 | describe('useField()', () => {
|
5 |
| - let form, name, subscription |
| 9 | + let form, name, subscription, container |
6 | 10 |
|
7 | 11 | beforeEach(() => {
|
8 | 12 | name = 'foo'
|
9 | 13 | subscription = { value: true }
|
| 14 | + container = document.createElement('div') |
| 15 | + }) |
| 16 | + afterEach(() => { |
| 17 | + document.body.removeChild(container) |
| 18 | + container = null |
| 19 | + cleanup() |
10 | 20 | })
|
11 |
| - afterEach(cleanup) |
12 | 21 |
|
13 | 22 | describe("form hook parameter's registerField", () => {
|
14 | 23 | beforeEach(() => {
|
@@ -51,7 +60,9 @@ describe('useField()', () => {
|
51 | 60 | beforeEach(() => {
|
52 | 61 | value = 'bar'
|
53 | 62 | blur = jest.fn()
|
54 |
| - change = jest.fn() |
| 63 | + change = jest.fn(() => { |
| 64 | + debugger |
| 65 | + }) |
55 | 66 | focus = jest.fn()
|
56 | 67 |
|
57 | 68 | form = {
|
@@ -109,7 +120,7 @@ describe('useField()', () => {
|
109 | 120 | })
|
110 | 121 |
|
111 | 122 | describe('when event has a checked prop', () => {
|
112 |
| - const event = { target: { type: 'radio', checked: false } } |
| 123 | + const event = { target: { type: 'checkbox', checked: false } } |
113 | 124 |
|
114 | 125 | it('calls provided handler with value', () => {
|
115 | 126 | const { onChange } = setupHook()
|
@@ -154,4 +165,66 @@ describe('useField()', () => {
|
154 | 165 | expect(returnMeta).toEqual(meta)
|
155 | 166 | })
|
156 | 167 | })
|
| 168 | + |
| 169 | + describe('field level validation', () => { |
| 170 | + it('should not call form validation if field validationallow validate field', () => { |
| 171 | + const FIELD_NAME = 'firstName' |
| 172 | + |
| 173 | + const onSubmit = jest.fn() |
| 174 | + const validate = jest.fn(values => { |
| 175 | + let errors = {} |
| 176 | + if (values[FIELD_NAME] && values[FIELD_NAME].length <= 4) { |
| 177 | + errors[FIELD_NAME] = 'Must be at least 4 chars' |
| 178 | + } |
| 179 | + return errors |
| 180 | + }) |
| 181 | + |
| 182 | + const required = jest.fn(value => (value ? undefined : 'Required')) |
| 183 | + |
| 184 | + const FormComponent = () => { |
| 185 | + const { form, handleSubmit } = useForm({ |
| 186 | + onSubmit, |
| 187 | + validate |
| 188 | + }) |
| 189 | + const firstName = useField(FIELD_NAME, form, required) |
| 190 | + |
| 191 | + return ( |
| 192 | + <form onSubmit={handleSubmit}> |
| 193 | + <label>First Name</label> |
| 194 | + <input {...firstName.input} placeholder="First Name" /> |
| 195 | + {firstName.meta.touched && firstName.meta.error && ( |
| 196 | + <span>{firstName.meta.error}</span> |
| 197 | + )} |
| 198 | + <button type="submit">Submit</button> |
| 199 | + </form> |
| 200 | + ) |
| 201 | + } |
| 202 | + |
| 203 | + act(() => { |
| 204 | + ReactDOM.render(<FormComponent />, container) |
| 205 | + }) |
| 206 | + |
| 207 | + expect(validate).toHaveBeenCalledTimes(2) |
| 208 | + expect(required).toHaveBeenCalledTimes(1) |
| 209 | + |
| 210 | + // span is not in dom because field error is not raised |
| 211 | + expect(container.querySelector('span')).toBe(null) |
| 212 | + |
| 213 | + // submit |
| 214 | + const button = container.querySelector('button') |
| 215 | + act(() => { |
| 216 | + button.dispatchEvent(new MouseEvent('click', { bubbles: true })) |
| 217 | + }) |
| 218 | + |
| 219 | + // span has required error |
| 220 | + expect(container.querySelector('span').innerHTML).toBe('Required') |
| 221 | + // form validate function has not been called |
| 222 | + expect(validate).toHaveBeenCalledTimes(2) |
| 223 | + // onSubmit has not been called in any moment |
| 224 | + expect(onSubmit).not.toHaveBeenCalled() |
| 225 | + |
| 226 | + // why this is not updated again? |
| 227 | + expect(required).toHaveBeenCalledTimes(1) |
| 228 | + }) |
| 229 | + }) |
157 | 230 | })
|
0 commit comments