/
SwitchBase.spec.js
390 lines (328 loc) · 11.3 KB
/
SwitchBase.spec.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// @flow
import React from 'react';
import { assert } from 'chai';
import { spy } from 'sinon';
import { createShallow, createMount, getClasses, unwrap } from '../test-utils';
import SwitchBase from './SwitchBase';
import IconButton from '../IconButton';
import Icon from '../Icon';
function assertIsChecked(wrapper) {
const iconButton = wrapper.find('span').at(0);
assert.strictEqual(
iconButton.hasClass('test-class-checked'),
true,
'should have the checked class on the root node',
);
const input = wrapper.find('input');
assert.strictEqual(input.instance().checked, true, 'the DOM node should be checked');
const label = iconButton.childAt(0);
const icon = label.childAt(0);
assert.strictEqual(icon.is('h2'), true, 'should be the checked icon');
}
function assertIsNotChecked(wrapper) {
const iconButton = wrapper.find('span').at(0);
assert.strictEqual(
iconButton.hasClass('test-class-checked'),
false,
'should not have the checked class on the root node',
);
const input = wrapper.find('input');
assert.strictEqual(input.instance().checked, false, 'the DOM node should not be checked');
const label = iconButton.childAt(0);
const icon = label.childAt(0);
assert.strictEqual(icon.is('h1'), true, 'should be the icon');
}
describe('<SwitchBase />', () => {
let shallow;
let mount;
let classes;
let SwitchBaseNaked;
const defaultProps = {
icon: <h1>h1</h1>,
checkedIcon: <h2>h2</h2>,
};
before(() => {
SwitchBaseNaked = unwrap(SwitchBase);
shallow = createShallow({ dive: true });
mount = createMount();
classes = getClasses(<SwitchBase {...defaultProps} />);
});
after(() => {
mount.cleanUp();
});
it('should render an IconButton', () => {
const wrapper = shallow(<SwitchBase {...defaultProps} />);
assert.strictEqual(wrapper.type(), IconButton);
});
it('should render an icon and input inside the button by default', () => {
const wrapper = shallow(<SwitchBase {...defaultProps} />);
assert.strictEqual(wrapper.childAt(0).is('h1'), true, 'should be the icon');
assert.strictEqual(
wrapper.childAt(1).is('input[type="checkbox"]'),
true,
'should be a checkbox input',
);
});
it('should have a ripple by default', () => {
const wrapper = shallow(<SwitchBase {...defaultProps} />);
assert.strictEqual(wrapper.props().disableRipple, undefined);
});
it('should pass disableRipple={true} to IconButton', () => {
const wrapper = shallow(<SwitchBase {...defaultProps} disableRipple />);
assert.strictEqual(wrapper.props().disableRipple, true, 'should set disableRipple to true');
});
// className is put on the root node, this is a special case!
it('should render with the user and root classes', () => {
const wrapper = shallow(<SwitchBase {...defaultProps} className="woofSwitchBase" />);
assert.strictEqual(wrapper.hasClass('woofSwitchBase'), true);
assert.strictEqual(wrapper.hasClass(classes.root), true);
});
it('should spread custom props on the root node', () => {
const wrapper = shallow(<SwitchBase {...defaultProps} data-my-prop="woofSwitchBase" />);
assert.strictEqual(
wrapper.props()['data-my-prop'],
'woofSwitchBase',
'custom prop should be woofSwitchBase',
);
});
it('should pass tabIndex to the input so it can be taken out of focus rotation', () => {
const wrapper = shallow(<SwitchBase {...defaultProps} tabIndex={-1} />);
const input = wrapper.find('input');
assert.strictEqual(input.props().tabIndex, -1);
});
it('should pass value, disabled, checked, and name to the input', () => {
const props = { name: 'gender', disabled: true, value: 'male' };
const wrapper = shallow(<SwitchBase {...defaultProps} {...props} />);
const input = wrapper.find('input');
Object.keys(props).forEach(n => {
assert.strictEqual(input.props()[n], props[n]);
});
});
it('should disable the components, and render the IconButton with the disabled className', () => {
const wrapper = shallow(<SwitchBase {...defaultProps} disabled />);
assert.strictEqual(wrapper.props().disabled, true, 'should disable the root node');
assert.strictEqual(wrapper.childAt(1).props().disabled, true, 'should disable the input node');
});
it('should apply the custom disabled className when disabled', () => {
const disabledClassName = 'foo';
const wrapperA = shallow(
<SwitchBase {...defaultProps} disabled classes={{ disabled: disabledClassName }} />,
);
assert.strictEqual(
wrapperA.hasClass(disabledClassName),
true,
'should have the custom disabled class',
);
wrapperA.setProps({ disabled: false });
wrapperA.setProps({ checked: true });
assert.strictEqual(
wrapperA.hasClass(disabledClassName),
false,
'should not have the custom disabled class',
);
});
describe('controlled', () => {
let wrapper;
beforeEach(() => {
wrapper = mount(
<SwitchBaseNaked
{...defaultProps}
classes={{
checked: 'test-class-checked',
}}
className="test-class"
checked={false}
/>,
);
});
it('should recognize a controlled input', () => {
assert.strictEqual(
wrapper.instance().isControlled,
true,
'should set instance.isControlled to true',
);
assertIsNotChecked(wrapper);
});
it('should check the checkbox', () => {
wrapper.setProps({ checked: true });
assertIsChecked(wrapper);
});
it('should uncheck the checkbox', () => {
wrapper.setProps({ checked: true });
wrapper.setProps({ checked: false });
assertIsNotChecked(wrapper);
});
});
describe('prop: defaultChecked', () => {
it('should work uncontrolled', () => {
const wrapper = mount(<SwitchBaseNaked {...defaultProps} classes={{}} defaultChecked />);
wrapper
.find('input')
.instance()
.click();
wrapper.update();
assertIsNotChecked(wrapper);
});
});
describe('uncontrolled', () => {
let wrapper;
beforeEach(() => {
wrapper = mount(
<SwitchBaseNaked
{...defaultProps}
classes={{
checked: 'test-class-checked',
}}
className="test-class"
/>,
);
});
it('should recognize an uncontrolled input', () => {
assert.strictEqual(wrapper.instance().isControlled, false);
assertIsNotChecked(wrapper);
});
it('should check the checkbox', () => {
wrapper
.find('input')
.instance()
.click();
wrapper.update();
assertIsChecked(wrapper);
});
it('should uncheck the checkbox', () => {
wrapper
.find('input')
.instance()
.click();
wrapper
.find('input')
.instance()
.click();
wrapper.update();
assertIsNotChecked(wrapper);
});
});
describe('prop: icon', () => {
it('should render an Icon', () => {
const wrapper = shallow(<SwitchBase {...defaultProps} icon={<Icon>heart</Icon>} />);
assert.strictEqual(wrapper.childAt(0).is(Icon), true);
});
});
describe('handleInputChange()', () => {
const event = {
target: {
checked: false,
},
};
it('should call onChange exactly once with event', () => {
const onChangeSpy = spy();
const wrapper = mount(
<SwitchBaseNaked {...defaultProps} classes={{}} onChange={onChangeSpy} />,
);
const instance = wrapper.instance();
instance.handleInputChange(event);
assert.strictEqual(onChangeSpy.callCount, 1);
assert.strictEqual(onChangeSpy.calledWith(event), true);
onChangeSpy.resetHistory();
});
describe('controlled', () => {
it('should call onChange once', () => {
const checked = true;
const onChangeSpy = spy();
const wrapper = mount(
<SwitchBaseNaked
{...defaultProps}
classes={{}}
checked={checked}
onChange={onChangeSpy}
/>,
);
const instance = wrapper.instance();
instance.handleInputChange(event);
assert.strictEqual(onChangeSpy.callCount, 1);
assert.strictEqual(
onChangeSpy.calledWith(event, !checked),
true,
'call onChange with event and !props.checked',
);
});
});
describe('not controlled no input', () => {
let checkedMock;
let wrapper;
let onChangeSpy;
before(() => {
onChangeSpy = spy();
wrapper = mount(<SwitchBaseNaked {...defaultProps} classes={{}} onChange={onChangeSpy} />);
checkedMock = true;
const instance = wrapper.instance();
wrapper.setState({ checked: checkedMock });
instance.handleInputChange(event);
});
it('should call onChange exactly once', () => {
assert.strictEqual(onChangeSpy.callCount, 1);
});
it('should call onChange with right params', () => {
assert.strictEqual(onChangeSpy.calledWith(event, !checkedMock), true);
});
it('should change state.checked !checkedMock', () => {
assert.strictEqual(wrapper.state('checked'), !checkedMock);
});
});
describe('prop: inputProps', () => {
it('should be able to add aria', () => {
const wrapper2 = shallow(
<SwitchBase {...defaultProps} inputProps={{ 'aria-label': 'foo' }} />,
);
assert.strictEqual(wrapper2.find('input').props()['aria-label'], 'foo');
});
});
describe('prop: id', () => {
it('should be able to add id to a checkbox input', () => {
const wrapper2 = shallow(<SwitchBase {...defaultProps} type="checkbox" id="foo" />);
assert.strictEqual(wrapper2.find('input').props().id, 'foo');
});
it('should be able to add id to a radio input', () => {
const wrapper2 = shallow(<SwitchBase {...defaultProps} type="radio" id="foo" />);
assert.strictEqual(wrapper2.find('input').props().id, 'foo');
});
});
});
describe('with muiFormControl context', () => {
let wrapper;
let muiFormControl;
function setFormControlContext(muiFormControlContext) {
muiFormControl = muiFormControlContext;
wrapper.setContext({ ...wrapper.context(), muiFormControl });
}
beforeEach(() => {
wrapper = shallow(<SwitchBase {...defaultProps} />);
});
describe('enabled', () => {
beforeEach(() => {
setFormControlContext({});
});
it('should not have the disabled class', () => {
assert.strictEqual(wrapper.hasClass(classes.disabled), false);
});
it('should be overridden by props', () => {
assert.strictEqual(wrapper.hasClass(classes.disabled), false);
wrapper.setProps({ disabled: true });
assert.strictEqual(wrapper.hasClass(classes.disabled), true);
});
});
describe('disabled', () => {
beforeEach(() => {
setFormControlContext({ disabled: true });
});
it('should have the disabled class', () => {
assert.strictEqual(wrapper.hasClass(classes.disabled), true);
});
it('should honor props', () => {
assert.strictEqual(wrapper.hasClass(classes.disabled), true);
wrapper.setProps({ disabled: false });
assert.strictEqual(wrapper.hasClass(classes.disabled), false);
});
});
});
});