@@ -5,6 +5,7 @@ import arrayMutators from 'final-form-arrays'
55import FieldArray from './FieldArray'
66
77const onSubmitMock = values => { }
8+ const sleep = ms => new Promise ( resolve => setTimeout ( resolve , ms ) )
89
910describe ( 'FieldArray' , ( ) => {
1011 it ( 'should warn error if not used inside a form' , ( ) => {
@@ -42,7 +43,7 @@ describe('FieldArray', () => {
4243 expect ( MyComp ) . toHaveBeenCalledTimes ( 1 )
4344 } )
4445
45- it ( 'should resubscribe if name changes' , ( ) => {
46+ it ( 'should resubscribe if name changes' , async ( ) => {
4647 const renderArray = jest . fn ( ( ) => < div /> )
4748 class Container extends React . Component {
4849 state = { name : 'dogs' }
@@ -77,9 +78,10 @@ describe('FieldArray', () => {
7778
7879 const button = TestUtils . findRenderedDOMComponentWithTag ( dom , 'button' )
7980 TestUtils . Simulate . click ( button )
81+ await sleep ( 2 )
8082
81- expect ( renderArray ) . toHaveBeenCalledTimes ( 2 )
82- expect ( renderArray . mock . calls [ 1 ] [ 0 ] . value ) . toEqual ( [ 'Garfield' ] )
83+ expect ( renderArray ) . toHaveBeenCalledTimes ( 4 )
84+ expect ( renderArray . mock . calls [ 3 ] [ 0 ] . value ) . toEqual ( [ 'Garfield' ] )
8385 } )
8486
8587 it ( 'should not resubscribe if name changes when not inside a <Form> (duh)' , ( ) => {
@@ -189,7 +191,7 @@ describe('FieldArray', () => {
189191 TestUtils . Simulate . click ( button )
190192 } )
191193
192- it ( 'should allow field-level validation' , ( ) => {
194+ it ( 'should allow field-level validation' , async ( ) => {
193195 const renderArray = jest . fn ( ( ) => < div /> )
194196 const render = jest . fn ( ( ) => (
195197 < form >
@@ -219,6 +221,7 @@ describe('FieldArray', () => {
219221 expect ( typeof renderArray . mock . calls [ 0 ] [ 0 ] . fields . push ) . toBe ( 'function' )
220222
221223 renderArray . mock . calls [ 0 ] [ 0 ] . fields . push ( 'c' )
224+ await sleep ( 2 )
222225
223226 expect ( renderArray ) . toHaveBeenCalledTimes ( 3 )
224227 expect ( renderArray . mock . calls [ 2 ] [ 0 ] . meta . valid ) . toBe ( false )
@@ -289,7 +292,7 @@ describe('FieldArray', () => {
289292 expect ( result ) . toEqual ( [ 'FOO[0]' , 'FOO[1]' , 'FOO[2]' ] )
290293 } )
291294
292- it ( 'should allow Fields to be rendered' , ( ) => {
295+ it ( 'should allow Field components to be rendered' , async ( ) => {
293296 const renderInput = jest . fn ( ( { input } ) => < input { ...input } /> )
294297 const dom = TestUtils . renderIntoDocument (
295298 < Form
@@ -318,6 +321,7 @@ describe('FieldArray', () => {
318321 const button = TestUtils . findRenderedDOMComponentWithTag ( dom , 'button' )
319322 TestUtils . Simulate . click ( button )
320323
324+ await sleep ( 2 )
321325 expect ( renderInput ) . toHaveBeenCalled ( )
322326 expect ( renderInput ) . toHaveBeenCalledTimes ( 1 )
323327 expect ( renderInput . mock . calls [ 0 ] [ 0 ] . input . name ) . toBe ( 'foo[0]' )
@@ -329,14 +333,15 @@ describe('FieldArray', () => {
329333 expect ( renderInput . mock . calls [ 1 ] [ 0 ] . input . value ) . toBe ( 'dog' )
330334
331335 TestUtils . Simulate . click ( button )
336+ await sleep ( 2 )
332337
333338 // notice it doesn't NEED to be called for foo[0] because that field hasn't changed!
334339 expect ( renderInput ) . toHaveBeenCalledTimes ( 3 )
335340 expect ( renderInput . mock . calls [ 2 ] [ 0 ] . input . name ) . toBe ( 'foo[1]' )
336341 expect ( renderInput . mock . calls [ 2 ] [ 0 ] . input . value ) . toBe ( '' )
337342 } )
338343
339- it ( 'should allow Fields to be rendered for complex objects' , ( ) => {
344+ it ( 'should allow Fields to be rendered for complex objects' , async ( ) => {
340345 const renderFirstNameInput = jest . fn ( ( { input } ) => < input { ...input } /> )
341346 const renderLastNameInput = jest . fn ( ( { input } ) => < input { ...input } /> )
342347 const dom = TestUtils . renderIntoDocument (
@@ -375,6 +380,7 @@ describe('FieldArray', () => {
375380
376381 const button = TestUtils . findRenderedDOMComponentWithTag ( dom , 'button' )
377382 TestUtils . Simulate . click ( button )
383+ await sleep ( 2 )
378384
379385 expect ( renderFirstNameInput ) . toHaveBeenCalled ( )
380386 expect ( renderFirstNameInput ) . toHaveBeenCalledTimes ( 1 )
@@ -399,6 +405,7 @@ describe('FieldArray', () => {
399405 expect ( renderLastNameInput ) . toHaveBeenCalledTimes ( 1 )
400406
401407 TestUtils . Simulate . click ( button )
408+ await sleep ( 2 )
402409
403410 // notice it doesn't NEED to be called for foo[0] because that field hasn't changed!
404411 expect ( renderFirstNameInput ) . toHaveBeenCalledTimes ( 3 )
0 commit comments