@@ -41,7 +41,23 @@ const dataWithEmptyMessage = {
4141 ] ,
4242} ;
4343
44- const schemaWithRequired : JsonSchema7 = {
44+ const dataWithNullMessage = {
45+ nested : [
46+ {
47+ message : null as string | null ,
48+ } ,
49+ ] ,
50+ } ;
51+
52+ const dataWithUndefinedMessage = {
53+ nested : [
54+ {
55+ message : undefined as string | undefined ,
56+ } ,
57+ ] ,
58+ } ;
59+
60+ const schemaWithMinLength : JsonSchema7 = {
4561 type : 'object' ,
4662 properties : {
4763 nested : {
@@ -57,6 +73,23 @@ const schemaWithRequired: JsonSchema7 = {
5773 } ,
5874} ;
5975
76+ const schemaWithRequired : JsonSchema7 = {
77+ type : 'object' ,
78+ properties : {
79+ nested : {
80+ type : 'array' ,
81+ items : {
82+ type : 'object' ,
83+ properties : {
84+ message : { type : 'string' } ,
85+ done : { type : 'boolean' } ,
86+ } ,
87+ required : [ 'message' ] ,
88+ } ,
89+ } ,
90+ } ,
91+ } ;
92+
6093const uischema = {
6194 type : 'VerticalLayout' ,
6295 elements : [
@@ -70,67 +103,126 @@ const uischema = {
70103describe ( 'Material table control' , ( ) => {
71104 let wrapper : ReactWrapper ;
72105
106+ const validSchemaDataPairs = [
107+ {
108+ schema : schemaWithRequired ,
109+ data : dataWithEmptyMessage ,
110+ } ,
111+ {
112+ schema : schemaWithMinLength ,
113+ data : dataWithUndefinedMessage ,
114+ } ,
115+ ] ;
116+
117+ const invalidSchemaDataPairs = [
118+ {
119+ schema : schemaWithRequired ,
120+ data : dataWithNullMessage ,
121+ message : 'must be string' ,
122+ } ,
123+ {
124+ schema : schemaWithRequired ,
125+ data : dataWithUndefinedMessage ,
126+ message : "must have required property 'message'" ,
127+ } ,
128+ {
129+ schema : schemaWithMinLength ,
130+ data : dataWithEmptyMessage ,
131+ message : 'must NOT have fewer than 3 characters' ,
132+ } ,
133+ ] ;
134+
73135 afterEach ( ( ) => wrapper . unmount ( ) ) ;
74136
75- it ( 'should show error message for invalid property with validation mode ValidateAndShow' , ( ) => {
76- wrapper = mount (
77- < JsonForms
78- data = { dataWithEmptyMessage }
79- schema = { schemaWithRequired }
80- uischema = { uischema }
81- renderers = { materialRenderers }
82- cells = { materialCells }
83- validationMode = 'ValidateAndShow'
84- />
85- ) ;
86- const messageFormHelperText = wrapper . find ( FormHelperText ) . at ( 0 ) ;
87- expect ( messageFormHelperText . text ( ) ) . toBe (
88- 'must NOT have fewer than 3 characters'
89- ) ;
90- expect ( messageFormHelperText . props ( ) . error ) . toBe ( true ) ;
91-
92- const doneFormHelperText = wrapper . find ( FormHelperText ) . at ( 1 ) ;
93- expect ( doneFormHelperText . text ( ) ) . toBe ( '' ) ;
94- expect ( doneFormHelperText . props ( ) . error ) . toBe ( false ) ;
95- } ) ;
96-
97- it ( 'should not show error message for invalid property with validation mode ValidateAndHide' , ( ) => {
98- wrapper = mount (
99- < JsonForms
100- data = { dataWithEmptyMessage }
101- schema = { schemaWithRequired }
102- uischema = { uischema }
103- renderers = { materialRenderers }
104- cells = { materialCells }
105- validationMode = 'ValidateAndHide'
106- />
107- ) ;
108- const messageFormHelperText = wrapper . find ( FormHelperText ) . at ( 0 ) ;
109- expect ( messageFormHelperText . text ( ) ) . toBe ( '' ) ;
110- expect ( messageFormHelperText . props ( ) . error ) . toBe ( false ) ;
111-
112- const doneFormHelperText = wrapper . find ( FormHelperText ) . at ( 1 ) ;
113- expect ( doneFormHelperText . text ( ) ) . toBe ( '' ) ;
114- expect ( doneFormHelperText . props ( ) . error ) . toBe ( false ) ;
115- } ) ;
116-
117- it ( 'should not show error message for invalid property with validation mode NoValidation' , ( ) => {
118- wrapper = mount (
119- < JsonForms
120- data = { dataWithEmptyMessage }
121- schema = { schemaWithRequired }
122- uischema = { uischema }
123- renderers = { materialRenderers }
124- cells = { materialCells }
125- validationMode = 'NoValidation'
126- />
127- ) ;
128- const messageFormHelperText = wrapper . find ( FormHelperText ) . at ( 0 ) ;
129- expect ( messageFormHelperText . text ( ) ) . toBe ( '' ) ;
130- expect ( messageFormHelperText . props ( ) . error ) . toBe ( false ) ;
131-
132- const doneFormHelperText = wrapper . find ( FormHelperText ) . at ( 1 ) ;
133- expect ( doneFormHelperText . text ( ) ) . toBe ( '' ) ;
134- expect ( doneFormHelperText . props ( ) . error ) . toBe ( false ) ;
135- } ) ;
137+ it . each ( invalidSchemaDataPairs ) (
138+ 'should show error message for invalid property with validation mode ValidateAndShow' ,
139+ ( { schema, data, message } ) => {
140+ wrapper = mount (
141+ < JsonForms
142+ data = { data }
143+ schema = { schema }
144+ uischema = { uischema }
145+ renderers = { materialRenderers }
146+ cells = { materialCells }
147+ validationMode = 'ValidateAndShow'
148+ />
149+ ) ;
150+ const messageFormHelperText = wrapper . find ( FormHelperText ) . at ( 0 ) ;
151+ expect ( messageFormHelperText . text ( ) ) . toBe ( message ) ;
152+ expect ( messageFormHelperText . props ( ) . error ) . toBe ( true ) ;
153+
154+ const doneFormHelperText = wrapper . find ( FormHelperText ) . at ( 1 ) ;
155+ expect ( doneFormHelperText . text ( ) ) . toBe ( '' ) ;
156+ expect ( doneFormHelperText . props ( ) . error ) . toBe ( false ) ;
157+ }
158+ ) ;
159+
160+ it . each ( invalidSchemaDataPairs ) (
161+ 'should not show error message for invalid property with validation mode ValidateAndHide' ,
162+ ( { schema, data } ) => {
163+ wrapper = mount (
164+ < JsonForms
165+ data = { data }
166+ schema = { schema }
167+ uischema = { uischema }
168+ renderers = { materialRenderers }
169+ cells = { materialCells }
170+ validationMode = 'ValidateAndHide'
171+ />
172+ ) ;
173+ const messageFormHelperText = wrapper . find ( FormHelperText ) . at ( 0 ) ;
174+ expect ( messageFormHelperText . text ( ) ) . toBe ( '' ) ;
175+ expect ( messageFormHelperText . props ( ) . error ) . toBe ( false ) ;
176+
177+ const doneFormHelperText = wrapper . find ( FormHelperText ) . at ( 1 ) ;
178+ expect ( doneFormHelperText . text ( ) ) . toBe ( '' ) ;
179+ expect ( doneFormHelperText . props ( ) . error ) . toBe ( false ) ;
180+ }
181+ ) ;
182+
183+ it . each ( invalidSchemaDataPairs ) (
184+ 'should not show error message for invalid property with validation mode NoValidation' ,
185+ ( { schema, data } ) => {
186+ wrapper = mount (
187+ < JsonForms
188+ data = { data }
189+ schema = { schema }
190+ uischema = { uischema }
191+ renderers = { materialRenderers }
192+ cells = { materialCells }
193+ validationMode = 'NoValidation'
194+ />
195+ ) ;
196+ const messageFormHelperText = wrapper . find ( FormHelperText ) . at ( 0 ) ;
197+ expect ( messageFormHelperText . text ( ) ) . toBe ( '' ) ;
198+ expect ( messageFormHelperText . props ( ) . error ) . toBe ( false ) ;
199+
200+ const doneFormHelperText = wrapper . find ( FormHelperText ) . at ( 1 ) ;
201+ expect ( doneFormHelperText . text ( ) ) . toBe ( '' ) ;
202+ expect ( doneFormHelperText . props ( ) . error ) . toBe ( false ) ;
203+ }
204+ ) ;
205+
206+ it . each ( validSchemaDataPairs ) (
207+ 'should not show error message for valid property' ,
208+ ( { schema, data } ) => {
209+ wrapper = mount (
210+ < JsonForms
211+ data = { data }
212+ schema = { schema }
213+ uischema = { uischema }
214+ renderers = { materialRenderers }
215+ cells = { materialCells }
216+ validationMode = 'ValidateAndShow'
217+ />
218+ ) ;
219+ const messageFormHelperText = wrapper . find ( FormHelperText ) . at ( 0 ) ;
220+ expect ( messageFormHelperText . text ( ) ) . toBe ( '' ) ;
221+ expect ( messageFormHelperText . props ( ) . error ) . toBe ( false ) ;
222+
223+ const doneFormHelperText = wrapper . find ( FormHelperText ) . at ( 1 ) ;
224+ expect ( doneFormHelperText . text ( ) ) . toBe ( '' ) ;
225+ expect ( doneFormHelperText . props ( ) . error ) . toBe ( false ) ;
226+ }
227+ ) ;
136228} ) ;
0 commit comments