|
| 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 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 = { |
| 61 | + type: 'object', |
| 62 | + properties: { |
| 63 | + nested: { |
| 64 | + type: 'array', |
| 65 | + items: { |
| 66 | + type: 'object', |
| 67 | + properties: { |
| 68 | + message: { type: 'string', minLength: 3 }, |
| 69 | + done: { type: 'boolean' }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | +}; |
| 75 | + |
| 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 | + |
| 93 | +const uischema = { |
| 94 | + type: 'VerticalLayout', |
| 95 | + elements: [ |
| 96 | + { |
| 97 | + type: 'Control', |
| 98 | + scope: '#/properties/nested', |
| 99 | + }, |
| 100 | + ], |
| 101 | +}; |
| 102 | + |
| 103 | +describe('Material table control', () => { |
| 104 | + let wrapper: ReactWrapper; |
| 105 | + |
| 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 | + |
| 135 | + afterEach(() => wrapper.unmount()); |
| 136 | + |
| 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 | + ); |
| 228 | +}); |
0 commit comments