1
- import React , { useState } from 'react' ;
1
+ import React from 'react' ;
2
2
import { mount } from 'enzyme' ;
3
3
import { resetWarned } from 'rc-util/lib/warning' ;
4
- import Form , { Field , useForm , List } from '../src' ;
4
+ import Form , { Field , useForm } from '../src' ;
5
5
import { Input } from './common/InfoField' ;
6
6
import { changeValue , getField } from './common' ;
7
7
@@ -47,8 +47,16 @@ describe('Form.InitialValues', () => {
47
47
path2 : 'Bamboo' ,
48
48
} ,
49
49
} ) ;
50
- expect ( getField ( wrapper , 'username' ) . find ( 'input' ) . props ( ) . value ) . toEqual ( 'Light' ) ;
51
- expect ( getField ( wrapper , [ 'path1' , 'path2' ] ) . find ( 'input' ) . props ( ) . value ) . toEqual ( 'Bamboo' ) ;
50
+ expect (
51
+ getField ( wrapper , 'username' )
52
+ . find ( 'input' )
53
+ . props ( ) . value ,
54
+ ) . toEqual ( 'Light' ) ;
55
+ expect (
56
+ getField ( wrapper , [ 'path1' , 'path2' ] )
57
+ . find ( 'input' )
58
+ . props ( ) . value ,
59
+ ) . toEqual ( 'Bamboo' ) ;
52
60
} ) ;
53
61
54
62
it ( 'update and reset should use new initialValues' , ( ) => {
@@ -83,15 +91,23 @@ describe('Form.InitialValues', () => {
83
91
expect ( form . getFieldsValue ( ) ) . toEqual ( {
84
92
username : 'Bamboo' ,
85
93
} ) ;
86
- expect ( getField ( wrapper , 'username' ) . find ( 'input' ) . props ( ) . value ) . toEqual ( 'Bamboo' ) ;
94
+ expect (
95
+ getField ( wrapper , 'username' )
96
+ . find ( 'input' )
97
+ . props ( ) . value ,
98
+ ) . toEqual ( 'Bamboo' ) ;
87
99
88
100
// Should not change it
89
101
wrapper . setProps ( { initialValues : { username : 'Light' } } ) ;
90
102
wrapper . update ( ) ;
91
103
expect ( form . getFieldsValue ( ) ) . toEqual ( {
92
104
username : 'Bamboo' ,
93
105
} ) ;
94
- expect ( getField ( wrapper , 'username' ) . find ( 'input' ) . props ( ) . value ) . toEqual ( 'Bamboo' ) ;
106
+ expect (
107
+ getField ( wrapper , 'username' )
108
+ . find ( 'input' )
109
+ . props ( ) . value ,
110
+ ) . toEqual ( 'Bamboo' ) ;
95
111
96
112
// Should change it
97
113
form . resetFields ( ) ;
@@ -100,68 +116,11 @@ describe('Form.InitialValues', () => {
100
116
expect ( form . getFieldsValue ( ) ) . toEqual ( {
101
117
username : 'Light' ,
102
118
} ) ;
103
- expect ( getField ( wrapper , 'username' ) . find ( 'input' ) . props ( ) . value ) . toEqual ( 'Light' ) ;
104
- } ) ;
105
-
106
- it ( `initialValues shouldn't be modified if preserve is false` , ( ) => {
107
- const formValue = {
108
- test : 'test' ,
109
- users : [ { first : 'aaa' , last : 'bbb' } ] ,
110
- } ;
111
-
112
- const Demo = ( ) => {
113
- const [ form ] = Form . useForm ( ) ;
114
- const [ show , setShow ] = useState ( false ) ;
115
-
116
- return (
117
- < >
118
- < button onClick = { ( ) => setShow ( prev => ! prev ) } > switch show</ button >
119
- { show && (
120
- < Form form = { form } initialValues = { formValue } preserve = { false } >
121
- < Field shouldUpdate >
122
- { ( ) => (
123
- < Field name = "test" preserve = { false } >
124
- < Input />
125
- </ Field >
126
- ) }
127
- </ Field >
128
- < List name = "users" >
129
- { fields => (
130
- < >
131
- { fields . map ( ( { key, name, ...restField } ) => (
132
- < >
133
- < Field
134
- { ...restField }
135
- name = { [ name , 'first' ] }
136
- rules = { [ { required : true , message : 'Missing first name' } ] }
137
- >
138
- < Input className = "first-name-input" placeholder = "First Name" />
139
- </ Field >
140
- < Field
141
- { ...restField }
142
- name = { [ name , 'last' ] }
143
- rules = { [ { required : true , message : 'Missing last name' } ] }
144
- >
145
- < Input placeholder = "Last Name" />
146
- </ Field >
147
- </ >
148
- ) ) }
149
- </ >
150
- ) }
151
- </ List >
152
- </ Form >
153
- ) }
154
- </ >
155
- ) ;
156
- } ;
157
-
158
- const wrapper = mount ( < Demo /> ) ;
159
- wrapper . find ( 'button' ) . simulate ( 'click' ) ;
160
- expect ( formValue . users [ 0 ] . last ) . toEqual ( 'bbb' ) ;
161
- wrapper . find ( 'button' ) . simulate ( 'click' ) ;
162
- expect ( formValue . users [ 0 ] . last ) . toEqual ( 'bbb' ) ;
163
- wrapper . find ( 'button' ) . simulate ( 'click' ) ;
164
- expect ( wrapper . find ( '.first-name-input' ) . first ( ) . find ( 'input' ) . instance ( ) . value ) . toEqual ( 'aaa' ) ;
119
+ expect (
120
+ getField ( wrapper , 'username' )
121
+ . find ( 'input' )
122
+ . props ( ) . value ,
123
+ ) . toEqual ( 'Light' ) ;
165
124
} ) ;
166
125
167
126
describe ( 'Field with initialValue' , ( ) => {
@@ -278,12 +237,21 @@ describe('Form.InitialValues', () => {
278
237
expect ( wrapper . find ( 'input' ) . props ( ) . value ) . toEqual ( 'story' ) ;
279
238
280
239
// First reset will get nothing
281
- wrapper . find ( 'button' ) . first ( ) . simulate ( 'click' ) ;
240
+ wrapper
241
+ . find ( 'button' )
242
+ . first ( )
243
+ . simulate ( 'click' ) ;
282
244
expect ( wrapper . find ( 'input' ) . props ( ) . value ) . toEqual ( '' ) ;
283
245
284
246
// Change field initialValue and reset
285
- wrapper . find ( 'button' ) . last ( ) . simulate ( 'click' ) ;
286
- wrapper . find ( 'button' ) . first ( ) . simulate ( 'click' ) ;
247
+ wrapper
248
+ . find ( 'button' )
249
+ . last ( )
250
+ . simulate ( 'click' ) ;
251
+ wrapper
252
+ . find ( 'button' )
253
+ . first ( )
254
+ . simulate ( 'click' ) ;
287
255
expect ( wrapper . find ( 'input' ) . props ( ) . value ) . toEqual ( 'light' ) ;
288
256
} ) ;
289
257
0 commit comments