-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
/
useSwitch.test.tsx
120 lines (99 loc) · 3.34 KB
/
useSwitch.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
import { expect } from 'chai';
import * as React from 'react';
import { spy } from 'sinon';
import {
act,
createRenderer,
focusVisible,
programmaticFocusTriggersFocusVisible,
screen,
simulatePointerDevice,
} from '@mui-internal/test-utils';
import { useSwitch, UseSwitchParameters } from '@mui/base/useSwitch';
describe('useSwitch', () => {
const { render } = createRenderer();
const invokeUseSwitch = (props: UseSwitchParameters) => {
const ref = React.createRef<ReturnType<typeof useSwitch>>();
function TestComponent() {
const switchDefinition = useSwitch(props);
React.useImperativeHandle(ref, () => switchDefinition, [switchDefinition]);
return null;
}
render(<TestComponent />);
return ref.current!;
};
describe('getInputProps', () => {
it('should include the incoming uncontrolled props in the output', () => {
const props: UseSwitchParameters = {
defaultChecked: true,
disabled: true,
readOnly: true,
required: true,
};
const { getInputProps } = invokeUseSwitch(props);
const inputProps = getInputProps();
expect(inputProps.defaultChecked).to.equal(true);
expect(inputProps.disabled).to.equal(true);
expect(inputProps.readOnly).to.equal(true);
expect(inputProps.required).to.equal(true);
});
it('should include the incoming controlled prop in the output', () => {
const props = {
checked: true,
};
const { getInputProps } = invokeUseSwitch(props);
const inputProps = getInputProps();
expect(inputProps!.checked).to.equal(true);
});
it('should call onChange if a change event is fired', () => {
const handleChange = spy();
function Switch() {
const { getInputProps } = useSwitch({ onChange: handleChange });
return <input {...getInputProps()} />;
}
render(<Switch />);
act(() => {
screen.getByRole('switch').click();
});
expect(handleChange.callCount).to.equal(1);
});
it('should call focus event handlers if focus events are fired', () => {
const handleBlur = spy();
const handleFocus = spy();
const handleFocusVisible = spy();
function Switch() {
const { getInputProps } = useSwitch({
onBlur: handleBlur,
onFocus: handleFocus,
onFocusVisible: handleFocusVisible,
});
return <input {...getInputProps()} />;
}
render(<Switch />);
const switchElement = screen.getByRole('switch');
simulatePointerDevice();
act(() => {
switchElement.focus();
});
expect(handleBlur.callCount).to.equal(0);
expect(handleFocus.callCount).to.equal(1);
expect(handleFocusVisible.callCount).to.equal(
programmaticFocusTriggersFocusVisible() ? 1 : 0,
);
act(() => {
switchElement.blur();
});
expect(handleBlur.callCount).to.equal(1);
expect(handleFocus.callCount).to.equal(1);
expect(handleFocusVisible.callCount).to.equal(
programmaticFocusTriggersFocusVisible() ? 1 : 0,
);
focusVisible(switchElement);
expect(handleBlur.callCount).to.equal(1);
expect(handleFocus.callCount).to.equal(2);
expect(handleFocusVisible.callCount).to.equal(
programmaticFocusTriggersFocusVisible() ? 2 : 1,
);
});
});
});