Skip to content

Commit b5f22e6

Browse files
committed
fix: more tests
1 parent 22589b5 commit b5f22e6

File tree

3 files changed

+147
-43
lines changed

3 files changed

+147
-43
lines changed

src/fields/components/AutoComplete/src/__tests__/auto-complete.spec.tsx

Lines changed: 145 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,48 +9,152 @@ import { default as AutoCompleteComp } from '..';
99
import { JSONSchema7 } from 'json-schema';
1010

1111
const schema: JSONSchema7 = {
12-
"type": "string",
13-
"title": "Example Auto Complete",
14-
"enum": ["Yes", "No"]
12+
'type': 'string',
13+
'title': 'Which Theme ?',
14+
'enum': [
15+
'Material UI',
16+
'No Theme'
17+
],
18+
'default': 'Material UI'
1519
};
16-
17-
const value = 'Simple Text';
20+
const multiOptionSchema: JSONSchema7 & { parsedArray: boolean } = {
21+
'type': 'string',
22+
'enum': [
23+
'foo',
24+
'bar',
25+
'fuzz',
26+
'qux'
27+
],
28+
'uniqueItems': true,
29+
'parsedArray': true
30+
};
31+
const value = 'Material UI';
32+
const htmlid = 'test';
1833

1934
describe('AutoComplete', () => {
20-
it('mounts with standard attributes (AutoComplete)', () => {
21-
const path = 'done';
22-
const label = 'Done';
23-
schema.description = label;
24-
const wrapper = mount(
25-
<AutoCompleteComp type={'string'} onChange={jest.fn} label={label} htmlid={path} value={value} schema={schema} />,
26-
);
27-
const fcComp = wrapper.find('WithStyles(ForwardRef(TextField))');
28-
expect(fcComp).toHaveLength(1);
29-
expect(fcComp.prop('value')).toBe(value);
30-
});
31-
32-
it('passes additional properties to the AutoComplete component', () => {
33-
const props = {
34-
color: 'secondary',
35-
};
36-
const path = 'done';
37-
const wrapper = mount(
38-
<AutoCompleteComp type={'string'} htmlid={path} onChange={jest.fn} options={{...props}} schema={schema} />,
39-
);
40-
const cbComp = wrapper.find('WithStyles(ForwardRef(Autocomplete))');
41-
expect(cbComp.prop('color')).toBe(props.color);
42-
});
43-
44-
it('renders the div when no schema is passed', () => {
45-
const props = {
46-
color: 'secondary',
47-
};
48-
const path = 'done';
49-
const wrapper = mount(
50-
<AutoCompleteComp schema={schema} type={'string'} htmlid={path} onChange={jest.fn} />,
51-
);
52-
53-
const cbComp = wrapper.find('div');
54-
expect(cbComp).toHaveLength(4);
55-
});
35+
it('mounts with standard attributes (control)', () => {
36+
const path = 'done';
37+
const label = 'Done';
38+
schema.description = label;
39+
const wrapper = mount(
40+
<AutoCompleteComp
41+
type={'string'}
42+
htmlid={htmlid}
43+
label={label}
44+
path={path}
45+
value={value}
46+
schema={schema}
47+
onChange={jest.fn}
48+
/>,
49+
);
50+
51+
const fcComp = wrapper.find('WithStyles(ForwardRef(TextField))');
52+
expect(fcComp).toHaveLength(1);
53+
expect(fcComp.prop('id')).toBe(htmlid);
54+
55+
const cbComp = wrapper.find('input');
56+
expect(cbComp).toHaveLength(1);
57+
expect(cbComp.prop('value')).toBe(value);
58+
});
59+
60+
it('passes additional properties to the Checkbox component', () => {
61+
const props = {
62+
color: 'secondary',
63+
}
64+
const wrapper = mount(
65+
<AutoCompleteComp
66+
type={'string'}
67+
htmlid={htmlid}
68+
label={'label'}
69+
path={'path'}
70+
schema={schema}
71+
onChange={jest.fn}
72+
options={
73+
{...props}
74+
}
75+
/>,
76+
);
77+
78+
const cbComp = wrapper.find('WithStyles(ForwardRef(Autocomplete))');
79+
expect(cbComp.prop('color')).toBe(props.color);
80+
});
81+
82+
it('calls onChange when clicked', () => {
83+
const onChange = jest.fn();
84+
const wrapper = mount(
85+
<AutoCompleteComp
86+
path={'a'}
87+
value={value}
88+
onChange={onChange}
89+
schema={schema}
90+
type={'string'}
91+
htmlid={htmlid}
92+
label={'label'}
93+
/>,
94+
);
95+
const cbComp = wrapper.find('WithStyles(ForwardRef(Autocomplete))');
96+
expect(cbComp).toHaveLength(1);
97+
cbComp.prop('onChange')({
98+
target: {
99+
value: 'new change'
100+
}
101+
});
102+
cbComp.prop('getOptionSelected')('new change');
103+
cbComp.prop('getOptionLabel')('new change');
104+
cbComp.prop('groupBy')('new change');
105+
cbComp.prop('getOptionDisabled')('new change');
106+
expect(onChange).toHaveBeenCalledTimes(1);
107+
});
108+
109+
it('calls onChange when clicked (multiple)', () => {
110+
const onChange = jest.fn();
111+
const wrapper = mount(
112+
<AutoCompleteComp
113+
path={'a'}
114+
value={['foo', 'bar']}
115+
onChange={onChange}
116+
schema={multiOptionSchema}
117+
type={'string'}
118+
htmlid={htmlid}
119+
label={'label'}
120+
options={{ multiple: true }}
121+
/>,
122+
);
123+
const cbComp = wrapper.find('WithStyles(ForwardRef(Autocomplete))');
124+
expect(cbComp).toHaveLength(1);
125+
cbComp.prop('onChange')({
126+
target: { value: ['foo', 'bar'] }
127+
});
128+
cbComp.prop('onChange')({
129+
target: { value: undefined }
130+
});
131+
cbComp.prop('groupBy')('new change');
132+
cbComp.prop('getOptionDisabled')('new change');
133+
expect(onChange).toHaveBeenCalledTimes(2);
134+
});
135+
136+
it('calls onChange when clicked (multiple) (edge case)', () => {
137+
const onChange = jest.fn();
138+
multiOptionSchema.anyOf = [];
139+
const wrapper = mount(
140+
<AutoCompleteComp
141+
path={'a'}
142+
value={'foo'}
143+
onChange={onChange}
144+
schema={multiOptionSchema}
145+
type={'string'}
146+
htmlid={htmlid}
147+
label={'label'}
148+
/>,
149+
);
150+
const cbComp = wrapper.find('WithStyles(ForwardRef(Autocomplete))');
151+
expect(cbComp).toHaveLength(1);
152+
cbComp.prop('onChange')({
153+
target: { value: ['foo', 'bar'] }
154+
});
155+
cbComp.prop('onChange')({
156+
target: { value: undefined }
157+
});
158+
expect(onChange).toHaveBeenCalledTimes(2);
159+
});
56160
});

src/fields/components/AutoComplete/src/auto-complete.props.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Utils
2-
import { valuesToOptions, isEnum, getEnumTitle } from '@react-jsonschema-form-utils/enum-utils';
2+
import { isEnum, getEnumTitle } from '@react-jsonschema-form-utils/enum-utils';
33
import { coerceValue } from '@react-jsonschema-form-utils/parse-values';
44

55
// Types

src/fields/components/AutoComplete/src/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export default ({
5656
groupBy={groupBy}
5757
getOptionDisabled={getOptionDisabled}
5858
value={value && String(value)}
59-
getOptionLabel={(option: { key: string; value: string; }) => option && String(option)}
59+
getOptionLabel={(option: string) => option && String(option)}
6060
getOptionSelected={(val) => val && String(val)}
6161
renderInput={(params) => <TextField {...params} label={title} value={value && String(value)} />}
6262
onChange={givenOnChange}

0 commit comments

Comments
 (0)