/
RadioGroupControl.test.tsx
184 lines (159 loc) · 6.89 KB
/
RadioGroupControl.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
The MIT License
Copyright (c) 2017-2019 EclipseSource Munich
https://github.com/eclipsesource/jsonforms
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import {
isEnumControl,
isOneOfEnumControl,
rankWith,
} from '@jsonforms/core';
import { JsonFormsStateProvider } from '@jsonforms/react';
import * as React from 'react';
import * as _ from 'lodash';
import Adapter from '@wojtekmaj/enzyme-adapter-react-17';
import Enzyme, { mount, ReactWrapper } from 'enzyme';
import '../../src';
import { vanillaStyles, RadioGroupControl, OneOfRadioGroupControl, JsonFormsStyleContext } from '../../src';
import { initCore } from '../util';
Enzyme.configure({ adapter: new Adapter() });
const fixture = {
data: { foo: 'D' },
schema: {
type: 'object',
properties: {
foo: {
type: 'string',
enum: ['A', 'B', 'C', 'D']
}
}
},
uischema: {
type: 'Control',
scope: '#/properties/foo'
}
};
const oneOfFixture = {
data: { foo: 'b' },
schema: {
type: 'object',
properties: {
foo: {
oneOf: [
{ const: 'a', title: 'A' },
{ const: 'b', title: 'B' },
{ const: 'c', title: 'C' }
]
}
}
},
uischema: {
type: 'Control',
scope: '#/properties/foo'
}
};
describe('Radio group control', () => {
let wrapper: ReactWrapper;
afterEach(() => wrapper.unmount());
test('render enum', () => {
const renderers = [{ tester: rankWith(10, isEnumControl), renderer: RadioGroupControl }];
const core = initCore(fixture.schema, fixture.uischema, fixture.data);
wrapper = mount(
<JsonFormsStateProvider initState={{ core, renderers }}>
<RadioGroupControl schema={fixture.schema} uischema={fixture.uischema} />
</JsonFormsStateProvider>
);
const radioButtons = wrapper.find('input[type="radio"]');
expect(radioButtons).toHaveLength(4);
// make sure one option is selected and expect "D"
const currentlyChecked = radioButtons.filter('input[checked=true]');
expect(currentlyChecked).toHaveLength(1);
expect((currentlyChecked.getDOMNode() as HTMLInputElement).value).toBe('D');
});
test('render enum with CSS classes', () => {
const renderers = [{ tester: rankWith(10, isEnumControl), renderer: RadioGroupControl }];
const core = initCore(fixture.schema, fixture.uischema, fixture.data);
wrapper = mount(
<JsonFormsStateProvider initState={{ core, renderers }}>
<RadioGroupControl schema={fixture.schema} uischema={fixture.uischema} />
</JsonFormsStateProvider>
);
const radioControl = wrapper.find('.radio');
expect(radioControl).toHaveLength(1);
expect(radioControl.prop('style')).toEqual({
display: 'flex', flexDirection: 'row'
});
const radioOptions = wrapper.find('.radio-option');
expect(radioOptions).toHaveLength(4);
const radioInput = wrapper.find('.radio-input');
expect(radioInput).toHaveLength(4);
const radioLabel = wrapper.find('.radio-label');
expect(radioLabel).toHaveLength(4);
});
test('do not render inline styles in radio if radio class is overwritten', () => {
const renderers = [{ tester: rankWith(10, isEnumControl), renderer: RadioGroupControl }];
const core = initCore(fixture.schema, fixture.uischema, fixture.data);
const customStyles = vanillaStyles.map(style => {
if (style.name !== 'control.radio') { return style; }
return {
name: style.name,
classNames: ['radio-custom-class']
};
});
wrapper = mount(
<JsonFormsStyleContext.Provider value={{ styles: customStyles }}>
<JsonFormsStateProvider initState={{ core, renderers }}>
<RadioGroupControl schema={fixture.schema} uischema={fixture.uischema} />
</JsonFormsStateProvider>
</JsonFormsStyleContext.Provider>
);
const radioControl = wrapper.find('.radio-custom-class');
expect(radioControl.prop('style')).toEqual({});
});
test('render oneOf', () => {
const renderers = [{ tester: rankWith(10, isOneOfEnumControl), renderer: OneOfRadioGroupControl }];
const core = initCore(oneOfFixture.schema, oneOfFixture.uischema, oneOfFixture.data);
wrapper = mount(
<JsonFormsStateProvider initState={{ core, renderers }}>
<OneOfRadioGroupControl schema={oneOfFixture.schema} uischema={oneOfFixture.uischema} />
</JsonFormsStateProvider>
);
const radioButtons = wrapper.find('input[type="radio"]');
expect(radioButtons).toHaveLength(3);
const currentlyChecked = radioButtons.filter('input[checked=true]');
expect(currentlyChecked).toHaveLength(1);
expect((currentlyChecked.getDOMNode() as HTMLInputElement).value).toBe('b');
});
test('Radio group should have only one selected option', () => {
const renderers = [{ tester: rankWith(10, isEnumControl), renderer: RadioGroupControl }];
const core = initCore(fixture.schema, fixture.uischema, fixture.data);
wrapper = mount(
<JsonFormsStateProvider initState={{ core, renderers }}>
<RadioGroupControl schema={fixture.schema} uischema={fixture.uischema} />
</JsonFormsStateProvider>
);
// change and verify selection
core.data = { ...core.data, foo: 'A' };
core.data = { ...core.data, foo: 'B' };
wrapper.setProps({ initState: { core } });
wrapper.update();
const currentlyChecked = wrapper.find('input[checked=true]');
expect(currentlyChecked).toHaveLength(1);
expect((currentlyChecked.getDOMNode() as HTMLInputElement).value).toBe('B');
});
});