forked from mui/material-ui
-
Notifications
You must be signed in to change notification settings - Fork 1
/
FormControl.test.js
323 lines (269 loc) · 10.3 KB
/
FormControl.test.js
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import * as React from 'react';
import { expect } from 'chai';
import { spy } from 'sinon';
import { createMount, getClasses } from '@material-ui/core/test-utils';
import describeConformance from '../test-utils/describeConformance';
import { act, createClientRender } from 'test/utils/createClientRender';
import Input from '../Input';
import Select from '../Select';
import FormControl from './FormControl';
import useFormControl from './useFormControl';
describe('<FormControl />', () => {
let mount;
const render = createClientRender();
let classes;
function TestComponent(props) {
const context = useFormControl();
React.useEffect(() => {
props.contextCallback(context);
});
return null;
}
before(() => {
mount = createMount({ strict: true });
classes = getClasses(<FormControl />);
});
after(() => {
mount.cleanUp();
});
describeConformance(<FormControl />, () => ({
classes,
inheritComponent: 'div',
mount,
refInstanceof: window.HTMLDivElement,
testComponentPropWith: 'fieldset',
}));
describe('initial state', () => {
it('should have no margin', () => {
const { container } = render(<FormControl />);
const root = container.firstChild;
expect(root).not.to.have.class(classes.marginNormal);
expect(root).not.to.have.class(classes.marginDense);
});
it('can have the margin normal class', () => {
const { container } = render(<FormControl margin="normal" />);
const root = container.firstChild;
expect(root).to.have.class(classes.marginNormal);
expect(root).not.to.have.class(classes.marginDense);
});
it('can have the margin dense class', () => {
const { container } = render(<FormControl margin="dense" />);
const root = container.firstChild;
expect(root).to.have.class(classes.marginDense);
expect(root).not.to.have.class(classes.marginNormal);
});
it('should not be filled initially', () => {
const readContext = spy();
render(
<FormControl>
<TestComponent contextCallback={readContext} />
</FormControl>,
);
expect(readContext.args[0][0]).to.have.property('filled', false);
});
it('should not be focused initially', () => {
const readContext = spy();
render(
<FormControl>
<TestComponent contextCallback={readContext} />
</FormControl>,
);
expect(readContext.args[0][0]).to.have.property('focused', false);
});
});
describe('prop: required', () => {
it('should not apply it to the DOM', () => {
const { container } = render(<FormControl required />);
expect(container.firstChild).not.to.have.attribute('required');
});
});
describe('prop: disabled', () => {
it('will be unfocused if it gets disabled', () => {
const readContext = spy();
const { container, setProps } = render(
<FormControl>
<Input />
<TestComponent contextCallback={readContext} />
</FormControl>,
);
expect(readContext.args[0][0]).to.have.property('focused', false);
act(() => {
container.querySelector('input').focus();
});
expect(readContext.args[1][0]).to.have.property('focused', true);
setProps({ disabled: true });
expect(readContext.args[2][0]).to.have.property('focused', false);
});
});
describe('prop: focused', () => {
it('should display input in focused state', () => {
const readContext = spy();
const { container } = render(
<FormControl focused>
<Input />
<TestComponent contextCallback={readContext} />
</FormControl>,
);
expect(readContext.args[0][0]).to.have.property('focused', true);
container.querySelector('input').blur();
expect(readContext.args[0][0]).to.have.property('focused', true);
});
});
describe('input', () => {
it('should be filled when a value is set', () => {
const readContext = spy();
render(
<FormControl>
<Input value="bar" />
<TestComponent contextCallback={readContext} />
</FormControl>,
);
expect(readContext.args[0][0]).to.have.property('filled', true);
});
it('should be filled when a defaultValue is set', () => {
const readContext = spy();
render(
<FormControl>
<Input defaultValue="bar" />
<TestComponent contextCallback={readContext} />
</FormControl>,
);
expect(readContext.args[0][0]).to.have.property('filled', true);
});
it('should not be adornedStart with an endAdornment', () => {
const readContext = spy();
render(
<FormControl>
<Input endAdornment={<div />} />
<TestComponent contextCallback={readContext} />
</FormControl>,
);
expect(readContext.args[0][0]).to.have.property('adornedStart', false);
});
it('should be adornedStar with a startAdornment', () => {
const readContext = spy();
render(
<FormControl>
<Input startAdornment={<div />} />
<TestComponent contextCallback={readContext} />
</FormControl>,
);
expect(readContext.args[0][0]).to.have.property('adornedStart', true);
});
});
describe('select', () => {
it('should not be adorned without a startAdornment', () => {
const readContext = spy();
render(
<FormControl>
<Select value="" />
<TestComponent contextCallback={readContext} />
</FormControl>,
);
expect(readContext.args[0][0]).to.have.property('adornedStart', false);
});
it('should be adorned with a startAdornment', () => {
const readContext = spy();
render(
<FormControl>
<Select value="" input={<Input startAdornment={<div />} />} />
<TestComponent contextCallback={readContext} />
</FormControl>,
);
expect(readContext.args[0][0].adornedStart, true);
});
});
describe('useFormControl', () => {
const FormController = React.forwardRef((_, ref) => {
const formControl = useFormControl();
React.useImperativeHandle(ref, () => formControl, [formControl]);
return null;
});
const FormControlled = React.forwardRef(function FormControlled(props, ref) {
return (
<FormControl {...props}>
<FormController ref={ref} />
</FormControl>
);
});
describe('from props', () => {
it('should have the required prop from the instance', () => {
const formControlRef = React.createRef();
const { setProps } = render(<FormControlled ref={formControlRef} />);
expect(formControlRef.current).to.have.property('required', false);
setProps({ required: true });
expect(formControlRef.current).to.have.property('required', true);
});
it('should have the error prop from the instance', () => {
const formControlRef = React.createRef();
const { setProps } = render(<FormControlled ref={formControlRef} />);
expect(formControlRef.current).to.have.property('error', false);
setProps({ error: true });
expect(formControlRef.current).to.have.property('error', true);
});
it('should have the margin prop from the instance', () => {
const formControlRef = React.createRef();
const { setProps } = render(<FormControlled ref={formControlRef} />);
expect(formControlRef.current).to.have.property('margin', 'none');
setProps({ margin: 'dense' });
expect(formControlRef.current).to.have.property('margin', 'dense');
});
it('should have the fullWidth prop from the instance', () => {
const formControlRef = React.createRef();
const { setProps } = render(<FormControlled ref={formControlRef} />);
expect(formControlRef.current).to.have.property('fullWidth', false);
setProps({ fullWidth: true });
expect(formControlRef.current).to.have.property('fullWidth', true);
});
});
describe('callbacks', () => {
describe('onFilled', () => {
it('should set the filled state', () => {
const formControlRef = React.createRef();
render(<FormControlled ref={formControlRef} />);
expect(formControlRef.current).to.have.property('filled', false);
formControlRef.current.onFilled();
expect(formControlRef.current).to.have.property('filled', true);
formControlRef.current.onFilled();
expect(formControlRef.current).to.have.property('filled', true);
});
});
describe('onEmpty', () => {
it('should clean the filled state', () => {
const formControlRef = React.createRef();
render(<FormControlled ref={formControlRef} />);
formControlRef.current.onFilled();
expect(formControlRef.current).to.have.property('filled', true);
formControlRef.current.onEmpty();
expect(formControlRef.current).to.have.property('filled', false);
formControlRef.current.onEmpty();
expect(formControlRef.current).to.have.property('filled', false);
});
});
describe('handleFocus', () => {
it('should set the focused state', () => {
const formControlRef = React.createRef();
render(<FormControlled ref={formControlRef} />);
expect(formControlRef.current).to.have.property('focused', false);
formControlRef.current.onFocus();
expect(formControlRef.current).to.have.property('focused', true);
formControlRef.current.onFocus();
expect(formControlRef.current).to.have.property('focused', true);
});
});
describe('handleBlur', () => {
it('should clear the focused state', () => {
const formControlRef = React.createRef();
render(<FormControlled ref={formControlRef} />);
expect(formControlRef.current).to.have.property('focused', false);
formControlRef.current.onFocus();
expect(formControlRef.current).to.have.property('focused', true);
formControlRef.current.onBlur();
expect(formControlRef.current).to.have.property('focused', false);
formControlRef.current.onBlur();
expect(formControlRef.current).to.have.property('focused', false);
});
});
});
});
});