@@ -309,4 +309,80 @@ describe('<FormRenderer />', () => {
309309
310310 expect ( registerSpy ) . toHaveBeenCalledWith ( [ ] ) ;
311311 } ) ;
312+
313+ describe ( 'children prop' , ( ) => {
314+ const ChildrenTemplate = ( { formFields, schema, hideButtons } ) => {
315+ const { handleSubmit } = useFormApi ( ) ;
316+ return (
317+ < form onSubmit = { handleSubmit } >
318+ { schema . title }
319+ { formFields }
320+ { ! hideButtons && < button type = "submit" > Child node submit</ button > }
321+ </ form >
322+ ) ;
323+ } ;
324+
325+ it ( 'should clone template props to children node' , ( ) => {
326+ render (
327+ < FormRenderer componentMapper = { componentMapper } schema = { schema } onSubmit = { jest . fn ( ) } >
328+ < ChildrenTemplate />
329+ </ FormRenderer >
330+ ) ;
331+
332+ expect ( screen . getByLabelText ( 'component1' ) ) . toBeInTheDocument ( ) ;
333+ expect ( screen . getByText ( 'Select field' ) ) . toBeInTheDocument ( ) ;
334+ expect ( screen . getByText ( 'Child node submit' ) ) . toBeInTheDocument ( ) ;
335+ } ) ;
336+
337+ it ( 'should use children node props' , ( ) => {
338+ render (
339+ < FormRenderer componentMapper = { componentMapper } schema = { schema } onSubmit = { jest . fn ( ) } >
340+ < ChildrenTemplate hideButtons />
341+ </ FormRenderer >
342+ ) ;
343+
344+ expect ( screen . getByLabelText ( 'component1' ) ) . toBeInTheDocument ( ) ;
345+ expect ( screen . getByText ( 'Select field' ) ) . toBeInTheDocument ( ) ;
346+ const submitButton = screen . queryByText ( 'Child node submit' ) ;
347+ expect ( submitButton ) . toBeNull ( ) ;
348+ } ) ;
349+
350+ it ( 'should submit data from children node' , ( ) => {
351+ const submitSpy = jest . fn ( ) ;
352+ render (
353+ < FormRenderer initialValues = { { foo : 'bar' } } componentMapper = { componentMapper } schema = { schema } onSubmit = { submitSpy } >
354+ < ChildrenTemplate />
355+ </ FormRenderer >
356+ ) ;
357+
358+ userEvent . click ( screen . getByText ( 'Child node submit' ) ) ;
359+
360+ expect ( submitSpy ) . toHaveBeenCalledWith ( { foo : 'bar' } , expect . any ( Object ) , expect . any ( Function ) ) ;
361+ } ) ;
362+
363+ it ( 'should use children render function' , ( ) => {
364+ render (
365+ < FormRenderer componentMapper = { componentMapper } schema = { schema } onSubmit = { jest . fn ( ) } >
366+ { ( props ) => < ChildrenTemplate { ...props } /> }
367+ </ FormRenderer >
368+ ) ;
369+
370+ expect ( screen . getByLabelText ( 'component1' ) ) . toBeInTheDocument ( ) ;
371+ expect ( screen . getByText ( 'Select field' ) ) . toBeInTheDocument ( ) ;
372+ expect ( screen . getByText ( 'Child node submit' ) ) . toBeInTheDocument ( ) ;
373+ } ) ;
374+
375+ it ( 'should submit data from children render function' , ( ) => {
376+ const submitSpy = jest . fn ( ) ;
377+ render (
378+ < FormRenderer initialValues = { { foo : 'bar' } } componentMapper = { componentMapper } schema = { schema } onSubmit = { submitSpy } >
379+ { ( props ) => < ChildrenTemplate { ...props } /> }
380+ </ FormRenderer >
381+ ) ;
382+
383+ userEvent . click ( screen . getByText ( 'Child node submit' ) ) ;
384+
385+ expect ( submitSpy ) . toHaveBeenCalledWith ( { foo : 'bar' } , expect . any ( Object ) , expect . any ( Function ) ) ;
386+ } ) ;
387+ } ) ;
312388} ) ;
0 commit comments