forked from mui/material-ui
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathListItemButton.test.js
260 lines (211 loc) · 8.01 KB
/
ListItemButton.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
import * as React from 'react';
import { expect } from 'chai';
import { act, createRenderer, fireEvent } from '@mui/internal-test-utils';
import ListItemButton, { listItemButtonClasses as classes } from '@mui/material/ListItemButton';
import ButtonBase from '@mui/material/ButtonBase';
import { ThemeProvider, createTheme } from '@mui/material/styles';
import ListContext from '../List/ListContext';
import describeConformance from '../../test/describeConformance';
describe('<ListItemButton />', () => {
const { render } = createRenderer();
describeConformance(<ListItemButton />, () => ({
classes,
inheritComponent: ButtonBase,
render,
refInstanceof: window.HTMLDivElement,
testComponentPropWith: 'a',
muiName: 'MuiListItemButton',
testVariantProps: { dense: true },
skip: ['componentsProp'],
}));
it('should render with gutters classes', () => {
const { getByRole } = render(<ListItemButton />);
expect(getByRole('button')).to.have.class(classes.gutters);
});
it('should render with the selected class', () => {
const { getByRole } = render(<ListItemButton selected />);
expect(getByRole('button')).to.have.class(classes.selected);
});
it('should disable the gutters', () => {
const { getByRole } = render(<ListItemButton disableGutters />);
expect(getByRole('button')).not.to.have.class(classes.gutters);
});
describe('context: dense', () => {
it('should forward the context', () => {
let context = null;
const { setProps } = render(
<ListItemButton>
<ListContext.Consumer>
{(options) => {
context = options;
}}
</ListContext.Consumer>
</ListItemButton>,
);
expect(context).to.have.property('dense', false);
setProps({ dense: true });
expect(context).to.have.property('dense', true);
});
});
describe('prop: focusVisibleClassName', () => {
before(function beforeCallback() {
if (/jsdom/.test(window.navigator.userAgent)) {
// JSDOM doesn't support :focus-visible
this.skip();
}
});
it('should merge the class names', async () => {
const { getByRole } = render(
<ListItemButton focusVisibleClassName="focusVisibleClassName" />,
);
const button = getByRole('button');
fireEvent.keyDown(document.activeElement || document.body, { key: 'Tab' });
await act(async () => {
button.focus();
});
expect(button).to.have.class('focusVisibleClassName');
expect(button).to.have.class(classes.focusVisible);
});
});
describe('prop: href', () => {
const href = 'example.com';
it('should rendered as link without specifying component="a"', () => {
const { getByRole } = render(<ListItemButton href={href} />);
const link = getByRole('link');
expect(!!link).to.equal(true);
});
it('should rendered as link when specifying component="div"', () => {
const { getByRole } = render(<ListItemButton href={href} component="div" />);
const link = getByRole('link');
expect(!!link).to.equal(true);
});
it('should rendered as link when specifying component="a"', () => {
const { getByRole } = render(<ListItemButton href={href} component="a" />);
const link = getByRole('link');
expect(!!link).to.equal(true);
});
it('should rendered as specifying component', () => {
const { getByRole } = render(<ListItemButton href={href} component="h1" />);
const heading = getByRole('heading');
expect(!!heading).to.equal(true);
});
});
describe('prop: to', () => {
const to = 'example.com';
it('should rendered as link without specifying component="a"', () => {
const { getByRole } = render(<ListItemButton to={to} />);
const link = getByRole('link');
expect(!!link).to.equal(true);
});
it('should rendered as link when specifying component="div"', () => {
const { getByRole } = render(<ListItemButton to={to} component="div" />);
const link = getByRole('link');
expect(!!link).to.equal(true);
});
it('should rendered as link when specifying component="a"', () => {
const { getByRole } = render(<ListItemButton to={to} component="a" />);
const link = getByRole('link');
expect(!!link).to.equal(true);
});
it('should rendered as specifying component', () => {
const { getByRole } = render(<ListItemButton to={to} component="h1" />);
const heading = getByRole('heading');
expect(!!heading).to.equal(true);
});
});
describe('prop: LinkComponent', () => {
const href = 'example.com';
const customLinkId = 'customLink';
const CustomLink = React.forwardRef((props, ref) => {
// eslint-disable-next-line jsx-a11y/anchor-has-content
return <a data-testid={customLinkId} ref={ref} {...props} />;
});
it('should rendered as LinkComponent when href is provided', () => {
const { container, getByTestId } = render(
<ListItemButton href={href} LinkComponent={CustomLink} />,
);
const button = container.firstChild;
expect(getByTestId(customLinkId)).not.to.equal(null);
expect(button).to.have.property('nodeName', 'A');
expect(button).to.have.attribute('href', href);
});
it('should ignore LinkComponent is component is provided', () => {
const { container, queryByTestId } = render(
<ListItemButton href={href} LinkComponent={CustomLink} component="h1" />,
);
const button = container.firstChild;
expect(queryByTestId(customLinkId)).to.equal(null);
expect(button).to.have.property('nodeName', 'H1');
expect(button).to.have.attribute('href', href);
});
it('should rendered as LinkComponent (from theme) when href is provided', () => {
const theme = createTheme({
components: {
MuiListItemButton: {
defaultProps: {
LinkComponent: CustomLink,
},
},
},
});
const { container, getByTestId } = render(
<ThemeProvider theme={theme}>
<ListItemButton href={href} />,
</ThemeProvider>,
);
const button = container.firstChild;
expect(getByTestId(customLinkId)).not.to.equal(null);
expect(button).to.have.property('nodeName', 'A');
expect(button).to.have.attribute('href', href);
});
it('should rendered as LinkComponent (from theme MuiButtonBase) when href is provided', () => {
const theme = createTheme({
components: {
MuiButtonBase: {
defaultProps: {
LinkComponent: CustomLink,
},
},
},
});
const { container, getByTestId } = render(
<ThemeProvider theme={theme}>
<ListItemButton href={href} />,
</ThemeProvider>,
);
const button = container.firstChild;
expect(getByTestId(customLinkId)).not.to.equal(null);
expect(button).to.have.property('nodeName', 'A');
expect(button).to.have.attribute('href', href);
});
it('should prefer LinkComponent from MuiListItemButton over MuiButtonBase', () => {
const WrongCustomLink = React.forwardRef((props, ref) => {
// eslint-disable-next-line jsx-a11y/anchor-has-content
return <a data-testid="wrong-link" ref={ref} {...props} />;
});
const theme = createTheme({
components: {
MuiListItemButton: {
defaultProps: {
LinkComponent: CustomLink,
},
},
MuiButtonBase: {
defaultProps: {
LinkComponent: WrongCustomLink,
},
},
},
});
const { container, getByTestId } = render(
<ThemeProvider theme={theme}>
<ListItemButton href={href} />,
</ThemeProvider>,
);
const button = container.firstChild;
expect(getByTestId(customLinkId)).not.to.equal(null);
expect(button).to.have.property('nodeName', 'A');
expect(button).to.have.attribute('href', href);
});
});
});