forked from mui/material-ui
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMenuItem.test.tsx
173 lines (140 loc) · 5.06 KB
/
MenuItem.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
import * as React from 'react';
import { expect } from 'chai';
import { spy } from 'sinon';
import { act, createRenderer, fireEvent, screen } from '@mui/internal-test-utils';
import { MenuProvider, MenuProviderValue } from '@mui/base/useMenu';
import { ThemeProvider } from '@mui/joy/styles';
import MenuItem, { menuItemClasses as classes } from '@mui/joy/MenuItem';
import ListItemButton from '@mui/joy/ListItemButton';
import describeConformance from '../../test/describeConformance';
const testContext: MenuProviderValue = {
registerItem: () => ({ id: '0', deregister: () => {} }),
getItemIndex: () => 0,
totalSubitemCount: 1,
dispatch: () => {},
getItemState: () => ({
highlighted: false,
selected: false,
focusable: false,
}),
};
function Wrapper({ children }: { children: React.ReactNode }) {
return <MenuProvider value={testContext}>{children}</MenuProvider>;
}
describe('Joy <MenuItem />', () => {
const { render: baseRender } = createRenderer();
const render = (element: React.JSX.Element, options = {}) =>
baseRender(element, {
wrapper: Wrapper as React.JSXElementConstructor<{ children?: React.ReactNode }>,
...options,
});
describeConformance(<MenuItem />, () => ({
classes,
inheritComponent: ListItemButton,
render: (node) => render(<MenuProvider value={testContext}>{node}</MenuProvider>),
ThemeProvider,
refInstanceof: window.HTMLLIElement,
testComponentPropWith: 'a',
muiName: 'JoyMenuItem',
testVariantProps: { variant: 'solid' },
testCustomVariant: true,
skip: ['propsSpread', 'componentsProp', 'classesRoot'],
slots: {
root: {
expectedClassName: classes.root,
},
},
}));
it('should render with the variant class', () => {
const { getByRole } = render(<MenuItem variant="outlined" />);
expect(getByRole('menuitem')).to.have.class(classes.variantOutlined);
});
it('should render with primary color class', () => {
const { getByRole } = render(<MenuItem color="primary" />);
expect(getByRole('menuitem')).to.have.class(classes.colorPrimary);
});
it('should render a focusable menuitem', () => {
render(<MenuItem />);
const menuitem = screen.getByRole('menuitem');
expect(menuitem).to.have.property('tabIndex', -1);
});
it('should render with the selected class but not aria-selected when `selected`', () => {
render(<MenuItem selected />);
const menuitem = screen.getByRole('menuitem');
expect(menuitem).to.have.class(classes.selected);
expect(menuitem).not.to.have.attribute('aria-selected');
});
it('can have a role of option', () => {
render(<MenuItem role="option" aria-selected={false} />);
expect(screen.queryByRole('option')).not.to.equal(null);
});
describe('event callbacks', () => {
const events: Array<keyof typeof fireEvent> = [
'click',
'mouseDown',
'mouseEnter',
'mouseLeave',
'mouseUp',
'touchEnd',
];
events.forEach((eventName) => {
it(`should fire ${eventName}`, async () => {
const handlerName = `on${eventName[0].toUpperCase()}${eventName.slice(1)}`;
const handler = spy();
render(<MenuItem {...{ [handlerName]: handler }} />);
fireEvent[eventName](screen.getByRole('menuitem'));
expect(handler.callCount).to.equal(1);
});
});
it(`should fire focus, keydown, keyup and blur`, async () => {
const handleFocus = spy();
const handleKeyDown = spy();
const handleKeyUp = spy();
const handleBlur = spy();
render(
<MenuItem
tabIndex={0}
onFocus={handleFocus}
onKeyDown={handleKeyDown}
onKeyUp={handleKeyUp}
onBlur={handleBlur}
/>,
);
const menuitem = screen.getByRole('menuitem');
await act(async () => {
menuitem.focus();
});
expect(handleFocus.callCount).to.equal(1);
fireEvent.keyDown(menuitem);
expect(handleKeyDown.callCount).to.equal(1);
fireEvent.keyUp(menuitem);
expect(handleKeyUp.callCount).to.equal(1);
await act(async () => {
menuitem.blur();
});
expect(handleKeyDown.callCount).to.equal(1);
});
it('should fire onTouchStart', function touchStartTest() {
// only run in supported browsers
if (typeof Touch === 'undefined') {
this.skip();
}
const handleTouchStart = spy();
render(<MenuItem onTouchStart={handleTouchStart} />);
const menuitem = screen.getByRole('menuitem');
const touch = new Touch({ identifier: 0, target: menuitem, clientX: 0, clientY: 0 });
fireEvent.touchStart(menuitem, { touches: [touch] });
expect(handleTouchStart.callCount).to.equal(1);
});
});
it('can be disabled', () => {
render(<MenuItem disabled />);
const menuitem = screen.getByRole('menuitem');
expect(menuitem).to.have.attribute('aria-disabled', 'true');
});
it('can be selected', () => {
render(<MenuItem selected />);
const menuitem = screen.getByRole('menuitem');
expect(menuitem).to.have.class(classes.selected);
});
});