forked from mui/material-ui
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SliderUnstyled.test.js
328 lines (282 loc) · 9.48 KB
/
SliderUnstyled.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
324
325
326
327
328
import SliderUnstyled, { sliderUnstyledClasses as classes } from '@mui/base/SliderUnstyled';
import { expect } from 'chai';
import * as React from 'react';
import { spy, stub } from 'sinon';
import {
createRenderer,
createMount,
describeConformanceUnstyled,
fireEvent,
screen,
} from 'test/utils';
function createTouches(touches) {
return {
changedTouches: touches.map(
(touch) =>
new Touch({
target: document.body,
...touch,
}),
),
};
}
describe('<SliderUnstyled />', () => {
before(function beforeHook() {
if (typeof Touch === 'undefined') {
this.skip();
}
});
const mount = createMount();
const { render } = createRenderer();
describeConformanceUnstyled(<SliderUnstyled value={0} />, () => ({
classes,
inheritComponent: 'span',
render,
mount,
refInstanceof: window.HTMLSpanElement,
testComponentPropWith: 'div',
muiName: 'MuiSlider',
slots: {
root: {
expectedClassName: classes.root,
},
thumb: {
expectedClassName: classes.thumb,
},
track: {
expectedClassName: classes.track,
},
rail: {
expectedClassName: classes.rail,
},
},
}));
it('forwards style props on the Root component', () => {
let ownerState = null;
let theme = null;
const Root = React.forwardRef(
({ ownerState: ownerStateProp, theme: themeProp, ...other }, ref) => {
ownerState = ownerStateProp;
theme = themeProp;
return <span ref={ref} {...other} />;
},
);
render(<SliderUnstyled components={{ Root }} />);
expect(ownerState).not.to.equal(null);
expect(theme).not.to.equal(null);
});
it('does not forward style props as DOM attributes if component slot is primitive', () => {
const elementRef = React.createRef();
render(
<SliderUnstyled
components={{
Root: 'span',
}}
ref={elementRef}
/>,
);
const { current: element } = elementRef;
expect(element.getAttribute('ownerState')).to.equal(null);
expect(element.getAttribute('theme')).to.equal(null);
});
describe('prop: marks', () => {
it('does not cause unknown-prop error', () => {
const marks = [
{
value: 33,
},
];
expect(() => {
render(<SliderUnstyled marks={marks} />);
}).not.to.throw();
});
});
describe('prop: orientation', () => {
it('sets the orientation via ARIA', () => {
render(<SliderUnstyled orientation="vertical" />);
const slider = screen.getByRole('slider');
expect(slider).to.have.attribute('aria-orientation', 'vertical');
});
it('does not set the orientation via appearance for WebKit browsers', function test() {
if (/jsdom/.test(window.navigator.userAgent) || !/WebKit/.test(window.navigator.userAgent)) {
this.skip();
}
render(<SliderUnstyled orientation="vertical" />);
const slider = screen.getByRole('slider');
expect(slider).to.have.property('tagName', 'INPUT');
expect(slider).to.have.property('type', 'range');
// Only relevant if we implement `[role="slider"]` with `input[type="range"]`
// We're not setting this by default because it changes horizontal keyboard navigation in WebKit: https://bugs.chromium.org/p/chromium/issues/detail?id=1162640
expect(slider).not.toHaveComputedStyle({ webkitAppearance: 'slider-vertical' });
});
});
describe('prop: valueLabelDisplay', () => {
it('renders a slider', () => {
render(<SliderUnstyled value={30} valueLabelDisplay="auto" />);
expect(screen.getByRole('slider')).to.have.attribute('aria-valuenow', '30');
});
});
[
['readonly range', Object.freeze([2, 1])],
['range', [2, 1]],
].forEach(([valueLabel, value]) => {
it(`calls onChange even if the ${valueLabel} did not change`, () => {
const handleChange = spy();
const { container } = render(
<SliderUnstyled min={0} max={5} onChange={handleChange} value={value} />,
);
stub(container.firstChild, 'getBoundingClientRect').callsFake(() => ({
width: 100,
height: 10,
bottom: 10,
left: 0,
}));
// pixel: 0 20 40 60 80 100
// slider: |---|---|---|---|---|
// values: 0 1 2 3 4 5
// value: ↑ ↑
// mouse: ↑
fireEvent.mouseDown(container.firstChild, {
buttons: 1,
clientX: 41,
});
expect(handleChange.callCount).to.equal(1);
expect(handleChange.args[0][1]).not.to.equal(value);
expect(handleChange.args[0][1]).to.deep.equal(value.slice().sort((a, b) => a - b));
});
});
describe('prop: disabled', () => {
it('should render the disabled classes', () => {
const { container, getByRole } = render(<SliderUnstyled disabled value={0} />);
expect(container.firstChild).to.have.class(classes.disabled);
expect(getByRole('slider')).not.to.have.attribute('tabIndex');
});
it('should not respond to drag events after becoming disabled', function test() {
// TODO: Don't skip once a fix for https://github.com/jsdom/jsdom/issues/3029 is released.
if (/jsdom/.test(window.navigator.userAgent)) {
this.skip();
}
const { getByRole, setProps, container } = render(<SliderUnstyled defaultValue={0} />);
stub(container.firstChild, 'getBoundingClientRect').callsFake(() => ({
width: 100,
height: 10,
bottom: 10,
left: 0,
}));
fireEvent.touchStart(
container.firstChild,
createTouches([{ identifier: 1, clientX: 21, clientY: 0 }]),
);
const thumb = getByRole('slider');
expect(thumb).to.have.attribute('aria-valuenow', '21');
expect(thumb).toHaveFocus();
setProps({ disabled: true });
expect(thumb).not.toHaveFocus();
expect(thumb).not.to.have.class(classes.active);
fireEvent.touchMove(
container.firstChild,
createTouches([{ identifier: 1, clientX: 30, clientY: 0 }]),
);
expect(thumb).to.have.attribute('aria-valuenow', '21');
});
it('should not respond to drag events if disabled', function test() {
// TODO: Don't skip once a fix for https://github.com/jsdom/jsdom/issues/3029 is released.
if (/jsdom/.test(window.navigator.userAgent)) {
this.skip();
}
const { getByRole, container } = render(<SliderUnstyled disabled defaultValue={21} />);
const thumb = getByRole('slider');
stub(container.firstChild, 'getBoundingClientRect').callsFake(() => ({
width: 100,
height: 10,
bottom: 10,
left: 0,
}));
fireEvent.touchStart(
container.firstChild,
createTouches([{ identifier: 1, clientX: 21, clientY: 0 }]),
);
fireEvent.touchMove(
document.body,
createTouches([{ identifier: 1, clientX: 30, clientY: 0 }]),
);
fireEvent.touchEnd(
document.body,
createTouches([{ identifier: 1, clientX: 30, clientY: 0 }]),
);
expect(thumb).to.have.attribute('aria-valuenow', '21');
});
});
describe('marks', () => {
it('should not render marks that are out of min&max bounds', function test() {
if (/jsdom/.test(window.navigator.userAgent)) {
this.skip();
}
const { container } = render(
<SliderUnstyled
marks={[
{
value: -1,
label: -1,
},
{
value: 0,
label: 0,
},
{
value: 100,
label: 100,
},
{
value: 120,
label: 120,
},
]}
/>,
);
expect(container.querySelectorAll(`.${classes.markLabel}`).length).to.equal(2);
expect(container.querySelectorAll(`.${classes.mark}`).length).to.equal(2);
expect(container.querySelectorAll(`.${classes.markLabel}`)[0].textContent).to.equal('0');
expect(container.querySelectorAll(`.${classes.markLabel}`)[1].textContent).to.equal('100');
});
});
describe('ARIA', () => {
it('should have the correct aria attributes', () => {
const { getByRole, container } = render(
<SliderUnstyled
value={50}
valueLabelDisplay="auto"
marks={[
{
value: 0,
label: 0,
},
{
value: 50,
label: 50,
},
{
value: 100,
label: 100,
},
]}
aria-label="a slider"
aria-labelledby="a slider label"
/>,
);
const sliderWrapperElement = container.firstChild;
const slider = getByRole('slider');
const markLabels = container.querySelectorAll(`.${classes.markLabel}`);
const input = container.querySelector('input');
expect(slider).to.have.attribute('aria-valuemin', '0');
expect(slider).to.have.attribute('aria-valuemax', '100');
expect(slider).to.have.attribute('aria-valuenow', '50');
expect(slider).to.have.attribute('aria-labelledby');
expect(markLabels[0]).to.have.attribute('aria-hidden', 'true');
expect(sliderWrapperElement).not.to.have.attribute('aria-labelledby');
expect(input).to.have.attribute('aria-labelledby', 'a slider label');
expect(input).to.have.attribute('aria-label', 'a slider');
expect(input).to.have.attribute('aria-valuenow', '50');
});
});
});