-
-
Notifications
You must be signed in to change notification settings - Fork 32.6k
Expand file tree
/
Copy pathMenuButton.test.tsx
More file actions
233 lines (197 loc) · 7.31 KB
/
MenuButton.test.tsx
File metadata and controls
233 lines (197 loc) · 7.31 KB
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
import * as React from 'react';
import { expect } from 'chai';
import { spy } from 'sinon';
import userEvent from '@testing-library/user-event';
import { act, createRenderer } from '@mui/internal-test-utils';
import { MenuButton, menuButtonClasses } from '@mui/base/MenuButton';
import { DropdownContext, DropdownContextValue, DropdownActionTypes } from '@mui/base/useDropdown';
import { describeConformanceUnstyled } from '../../test/describeConformanceUnstyled';
// TODO v6: initialize @testing-library/user-event using userEvent.setup() instead of directly calling methods e.g. userEvent.click() for all related tests in this file
// currently the setup() method uses the ClipboardEvent constructor which is incompatible with our lowest supported version of iOS Safari (12.2) https://github.com/mui/material-ui/blob/master/.browserslistrc#L44
// userEvent.setup() requires Safari 14 or up to work
const testContext: DropdownContextValue = {
dispatch: () => {},
popupId: 'menu-popup',
registerPopup: () => {},
registerTrigger: () => {},
state: { open: true, changeReason: null },
triggerElement: null,
};
describe('<MenuButton />', () => {
const { render } = createRenderer();
describeConformanceUnstyled(<MenuButton />, () => ({
inheritComponent: 'button',
render: (node) => {
return render(
<DropdownContext.Provider value={testContext}>{node}</DropdownContext.Provider>,
);
},
refInstanceof: window.HTMLButtonElement,
slots: {
root: {
expectedClassName: menuButtonClasses.root,
testWithElement: null,
},
},
skip: ['componentProp'],
}));
describe('prop: disabled', () => {
it('should render a disabled button', () => {
const { getByRole } = render(
<DropdownContext.Provider value={testContext}>
<MenuButton disabled />
</DropdownContext.Provider>,
);
const button = getByRole('button');
expect(button).to.have.property('disabled', true);
});
it('should not open the menu when clicked', () => {
const dispatchSpy = spy();
const context = {
...testContext,
state: { open: false, changeReason: null },
dispatch: dispatchSpy,
};
const { getByRole } = render(
<DropdownContext.Provider value={context}>
<MenuButton disabled />
</DropdownContext.Provider>,
);
const button = getByRole('button');
button.click();
expect(dispatchSpy.called).to.equal(false);
});
});
describe('prop: focusableWhenDisabled', () => {
it('has the aria-disabled instead of disabled attribute when disabled', () => {
const { getByRole } = render(
<DropdownContext.Provider value={testContext}>
<MenuButton disabled focusableWhenDisabled />
</DropdownContext.Provider>,
);
const button = getByRole('button');
expect(button).to.have.attribute('aria-disabled');
expect(button).not.to.have.attribute('disabled');
});
it('can receive focus when focusableWhenDisabled is set', () => {
const { getByRole } = render(
<DropdownContext.Provider value={testContext}>
<MenuButton disabled focusableWhenDisabled />
</DropdownContext.Provider>,
);
const button = getByRole('button');
act(() => {
button.focus();
});
expect(button).toHaveFocus();
});
});
it('toggles the menu state when clicked', () => {
const dispatchSpy = spy();
const context = {
...testContext,
state: { open: false, changeReason: null },
dispatch: dispatchSpy,
};
const { getByRole } = render(
<DropdownContext.Provider value={context}>
<MenuButton />
</DropdownContext.Provider>,
);
const button = getByRole('button');
button.click();
expect(dispatchSpy.calledOnce).to.equal(true);
expect(dispatchSpy.args[0][0]).to.contain({ type: DropdownActionTypes.toggle });
});
describe('keyboard navigation', () => {
[<MenuButton />, <MenuButton slots={{ root: 'span' }} />].forEach((buttonComponent) => {
const buttonType = buttonComponent.props.slots?.root ? 'non-native' : 'native';
['ArrowUp', 'ArrowDown'].forEach((key) =>
it(`opens the menu when pressing "${key}" on a ${buttonType} button`, async () => {
const dispatchSpy = spy();
const context = {
...testContext,
state: { open: false, changeReason: null },
dispatch: dispatchSpy,
};
const { getByRole } = render(
<DropdownContext.Provider value={context}>{buttonComponent}</DropdownContext.Provider>,
);
const button = getByRole('button');
act(() => {
button.focus();
});
await userEvent.keyboard(`{${key}}`);
expect(dispatchSpy.calledOnce).to.equal(true);
expect(dispatchSpy.args[0][0]).to.contain({ type: DropdownActionTypes.open });
}),
);
['Enter', ' '].forEach((key) =>
it(`opens the menu when pressing "${key}" on a ${buttonType} button`, async () => {
const dispatchSpy = spy();
const context = {
...testContext,
state: { open: false, changeReason: null },
dispatch: dispatchSpy,
};
const { getByRole } = render(
<DropdownContext.Provider value={context}>{buttonComponent}</DropdownContext.Provider>,
);
const button = getByRole('button');
act(() => {
button.focus();
});
await userEvent.keyboard(`{${key}}`);
expect(dispatchSpy.calledOnce).to.equal(true);
expect(dispatchSpy.args[0][0]).to.contain({ type: DropdownActionTypes.toggle });
}),
);
});
});
describe('accessibility attributes', () => {
it('has the aria-haspopup attribute', () => {
const { getByRole } = render(
<DropdownContext.Provider value={testContext}>
<MenuButton />
</DropdownContext.Provider>,
);
const button = getByRole('button');
expect(button).to.have.attribute('aria-haspopup');
});
it('has the aria-expanded=false attribute when closed', () => {
const context = {
...testContext,
state: { open: false, changeReason: null },
};
const { getByRole } = render(
<DropdownContext.Provider value={context}>
<MenuButton />
</DropdownContext.Provider>,
);
const button = getByRole('button');
expect(button).to.have.attribute('aria-expanded', 'false');
});
it('has the aria-expanded=true attribute when open', () => {
const context = {
...testContext,
state: { open: true, changeReason: null },
};
const { getByRole } = render(
<DropdownContext.Provider value={context}>
<MenuButton />
</DropdownContext.Provider>,
);
const button = getByRole('button');
expect(button).to.have.attribute('aria-expanded', 'true');
});
it('has the aria-controls attribute', () => {
const { getByRole } = render(
<DropdownContext.Provider value={testContext}>
<MenuButton />
</DropdownContext.Provider>,
);
const button = getByRole('button');
expect(button).to.have.attribute('aria-controls', 'menu-popup');
});
});
});