1
1
import 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' ;
3
4
import { mount } from 'enzyme' ;
5
+ import { Alert } from '@patternfly/react-core' ;
6
+
4
7
import FormTemplate , { Title , Description , Button } from '../form-template' ;
5
8
import RenderWithProvider from '../../../../__mocks__/with-provider' ;
9
+ import componentMapper from '../component-mapper' ;
6
10
7
11
describe ( 'FormTemplate PF4 Common' , ( ) => {
8
12
let initialProps ;
9
13
let ContextWrapper ;
10
14
let formOptions ;
11
15
12
16
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
+ } ;
14
25
ContextWrapper = ( { children, ...props } ) => (
15
26
< RenderWithProvider value = { { formOptions } } >
16
27
< Form onSubmit = { jest . fn ( ) } > { ( ) => children } </ Form >
@@ -37,7 +48,7 @@ describe('FormTemplate PF4 Common', () => {
37
48
expect ( wrapper . find ( Title ) ) . toHaveLength ( 1 ) ;
38
49
expect ( wrapper . find ( Description ) ) . toHaveLength ( 0 ) ;
39
50
expect ( wrapper . find ( Button ) ) . toHaveLength ( 2 ) ;
40
- expect ( wrapper . find ( FormSpy ) ) . toHaveLength ( 1 ) ;
51
+ expect ( wrapper . find ( FormSpy ) ) . toHaveLength ( 2 ) ;
41
52
} ) ;
42
53
43
54
it ( 'should hide buttons' , ( ) => {
@@ -48,7 +59,7 @@ describe('FormTemplate PF4 Common', () => {
48
59
) ;
49
60
50
61
expect ( wrapper . find ( Button ) ) . toHaveLength ( 0 ) ;
51
- expect ( wrapper . find ( FormSpy ) ) . toHaveLength ( 0 ) ;
62
+ expect ( wrapper . find ( FormSpy ) ) . toHaveLength ( 1 ) ;
52
63
} ) ;
53
64
54
65
it ( 'should render description' , ( ) => {
@@ -158,4 +169,86 @@ describe('FormTemplate PF4 Common', () => {
158
169
159
170
expect ( onCancel ) . toHaveBeenCalledWith ( expectedValues ) ;
160
171
} ) ;
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
+ } ) ;
161
254
} ) ;
0 commit comments