11import React from 'react' ;
2- import { FormRenderer , Form , FormSpy } from '@data-driven-forms/react-form-renderer' ;
2+ import { act } from 'react-dom/test-utils' ;
3+ import { FormRenderer , Form , FormSpy , FormError } from '@data-driven-forms/react-form-renderer' ;
34import { mount } from 'enzyme' ;
5+ import { Alert } from '@patternfly/react-core' ;
6+
47import FormTemplate , { Title , Description , Button } from '../form-template' ;
58import RenderWithProvider from '../../../../__mocks__/with-provider' ;
9+ import componentMapper from '../component-mapper' ;
610
711describe ( 'FormTemplate PF4 Common' , ( ) => {
812 let initialProps ;
913 let ContextWrapper ;
1014 let formOptions ;
1115
1216 beforeEach ( ( ) => {
13- formOptions = { onSubmit : jest . fn ( ) , onReset : jest . fn ( ) , onCancel : jest . fn ( ) , canReset : true , pristine : true } ;
17+ formOptions = {
18+ onSubmit : jest . fn ( ) ,
19+ onReset : jest . fn ( ) ,
20+ onCancel : jest . fn ( ) ,
21+ canReset : true ,
22+ pristine : true ,
23+ getState : jest . fn ( ) . mockImplementation ( ( ) => ( { } ) )
24+ } ;
1425 ContextWrapper = ( { children, ...props } ) => (
1526 < RenderWithProvider value = { { formOptions } } >
1627 < Form onSubmit = { jest . fn ( ) } > { ( ) => children } </ Form >
@@ -37,7 +48,7 @@ describe('FormTemplate PF4 Common', () => {
3748 expect ( wrapper . find ( Title ) ) . toHaveLength ( 1 ) ;
3849 expect ( wrapper . find ( Description ) ) . toHaveLength ( 0 ) ;
3950 expect ( wrapper . find ( Button ) ) . toHaveLength ( 2 ) ;
40- expect ( wrapper . find ( FormSpy ) ) . toHaveLength ( 1 ) ;
51+ expect ( wrapper . find ( FormSpy ) ) . toHaveLength ( 2 ) ;
4152 } ) ;
4253
4354 it ( 'should hide buttons' , ( ) => {
@@ -48,7 +59,7 @@ describe('FormTemplate PF4 Common', () => {
4859 ) ;
4960
5061 expect ( wrapper . find ( Button ) ) . toHaveLength ( 0 ) ;
51- expect ( wrapper . find ( FormSpy ) ) . toHaveLength ( 0 ) ;
62+ expect ( wrapper . find ( FormSpy ) ) . toHaveLength ( 1 ) ;
5263 } ) ;
5364
5465 it ( 'should render description' , ( ) => {
@@ -158,4 +169,86 @@ describe('FormTemplate PF4 Common', () => {
158169
159170 expect ( onCancel ) . toHaveBeenCalledWith ( expectedValues ) ;
160171 } ) ;
172+
173+ it ( 'show form alert message' , async ( ) => {
174+ const wrapper = mount (
175+ < FormRenderer
176+ schema = { {
177+ fields : [
178+ {
179+ component : 'text-field' ,
180+ name : 'field'
181+ }
182+ ]
183+ } }
184+ validate = { ( { field } ) => {
185+ if ( field ) {
186+ return { [ FormError ] : 'some error title' } ;
187+ }
188+ } }
189+ onSubmit = { jest . fn ( ) }
190+ FormTemplate = { FormTemplate }
191+ componentMapper = { componentMapper }
192+ />
193+ ) ;
194+
195+ expect ( wrapper . find ( Alert ) ) . toHaveLength ( 0 ) ;
196+
197+ await act ( async ( ) => {
198+ wrapper
199+ . find ( 'input' )
200+ . first ( )
201+ . instance ( ) . value = 'cats' ;
202+ wrapper
203+ . find ( 'input' )
204+ . first ( )
205+ . simulate ( 'change' ) ;
206+ } ) ;
207+ wrapper . update ( ) ;
208+
209+ expect ( wrapper . find ( Alert ) ) . toHaveLength ( 1 ) ;
210+ expect ( wrapper . find ( Alert ) . props ( ) . title ) . toEqual ( 'some error title' ) ;
211+ expect ( wrapper . find ( Alert ) . text ( ) ) . toEqual ( 'Danger alert:some error title' ) ;
212+ } ) ;
213+
214+ it ( 'show form alert message as object' , async ( ) => {
215+ const wrapper = mount (
216+ < FormRenderer
217+ schema = { {
218+ fields : [
219+ {
220+ component : 'text-field' ,
221+ name : 'field'
222+ }
223+ ]
224+ } }
225+ validate = { ( { field } ) => {
226+ if ( field ) {
227+ return { [ FormError ] : { title : 'some error title' , description : 'some description' } } ;
228+ }
229+ } }
230+ onSubmit = { jest . fn ( ) }
231+ FormTemplate = { FormTemplate }
232+ componentMapper = { componentMapper }
233+ />
234+ ) ;
235+
236+ expect ( wrapper . find ( Alert ) ) . toHaveLength ( 0 ) ;
237+
238+ await act ( async ( ) => {
239+ wrapper
240+ . find ( 'input' )
241+ . first ( )
242+ . instance ( ) . value = 'cats' ;
243+ wrapper
244+ . find ( 'input' )
245+ . first ( )
246+ . simulate ( 'change' ) ;
247+ } ) ;
248+ wrapper . update ( ) ;
249+
250+ expect ( wrapper . find ( Alert ) ) . toHaveLength ( 1 ) ;
251+ expect ( wrapper . find ( Alert ) . props ( ) . title ) . toEqual ( 'some error title' ) ;
252+ expect ( wrapper . find ( Alert ) . text ( ) ) . toEqual ( 'Danger alert:some error titlesome description' ) ;
253+ } ) ;
161254} ) ;
0 commit comments