@@ -3,6 +3,7 @@ import ReactDOMServer from 'react-dom/server'
33import TestUtils from 'react-dom/test-utils'
44import Form from './ReactFinalForm'
55import Field from './Field'
6+ import deepEqual from 'fast-deep-equal'
67
78const onSubmitMock = values => { }
89const sleep = ms => new Promise ( resolve => setTimeout ( resolve , ms ) )
@@ -224,6 +225,7 @@ describe('ReactFinalForm', () => {
224225 TestUtils . Simulate . submit ( form )
225226 expect ( onSubmit ) . toHaveBeenCalled ( )
226227 } )
228+
227229 it ( 'should reinitialize when initialValues prop changes' , ( ) => {
228230 const renderInput = jest . fn ( ( { input } ) => < input { ...input } /> )
229231 class Container extends React . Component {
@@ -260,25 +262,68 @@ describe('ReactFinalForm', () => {
260262 expect ( renderInput ) . toHaveBeenCalledTimes ( 3 )
261263 expect ( renderInput . mock . calls [ 2 ] [ 0 ] . input . value ) . toBe ( 'bar' )
262264 } )
263- it ( 'should not reinitialize if initialValues prop is shallow equal' , ( ) => {
265+
266+ it ( 'should reinitialize when initialValues prop changes, deeply' , ( ) => {
267+ const renderInput = jest . fn ( ( { input } ) => < input { ...input } /> )
268+ class Container extends React . Component {
269+ state = { initValues : { foo : { bar : 'baz' } } }
270+ render ( ) {
271+ return (
272+ < Form
273+ onSubmit = { onSubmitMock }
274+ subscription = { { dirty : true } }
275+ initialValues = { this . state . initValues }
276+ >
277+ { ( ) => (
278+ < form >
279+ < Field name = "foo.bar" render = { renderInput } />
280+ < button
281+ type = "button"
282+ onClick = { ( ) =>
283+ this . setState ( { initValues : { foo : { bar : 'baq' } } } )
284+ }
285+ >
286+ Initialize
287+ </ button >
288+ </ form >
289+ ) }
290+ </ Form >
291+ )
292+ }
293+ }
294+ const dom = TestUtils . renderIntoDocument ( < Container /> )
295+ expect ( renderInput ) . toHaveBeenCalled ( )
296+ expect ( renderInput ) . toHaveBeenCalledTimes ( 1 )
297+ const init = TestUtils . findRenderedDOMComponentWithTag ( dom , 'button' )
298+ TestUtils . Simulate . click ( init )
299+
300+ // once to change prop and again after reinitialization
301+ expect ( renderInput ) . toHaveBeenCalledTimes ( 3 )
302+ expect ( renderInput . mock . calls [ 2 ] [ 0 ] . input . value ) . toBe ( 'baq' )
303+ } )
304+
305+ it ( 'should not reinitialize if initialValues prop is deep equal' , ( ) => {
264306 const renderInput = jest . fn ( ( { input } ) => < input { ...input } /> )
265307 const lastRenderInputCall = ( ) =>
266308 renderInput . mock . calls [ renderInput . mock . calls . length - 1 ]
267309 class Container extends React . Component {
268- state = { initValues : { foo : ' bar' } }
310+ state = { initValues : { foo : { bar : 'baz' } } }
269311 render ( ) {
270312 return (
271313 < Form
272314 onSubmit = { onSubmitMock }
273315 subscription = { { dirty : true } }
274316 initialValues = { this . state . initValues }
317+ initialValuesEqual = { deepEqual }
275318 >
276319 { ( ) => (
277320 < form >
278- < Field name = "foo" render = { renderInput } />
321+ < Field name = "foo.bar " render = { renderInput } />
279322 < button
280323 type = "button"
281- onClick = { ( ) => this . setState ( { initValues : { foo : 'bar' } } ) }
324+ onClick = { ( ) =>
325+ this . setState ( { initValues : { foo : { bar : 'baz' } } } )
326+ }
282327 >
283328 Initialize
284329 </ button >
@@ -300,6 +345,7 @@ describe('ReactFinalForm', () => {
300345 const numCalls = renderInput . mock . calls . length
301346 expect ( renderInput . mock . calls [ numCalls - 1 ] [ 0 ] . input . value ) . toBe ( 'bar!' )
302347 } )
348+
303349 it ( 'should respect keepDirtyOnReinitialize prop when initialValues prop changes' , ( ) => {
304350 const renderInput = jest . fn ( ( { input } ) => < input { ...input } /> )
305351 class Container extends React . Component {
0 commit comments