-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
/
useButton.test.tsx
174 lines (142 loc) · 6 KB
/
useButton.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
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
import * as React from 'react';
import { useButton } from '@mui/base/ButtonUnstyled';
import { createRenderer, fireEvent } from 'test/utils';
import { expect } from 'chai';
import { spy } from 'sinon';
describe('useButton', () => {
const { render } = createRenderer();
describe('state: active', () => {
describe('when using a button element', () => {
it('is set when triggered by mouse', () => {
function TestComponent() {
const buttonRef = React.useRef(null);
const { active, getRootProps } = useButton({ ref: buttonRef });
return <button {...getRootProps()} className={active ? 'active' : ''} />;
}
const { getByRole } = render(<TestComponent />);
const button = getByRole('button');
fireEvent.mouseDown(button);
expect(button).to.have.class('active');
fireEvent.mouseUp(button);
expect(button).not.to.have.class('active');
});
it('is set when triggered by keyboard', () => {
function TestComponent() {
const buttonRef = React.useRef(null);
const { active, getRootProps } = useButton({ ref: buttonRef });
return <button {...getRootProps()} className={active ? 'active' : ''} />;
}
const { getByRole } = render(<TestComponent />);
const button = getByRole('button');
button.focus();
fireEvent.keyDown(button, { key: ' ' });
expect(button).to.have.class('active');
fireEvent.keyUp(button, { key: ' ' });
expect(button).not.to.have.class('active');
});
it('is set when clicked on an element inside the button', () => {
function TestComponent() {
const buttonRef = React.useRef(null);
const { active, getRootProps } = useButton({ ref: buttonRef });
return (
<button {...getRootProps()} className={active ? 'active' : ''}>
<span>Click here</span>
</button>
);
}
const { getByText, getByRole } = render(<TestComponent />);
const span = getByText('Click here');
const button = getByRole('button');
fireEvent.mouseDown(span);
expect(button).to.have.class('active');
});
it('is unset when mouse button is released above another element', () => {
function TestComponent() {
const buttonRef = React.useRef(null);
const { active, getRootProps } = useButton({ ref: buttonRef });
return (
<div data-testid="parent">
<button {...getRootProps()} className={active ? 'active' : ''} />
</div>
);
}
const { getByRole, getByTestId } = render(<TestComponent />);
const button = getByRole('button');
const background = getByTestId('parent');
fireEvent.mouseDown(button);
expect(button).to.have.class('active');
fireEvent.mouseUp(background);
expect(button).not.to.have.class('active');
});
});
describe('when using a span element', () => {
it('is set when triggered by mouse', () => {
function TestComponent() {
const buttonRef = React.useRef(null);
const { active, getRootProps } = useButton({ ref: buttonRef });
return <span {...getRootProps()} className={active ? 'active' : ''} />;
}
const { getByRole } = render(<TestComponent />);
const button = getByRole('button');
fireEvent.mouseDown(button);
expect(button).to.have.class('active');
fireEvent.mouseUp(button);
expect(button).not.to.have.class('active');
});
it('is set when triggered by keyboard', () => {
function TestComponent() {
const buttonRef = React.useRef(null);
const { active, getRootProps } = useButton({ ref: buttonRef });
return <span {...getRootProps()} className={active ? 'active' : ''} />;
}
const { getByRole } = render(<TestComponent />);
const button = getByRole('button');
button.focus();
fireEvent.keyDown(button, { key: ' ' });
expect(button).to.have.class('active');
fireEvent.keyUp(button, { key: ' ' });
expect(button).not.to.have.class('active');
});
});
describe('event handlers', () => {
interface WithClickHandler {
onClick: React.MouseEventHandler;
}
it('calls them when provided in props', () => {
function TestComponent(props: WithClickHandler) {
const ref = React.useRef(null);
const { getRootProps } = useButton({ ...props, ref });
return <button {...getRootProps()} />;
}
const handleClick = spy();
const { getByRole } = render(<TestComponent onClick={handleClick} />);
fireEvent.click(getByRole('button'));
expect(handleClick.callCount).to.equal(1);
});
it('calls them when provided in getRootProps()', () => {
const handleClick = spy();
function TestComponent() {
const ref = React.useRef(null);
const { getRootProps } = useButton({ ref });
return <button {...getRootProps({ onClick: handleClick })} />;
}
const { getByRole } = render(<TestComponent />);
fireEvent.click(getByRole('button'));
expect(handleClick.callCount).to.equal(1);
});
it('calls the one provided in getRootProps() when both props and getRootProps have ones', () => {
const handleClickExternal = spy();
const handleClickInternal = spy();
function TestComponent(props: WithClickHandler) {
const ref = React.useRef(null);
const { getRootProps } = useButton({ ...props, ref });
return <button {...getRootProps({ onClick: handleClickInternal })} />;
}
const { getByRole } = render(<TestComponent onClick={handleClickExternal} />);
fireEvent.click(getByRole('button'));
expect(handleClickInternal.callCount).to.equal(1);
expect(handleClickExternal.callCount).to.equal(0);
});
});
});
});