1
1
import React from 'react' ;
2
2
import { mount } from 'enzyme' ;
3
+ import { act } from 'react-dom/test-utils' ;
3
4
4
5
import FormRenderer from '../../files/form-renderer' ;
5
6
import componentTypes from '../../files/component-types' ;
6
7
import FormTemplate from '../../../../../__mocks__/mock-form-template' ;
7
8
import useFieldApi from '../../files/use-field-api' ;
8
- import { act } from 'react-dom/test-utils' ;
9
9
10
10
describe ( 'FormRenderer validator' , ( ) => {
11
11
const TextField = ( props ) => {
@@ -18,11 +18,12 @@ describe('FormRenderer validator', () => {
18
18
) ;
19
19
} ;
20
20
21
+ const VALUE = 'some-value' ;
22
+ const NAME = 'field1' ;
23
+
21
24
it ( 'pass value, allvalues, meta to custom validator func' , async ( ) => {
22
25
expect . assertions ( 3 ) ;
23
26
24
- const VALUE = 'some-value' ;
25
- const NAME = 'field1' ;
26
27
const META = expect . any ( Object ) ;
27
28
28
29
const validator = ( value , allValues , meta ) => {
@@ -55,4 +56,106 @@ describe('FormRenderer validator', () => {
55
56
wrapper . find ( 'input' ) . simulate ( 'change' , { target : { value : VALUE } } ) ;
56
57
} ) ;
57
58
} ) ;
59
+
60
+ describe ( 'warning validators' , ( ) => {
61
+ const TextFieldWarning = ( props ) => {
62
+ const { input, meta, ...rest } = useFieldApi ( props ) ;
63
+ return (
64
+ < div >
65
+ < input { ...input } { ...rest } />
66
+ { meta . warning && < div id = "warning" > { meta . warning } </ div > }
67
+ </ div >
68
+ ) ;
69
+ } ;
70
+
71
+ let wrapper ;
72
+
73
+ it ( 'should not convert object validator to warning when warnings are not used' , async ( ) => {
74
+ await act ( async ( ) => {
75
+ wrapper = mount (
76
+ < FormRenderer
77
+ FormTemplate = { FormTemplate }
78
+ componentMapper = { {
79
+ [ componentTypes . TEXT_FIELD ] : TextFieldWarning
80
+ } }
81
+ schema = { {
82
+ fields : [ { component : 'text-field' , name : NAME , validate : [ { type : 'required' , warning : true } ] } ]
83
+ } }
84
+ onSubmit = { jest . fn ( ) }
85
+ />
86
+ ) ;
87
+ } ) ;
88
+ wrapper . update ( ) ;
89
+
90
+ expect ( wrapper . find ( '#warning' ) ) . toHaveLength ( 0 ) ;
91
+ } ) ;
92
+
93
+ it ( 'should convert object validator to warning' , async ( ) => {
94
+ await act ( async ( ) => {
95
+ wrapper = mount (
96
+ < FormRenderer
97
+ FormTemplate = { FormTemplate }
98
+ componentMapper = { {
99
+ [ componentTypes . TEXT_FIELD ] : TextFieldWarning
100
+ } }
101
+ schema = { {
102
+ fields : [ { useWarnings : true , component : 'text-field' , name : NAME , validate : [ { type : 'required' , warning : true } ] } ]
103
+ } }
104
+ onSubmit = { jest . fn ( ) }
105
+ />
106
+ ) ;
107
+ } ) ;
108
+ wrapper . update ( ) ;
109
+
110
+ expect ( wrapper . find ( '#warning' ) . text ( ) ) . toEqual ( 'Required' ) ;
111
+ } ) ;
112
+
113
+ it ( 'should convert function validator to warning' , async ( ) => {
114
+ const ERROR = 'SOME-ERROR' ;
115
+
116
+ const customValidator = ( ) => ( { type : 'warning' , error : ERROR } ) ;
117
+
118
+ await act ( async ( ) => {
119
+ wrapper = mount (
120
+ < FormRenderer
121
+ FormTemplate = { FormTemplate }
122
+ componentMapper = { {
123
+ [ componentTypes . TEXT_FIELD ] : TextFieldWarning
124
+ } }
125
+ schema = { {
126
+ fields : [ { useWarnings : true , component : 'text-field' , name : NAME , validate : [ customValidator ] } ]
127
+ } }
128
+ onSubmit = { jest . fn ( ) }
129
+ />
130
+ ) ;
131
+ } ) ;
132
+ wrapper . update ( ) ;
133
+
134
+ expect ( wrapper . find ( '#warning' ) . text ( ) ) . toEqual ( ERROR ) ;
135
+ } ) ;
136
+
137
+ it ( 'should convert async function validator to warning' , async ( ) => {
138
+ const ERROR = 'SOME-ERROR' ;
139
+
140
+ const customValidator = ( ) => new Promise ( ( res , rej ) => setTimeout ( ( ) => rej ( { type : 'warning' , error : ERROR } ) ) ) ;
141
+
142
+ await act ( async ( ) => {
143
+ wrapper = mount (
144
+ < FormRenderer
145
+ FormTemplate = { FormTemplate }
146
+ componentMapper = { {
147
+ [ componentTypes . TEXT_FIELD ] : TextFieldWarning
148
+ } }
149
+ schema = { {
150
+ fields : [ { useWarnings : true , component : 'text-field' , name : NAME , validate : [ customValidator ] } ]
151
+ } }
152
+ onSubmit = { jest . fn ( ) }
153
+ />
154
+ ) ;
155
+ } ) ;
156
+ wrapper . update ( ) ;
157
+
158
+ expect ( wrapper . find ( '#warning' ) . text ( ) ) . toEqual ( ERROR ) ;
159
+ } ) ;
160
+ } ) ;
58
161
} ) ;
0 commit comments