Skip to content

Commit ef97e44

Browse files
committed
Add tests for react material array cell validation mode
Ref #2398
1 parent 4d8a72d commit ef97e44

File tree

2 files changed

+138
-1
lines changed

2 files changed

+138
-1
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
The MIT License
3+
4+
Copyright (c) 2017-2019 EclipseSource Munich
5+
https://github.com/eclipsesource/jsonforms
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
*/
25+
import { JsonSchema7 } from '@jsonforms/core';
26+
import * as React from 'react';
27+
28+
import { materialCells, materialRenderers } from '../../src';
29+
import Enzyme, { mount, ReactWrapper } from 'enzyme';
30+
import Adapter from '@wojtekmaj/enzyme-adapter-react-17';
31+
import { JsonForms } from '@jsonforms/react';
32+
import { FormHelperText } from '@mui/material';
33+
34+
Enzyme.configure({ adapter: new Adapter() });
35+
36+
const dataWithEmptyMessage = {
37+
nested: [
38+
{
39+
message: '',
40+
},
41+
],
42+
};
43+
44+
const schemaWithRequired: JsonSchema7 = {
45+
type: 'object',
46+
properties: {
47+
nested: {
48+
type: 'array',
49+
items: {
50+
type: 'object',
51+
properties: {
52+
message: { type: 'string', minLength: 3 },
53+
done: { type: 'boolean' },
54+
},
55+
},
56+
},
57+
},
58+
};
59+
60+
const uischema = {
61+
type: 'VerticalLayout',
62+
elements: [
63+
{
64+
type: 'Control',
65+
scope: '#/properties/nested',
66+
},
67+
],
68+
};
69+
70+
describe('Material table control', () => {
71+
let wrapper: ReactWrapper;
72+
73+
afterEach(() => wrapper.unmount());
74+
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+
});
136+
});

packages/material-renderers/test/renderers/util.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import {
2727
createAjv,
28+
JsonFormsCore,
2829
JsonSchema,
2930
TesterContext,
3031
Translator,
@@ -37,7 +38,7 @@ export const initCore = (
3738
schema: JsonSchema,
3839
uischema: UISchemaElement,
3940
data?: any
40-
) => {
41+
): JsonFormsCore => {
4142
return { schema, uischema, data, ajv: createAjv() };
4243
};
4344

0 commit comments

Comments
 (0)