1
- import React from 'react' ;
1
+ import React , { useState } from 'react' ;
2
2
import { mount } from 'enzyme' ;
3
3
import { resetWarned } from 'rc-util/lib/warning' ;
4
- import Form , { Field , useForm } from '../src' ;
4
+ import Form , { Field , useForm , List } from '../src' ;
5
5
import { Input } from './common/InfoField' ;
6
6
import { changeValue , getField } from './common' ;
7
7
@@ -47,16 +47,8 @@ describe('Form.InitialValues', () => {
47
47
path2 : 'Bamboo' ,
48
48
} ,
49
49
} ) ;
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' ) ;
50
+ expect ( getField ( wrapper , 'username' ) . find ( 'input' ) . props ( ) . value ) . toEqual ( 'Light' ) ;
51
+ expect ( getField ( wrapper , [ 'path1' , 'path2' ] ) . find ( 'input' ) . props ( ) . value ) . toEqual ( 'Bamboo' ) ;
60
52
} ) ;
61
53
62
54
it ( 'update and reset should use new initialValues' , ( ) => {
@@ -91,23 +83,15 @@ describe('Form.InitialValues', () => {
91
83
expect ( form . getFieldsValue ( ) ) . toEqual ( {
92
84
username : 'Bamboo' ,
93
85
} ) ;
94
- expect (
95
- getField ( wrapper , 'username' )
96
- . find ( 'input' )
97
- . props ( ) . value ,
98
- ) . toEqual ( 'Bamboo' ) ;
86
+ expect ( getField ( wrapper , 'username' ) . find ( 'input' ) . props ( ) . value ) . toEqual ( 'Bamboo' ) ;
99
87
100
88
// Should not change it
101
89
wrapper . setProps ( { initialValues : { username : 'Light' } } ) ;
102
90
wrapper . update ( ) ;
103
91
expect ( form . getFieldsValue ( ) ) . toEqual ( {
104
92
username : 'Bamboo' ,
105
93
} ) ;
106
- expect (
107
- getField ( wrapper , 'username' )
108
- . find ( 'input' )
109
- . props ( ) . value ,
110
- ) . toEqual ( 'Bamboo' ) ;
94
+ expect ( getField ( wrapper , 'username' ) . find ( 'input' ) . props ( ) . value ) . toEqual ( 'Bamboo' ) ;
111
95
112
96
// Should change it
113
97
form . resetFields ( ) ;
@@ -116,11 +100,68 @@ describe('Form.InitialValues', () => {
116
100
expect ( form . getFieldsValue ( ) ) . toEqual ( {
117
101
username : 'Light' ,
118
102
} ) ;
119
- expect (
120
- getField ( wrapper , 'username' )
121
- . find ( 'input' )
122
- . props ( ) . value ,
123
- ) . toEqual ( 'Light' ) ;
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' ) ;
124
165
} ) ;
125
166
126
167
describe ( 'Field with initialValue' , ( ) => {
@@ -237,21 +278,12 @@ describe('Form.InitialValues', () => {
237
278
expect ( wrapper . find ( 'input' ) . props ( ) . value ) . toEqual ( 'story' ) ;
238
279
239
280
// First reset will get nothing
240
- wrapper
241
- . find ( 'button' )
242
- . first ( )
243
- . simulate ( 'click' ) ;
281
+ wrapper . find ( 'button' ) . first ( ) . simulate ( 'click' ) ;
244
282
expect ( wrapper . find ( 'input' ) . props ( ) . value ) . toEqual ( '' ) ;
245
283
246
284
// Change field initialValue and reset
247
- wrapper
248
- . find ( 'button' )
249
- . last ( )
250
- . simulate ( 'click' ) ;
251
- wrapper
252
- . find ( 'button' )
253
- . first ( )
254
- . simulate ( 'click' ) ;
285
+ wrapper . find ( 'button' ) . last ( ) . simulate ( 'click' ) ;
286
+ wrapper . find ( 'button' ) . first ( ) . simulate ( 'click' ) ;
255
287
expect ( wrapper . find ( 'input' ) . props ( ) . value ) . toEqual ( 'light' ) ;
256
288
} ) ;
257
289
0 commit comments