-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
/
Copy pathIconButton.test.js
214 lines (179 loc) · 7.03 KB
/
IconButton.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
import * as React from 'react';
import { expect } from 'chai';
import PropTypes from 'prop-types';
import { createRenderer, reactMajor, screen, within } from '@mui/internal-test-utils';
import capitalize from '@mui/utils/capitalize';
import { ThemeProvider, createTheme } from '@mui/material/styles';
import IconButton, { iconButtonClasses as classes } from '@mui/material/IconButton';
import Icon from '@mui/material/Icon';
import ButtonBase from '@mui/material/ButtonBase';
import describeConformance from '../../test/describeConformance';
import * as ripple from '../../test/ripple';
describe('<IconButton />', () => {
const { render } = createRenderer();
describeConformance(<IconButton>book</IconButton>, () => ({
classes,
inheritComponent: ButtonBase,
render,
refInstanceof: window.HTMLButtonElement,
muiName: 'MuiIconButton',
testVariantProps: { edge: 'end', disabled: true },
skip: ['componentProp', 'componentsProp'],
}));
it('should render Icon children with right classes', () => {
const childClassName = 'child-woof';
const iconChild = <Icon data-testid="icon" className={childClassName} />;
const { getByTestId } = render(<IconButton>{iconChild}</IconButton>);
expect(getByTestId('icon')).to.have.class(childClassName);
});
it('should have a ripple', async () => {
const { container, getByRole } = render(
<IconButton TouchRippleProps={{ className: 'touch-ripple' }}>book</IconButton>,
);
await ripple.startTouch(getByRole('button'));
expect(container.querySelector('.touch-ripple')).not.to.equal(null);
});
it('can disable the ripple and hover effect', async () => {
const { container, getByRole } = render(
<IconButton disableRipple TouchRippleProps={{ className: 'touch-ripple' }}>
book
</IconButton>,
);
await ripple.startTouch(getByRole('button'));
expect(container.querySelector('.touch-ripple')).to.equal(null);
});
describe('prop: size', () => {
it('should render the right class', () => {
let root;
root = render(<IconButton size="small">book</IconButton>).container.firstChild;
expect(root).to.have.class(classes.sizeSmall);
root = render(<IconButton size="medium">book</IconButton>).container.firstChild;
expect(root).not.to.have.class(classes.sizeSmall);
root = render(<IconButton size="large">book</IconButton>).container.firstChild;
expect(root).to.have.class(classes.sizeLarge);
root = render(<IconButton>book</IconButton>).container.firstChild;
expect(root).not.to.have.class(classes.sizeSmall);
expect(root).not.to.have.class(classes.sizeLarge);
});
});
describe('prop: edge', () => {
it('edge="start" should render the right class', () => {
const { container } = render(<IconButton edge="start">book</IconButton>);
expect(container.firstChild).to.have.class(classes.edgeStart);
});
it('edge="end" should render the right class', () => {
const { container } = render(<IconButton edge="end">book</IconButton>);
expect(container.firstChild).to.have.class(classes.edgeEnd);
});
it('no edge should render the right class', () => {
const { container } = render(<IconButton>book</IconButton>);
expect(container.firstChild).not.to.have.class(classes.edgeStart);
expect(container.firstChild).not.to.have.class(classes.edgeEnd);
});
});
describe('prop: disabled', () => {
it('should disable the component', () => {
const { getByRole } = render(<IconButton disabled>book</IconButton>);
const button = getByRole('button');
expect(button).to.have.property('disabled', true);
expect(button).to.have.class(classes.disabled);
});
});
describe('prop: color', () => {
['primary', 'secondary', 'error', 'info', 'success', 'warning'].forEach((color) => {
it(`should render the ${color} class`, () => {
const { getByRole } = render(<IconButton color={color}>Hello World</IconButton>);
const button = getByRole('button');
expect(button).to.have.class(classes[`color${capitalize(color)}`]);
});
});
});
it('should raise a warning about onClick in children because of Firefox', function test() {
if (reactMajor >= 19) {
// React 19 removed prop types support
this.skip();
}
expect(() => {
PropTypes.checkPropTypes(
IconButton.propTypes,
{ classes: {}, children: <svg onClick={() => {}} /> },
'prop',
'MockedName',
);
}).toErrorDev(['MUI: You are providing an onClick event listener']);
});
it('should not throw error for a custom color', () => {
expect(() => (
<ThemeProvider
theme={createTheme({
components: {
MuiIconButton: {
defaultProps: {
color: 'custom',
},
},
},
})}
>
<IconButton />
</ThemeProvider>
)).not.to.throw();
});
it('should disable ripple if disableRipple:true is set in MuiButtonBase', async () => {
const { container, getByRole } = render(
<ThemeProvider
theme={createTheme({
components: {
MuiButtonBase: {
defaultProps: {
disableRipple: true,
},
},
},
})}
>
<IconButton TouchRippleProps={{ className: 'touch-ripple' }}>book</IconButton>,
</ThemeProvider>,
);
await ripple.startTouch(getByRole('button'));
expect(container.querySelector('.touch-ripple')).to.equal(null);
});
describe('prop: loading', () => {
it('does not render the wrapper by default', () => {
render(<IconButton />);
const button = screen.getByRole('button');
expect(button).to.have.property('disabled', false);
expect(button.firstChild).to.equal(null);
});
it('disables the button', () => {
render(<IconButton loading />);
const button = screen.getByRole('button');
expect(button).to.have.property('tabIndex', -1);
expect(button).to.have.property('disabled', true);
});
it('cannot be enabled while `loading`', () => {
render(<IconButton disabled={false} loading />);
expect(screen.getByRole('button')).to.have.property('disabled', true);
});
it('renders a progressbar that is labelled by the button', () => {
render(<IconButton loading>Submit</IconButton>);
const button = screen.getByRole('button');
const progressbar = within(button).getByRole('progressbar');
expect(progressbar).toHaveAccessibleName('Submit');
});
});
describe('prop: loadingIndicator', () => {
it('is not rendered by default', () => {
render(<IconButton loadingIndicator="loading">Test</IconButton>);
expect(screen.getByRole('button')).to.have.text('Test');
});
it('is rendered before the children when `loading`', () => {
render(
<IconButton loadingIndicator="loading…" loading>
Test
</IconButton>,
);
expect(screen.getByRole('button')).to.have.text('loading…Test');
});
});
});