1- import React , { useState } from 'react' ;
1+ import React from 'react' ;
22import { mount } from 'enzyme' ;
33import { resetWarned } from 'rc-util/lib/warning' ;
4- import Form , { Field , useForm , List } from '../src' ;
4+ import Form , { Field , useForm } from '../src' ;
55import { Input } from './common/InfoField' ;
66import { changeValue , getField } from './common' ;
77
@@ -47,8 +47,16 @@ describe('Form.InitialValues', () => {
4747 path2 : 'Bamboo' ,
4848 } ,
4949 } ) ;
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' ) ;
5260 } ) ;
5361
5462 it ( 'update and reset should use new initialValues' , ( ) => {
@@ -83,15 +91,23 @@ describe('Form.InitialValues', () => {
8391 expect ( form . getFieldsValue ( ) ) . toEqual ( {
8492 username : 'Bamboo' ,
8593 } ) ;
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' ) ;
8799
88100 // Should not change it
89101 wrapper . setProps ( { initialValues : { username : 'Light' } } ) ;
90102 wrapper . update ( ) ;
91103 expect ( form . getFieldsValue ( ) ) . toEqual ( {
92104 username : 'Bamboo' ,
93105 } ) ;
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' ) ;
95111
96112 // Should change it
97113 form . resetFields ( ) ;
@@ -100,68 +116,11 @@ describe('Form.InitialValues', () => {
100116 expect ( form . getFieldsValue ( ) ) . toEqual ( {
101117 username : 'Light' ,
102118 } ) ;
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' ) ;
165124 } ) ;
166125
167126 describe ( 'Field with initialValue' , ( ) => {
@@ -278,12 +237,21 @@ describe('Form.InitialValues', () => {
278237 expect ( wrapper . find ( 'input' ) . props ( ) . value ) . toEqual ( 'story' ) ;
279238
280239 // First reset will get nothing
281- wrapper . find ( 'button' ) . first ( ) . simulate ( 'click' ) ;
240+ wrapper
241+ . find ( 'button' )
242+ . first ( )
243+ . simulate ( 'click' ) ;
282244 expect ( wrapper . find ( 'input' ) . props ( ) . value ) . toEqual ( '' ) ;
283245
284246 // 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' ) ;
287255 expect ( wrapper . find ( 'input' ) . props ( ) . value ) . toEqual ( 'light' ) ;
288256 } ) ;
289257
0 commit comments