-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
useVirtualField.spec.tsx
99 lines (87 loc) 路 3.07 KB
/
useVirtualField.spec.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
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
import React from 'react'
import { act, renderHook } from '@testing-library/react-hooks'
import FormContext from '../context';
import useForm from '../hooks/useForm'
import useVirtualField from '../hooks/useVirtualField'
import { createForm } from '..';
import { FormLifeCycle, LifeCycleTypes } from '@uform/core';
describe('useVirtualField hook',()=>{
test('form is required', ()=>{
expect(() => {
useVirtualField({})
}).toThrow()
})
test('basic ', ()=>{
let globalForm
let globalGraph
const formInstance = createForm({
lifecycles: [
new FormLifeCycle(LifeCycleTypes.ON_FORM_GRAPH_CHANGE, (graph) => {
globalGraph = graph
})
]
})
const formWrapper = ({ children }) => {
const form = useForm({
form: formInstance
})
globalForm = form
return <FormContext.Provider value={form}>{children}</FormContext.Provider>
}
const { result } = renderHook(() => useVirtualField({ name: 'username' }), { wrapper: formWrapper })
expect(result.current.form).toEqual(globalForm)
expect(result.current.state.props).toEqual({})
expect(result.current.state).toEqual({
...globalGraph.get('username').getState(),
mounted: false,
})
})
test('update', ()=>{
let globalForm
const formWrapper = ({ children }) => {
const form = useForm({})
globalForm = form
return <FormContext.Provider value={form}>{children}</FormContext.Provider>
}
const { result } = renderHook(() => useVirtualField({ name: 'username' }), { wrapper: formWrapper })
expect(result.current.state.visible).toEqual(true)
act(() => {
globalForm.setFieldState('username', state => state.visible = false)
})
expect(result.current.state.visible).toEqual(false)
})
test('mounted change', async ()=>{
const formWrapper = ({ children }) => {
const form = useForm({})
return <FormContext.Provider value={form}>{children}</FormContext.Provider>
}
const { result, rerender } = renderHook(() => useVirtualField({ name: 'username' }), { wrapper: formWrapper })
expect(result.current.state.mounted).toEqual(false)
rerender()
expect(result.current.state.mounted).toEqual(true)
})
test('dirty', async ()=>{
const formWrapper = ({ children }) => {
const formInstance = createForm({
lifecycles: [
new FormLifeCycle(LifeCycleTypes.ON_FIELD_CHANGE, (field) => {
})
]
})
const form = useForm({
form: formInstance
})
return <FormContext.Provider value={form}>{children}</FormContext.Provider>
}
const initialProps = {
name: 'username',
props: { disabled: true },
}
const { result, rerender } = renderHook(() => useVirtualField(initialProps), { wrapper: formWrapper })
expect(result.current.props).toEqual({ disabled: true })
initialProps.props = { disabled: false }
expect(result.current.props).toEqual({ disabled: true })
rerender()
expect(result.current.props).toEqual(initialProps.props)
})
})