@@ -202,4 +202,91 @@ describe('FormSpy', () => {
202202 const button = TestUtils . findRenderedDOMComponentWithTag ( dom , 'button' )
203203 TestUtils . Simulate . click ( button )
204204 } )
205+
206+ it ( 'should call onChange' , ( ) => {
207+ const input = jest . fn ( ( { input } ) => < input { ...input } /> )
208+ const onChange = jest . fn ( )
209+ TestUtils . renderIntoDocument (
210+ < Form onSubmit = { onSubmitMock } >
211+ { ( ) => (
212+ < form >
213+ < FormSpy subscription = { { dirty : true } } onChange = { onChange } />
214+ < Field name = "foo" render = { input } />
215+ </ form >
216+ ) }
217+ </ Form >
218+ )
219+ expect ( input ) . toHaveBeenCalled ( )
220+ expect ( input ) . toHaveBeenCalledTimes ( 1 )
221+ expect ( onChange ) . toHaveBeenCalled ( )
222+ expect ( onChange ) . toHaveBeenCalledTimes ( 1 )
223+ expect ( onChange ) . toHaveBeenCalledWith ( { dirty : false } )
224+
225+ input . mock . calls [ 0 ] [ 0 ] . input . onChange ( 'bar' )
226+
227+ expect ( input ) . toHaveBeenCalledTimes ( 2 )
228+ expect ( onChange ) . toHaveBeenCalledTimes ( 2 )
229+ expect ( onChange ) . toHaveBeenCalledWith ( { dirty : true } )
230+ } )
231+
232+ it ( 'should not render with render prop when given onChange' , ( ) => {
233+ const onChange = jest . fn ( )
234+ const render = jest . fn ( )
235+ TestUtils . renderIntoDocument (
236+ < Form onSubmit = { onSubmitMock } >
237+ { ( ) => (
238+ < form >
239+ < FormSpy
240+ subscription = { { dirty : true } }
241+ onChange = { onChange }
242+ render = { render }
243+ />
244+ </ form >
245+ ) }
246+ </ Form >
247+ )
248+ expect ( onChange ) . toHaveBeenCalled ( )
249+ expect ( onChange ) . toHaveBeenCalledTimes ( 1 )
250+ expect ( render ) . not . toHaveBeenCalled ( )
251+ } )
252+
253+ it ( 'should not render with child render prop when given onChange' , ( ) => {
254+ const onChange = jest . fn ( )
255+ const render = jest . fn ( )
256+ TestUtils . renderIntoDocument (
257+ < Form onSubmit = { onSubmitMock } >
258+ { ( ) => (
259+ < form >
260+ < FormSpy subscription = { { dirty : true } } onChange = { onChange } >
261+ { render }
262+ </ FormSpy >
263+ </ form >
264+ ) }
265+ </ Form >
266+ )
267+ expect ( onChange ) . toHaveBeenCalled ( )
268+ expect ( onChange ) . toHaveBeenCalledTimes ( 1 )
269+ expect ( render ) . not . toHaveBeenCalled ( )
270+ } )
271+
272+ it ( 'should not render with component prop when given onChange' , ( ) => {
273+ const onChange = jest . fn ( )
274+ const render = jest . fn ( )
275+ TestUtils . renderIntoDocument (
276+ < Form onSubmit = { onSubmitMock } >
277+ { ( ) => (
278+ < form >
279+ < FormSpy
280+ subscription = { { dirty : true } }
281+ onChange = { onChange }
282+ component = { render }
283+ />
284+ </ form >
285+ ) }
286+ </ Form >
287+ )
288+ expect ( onChange ) . toHaveBeenCalled ( )
289+ expect ( onChange ) . toHaveBeenCalledTimes ( 1 )
290+ expect ( render ) . not . toHaveBeenCalled ( )
291+ } )
205292} )
0 commit comments