-
-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathuseFieldArray.test.js
More file actions
101 lines (92 loc) · 2.82 KB
/
useFieldArray.test.js
File metadata and controls
101 lines (92 loc) · 2.82 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import React from 'react'
import { act, render, cleanup } from '@testing-library/react'
import '@testing-library/jest-dom/extend-expect'
import arrayMutators from 'final-form-arrays'
import { ErrorBoundary } from './testUtils'
import { Form } from 'react-final-form'
import useFieldArray from './useFieldArray'
const onSubmitMock = values => {}
describe('FieldArray', () => {
afterEach(cleanup)
// Most of the functionality of useFieldArray is tested in FieldArray.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 MyFieldComponent = () => {
useFieldArray('name')
return <div />
}
render(
<ErrorBoundary spy={errorSpy}>
<MyFieldComponent />
</ErrorBoundary>
)
expect(errorSpy).toHaveBeenCalled()
expect(errorSpy).toHaveBeenCalledTimes(1)
expect(errorSpy.mock.calls[0][0].message).toBe(
'useFieldArray must be used inside of a <Form> component'
)
console.error.mockRestore()
})
it('should track field array state', () => {
const spy = jest.fn()
const MyFieldArray = () => {
spy(useFieldArray('names'))
return null
}
render(
<Form onSubmit={onSubmitMock} mutators={arrayMutators} subscription={{}}>
{() => (
<form>
<MyFieldArray />
</form>
)}
</Form>
)
expect(spy).toHaveBeenCalled()
expect(spy).toHaveBeenCalledTimes(1)
expect(spy.mock.calls[0][0].fields.length).toBe(0)
act(() => spy.mock.calls[0][0].fields.push('bob'))
expect(spy).toHaveBeenCalledTimes(2)
expect(spy.mock.calls[1][0].fields.length).toBe(1)
})
it('should have undefined as value when not initialized', () => {
const spy = jest.fn()
const MyFieldArray = () => {
spy(useFieldArray('names'))
return null
}
render(
<Form onSubmit={onSubmitMock} mutators={arrayMutators} subscription={{}}>
{() => (
<form>
<MyFieldArray />
</form>
)}
</Form>
)
expect(spy).toHaveBeenCalled()
expect(spy).toHaveBeenCalledTimes(1)
expect(spy.mock.calls[0][0].fields.value).toEqual(undefined)
})
it('should have empty array as value when initialized', () => {
const spy = jest.fn()
const MyFieldArray = () => {
spy(useFieldArray('names', { initialValue: [] }))
return null
}
render(
<Form onSubmit={onSubmitMock} mutators={arrayMutators} subscription={{}}>
{() => (
<form>
<MyFieldArray />
</form>
)}
</Form>
)
expect(spy).toHaveBeenCalled()
expect(spy).toHaveBeenCalledTimes(1)
expect(spy.mock.calls[0][0].fields.value).toEqual([])
})
})