-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
columnHeaders.DataGridPro.test.tsx
270 lines (242 loc) · 9.81 KB
/
columnHeaders.DataGridPro.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
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
261
262
263
264
265
266
267
268
269
270
import * as React from 'react';
// @ts-ignore Remove once the test utils are typed
import { createRenderer, fireEvent, screen } from '@mui/monorepo/test/utils';
import { expect } from 'chai';
import { gridClasses, DataGridPro, DataGridProProps } from '@mui/x-data-grid-pro';
import { getColumnHeaderCell, getColumnValues } from 'test/utils/helperFn';
const isJSDOM = /jsdom/.test(window.navigator.userAgent);
describe('<DataGridPro /> - Column Headers', () => {
const { render, clock } = createRenderer({ clock: 'fake' });
const baselineProps = {
autoHeight: isJSDOM,
disableColumnResize: false,
rows: [
{
id: 0,
brand: 'Nike',
foundationYear: 1964,
},
{
id: 1,
brand: 'Adidas',
foundationYear: 1949,
},
{
id: 2,
brand: 'Puma',
foundationYear: 1948,
},
],
};
it('should not scroll the column headers when a column is focused', function test() {
if (isJSDOM) {
this.skip(); // JSDOM version of .focus() doesn't scroll
}
render(
<div style={{ width: 102, height: 500 }}>
<DataGridPro
{...baselineProps}
columns={[{ field: 'brand' }, { field: 'foundationYear' }]}
/>
</div>,
);
const columnHeaders = document.querySelector('.MuiDataGrid-columnHeaders')!;
expect(columnHeaders.scrollLeft).to.equal(0);
const columnCell = getColumnHeaderCell(0);
columnCell.focus();
fireEvent.keyDown(columnCell, { key: 'End' });
expect(columnHeaders.scrollLeft).to.equal(0);
});
describe('GridColumnHeaderMenu', () => {
it('should close the menu when the window is scrolled', () => {
render(
<div style={{ width: 300, height: 200 }}>
<DataGridPro {...baselineProps} columns={[{ field: 'brand' }]} />
</div>,
);
const columnCell = getColumnHeaderCell(0);
const menuIconButton = columnCell.querySelector('button[aria-label="Menu"]');
fireEvent.click(menuIconButton);
clock.runToLast();
expect(screen.queryByRole('menu')).not.to.equal(null);
const virtualScroller = document.querySelector('.MuiDataGrid-virtualScroller')!;
fireEvent.wheel(virtualScroller);
clock.runToLast();
expect(screen.queryByRole('menu')).to.equal(null);
});
it('should not close the menu when updating the rows prop', () => {
const Test = (props: Partial<DataGridProProps>) => {
return (
<div style={{ width: 300, height: 500 }}>
<DataGridPro {...baselineProps} columns={[{ field: 'brand' }]} {...props} />
</div>
);
};
const { setProps } = render(<Test />);
const columnCell = getColumnHeaderCell(0);
const menuIconButton = columnCell.querySelector('button[aria-label="Menu"]');
fireEvent.click(menuIconButton);
clock.runToLast();
expect(screen.queryByRole('menu')).not.to.equal(null);
setProps({ rows: [...baselineProps.rows] });
clock.runToLast();
expect(screen.queryByRole('menu')).not.to.equal(null);
});
it('should not modify column order when menu is clicked', () => {
render(
<div style={{ width: 300, height: 500 }}>
<DataGridPro {...baselineProps} columns={[{ field: 'brand' }]} />
</div>,
);
expect(getColumnValues(0)).to.deep.equal(['Nike', 'Adidas', 'Puma']);
const columnCell = getColumnHeaderCell(0);
const menuIconButton = columnCell.querySelector('button[aria-label="Menu"]');
fireEvent.click(menuIconButton);
clock.runToLast();
expect(screen.queryByRole('menu')).not.to.equal(null);
fireEvent.click(screen.queryByRole('menu'));
expect(getColumnValues(0)).to.deep.equal(['Nike', 'Adidas', 'Puma']);
});
it('should sort column when sort by Asc is clicked', () => {
render(
<div style={{ width: 300, height: 500 }}>
<DataGridPro {...baselineProps} columns={[{ field: 'brand' }]} />
</div>,
);
const columnCell = getColumnHeaderCell(0);
const menuIconButton = columnCell.querySelector('button[aria-label="Menu"]');
expect(getColumnValues(0)).to.deep.equal(['Nike', 'Adidas', 'Puma']);
fireEvent.click(menuIconButton);
clock.runToLast();
expect(screen.queryByRole('menu')).not.to.equal(null);
fireEvent.click(screen.getByRole('menuitem', { name: 'Sort by ASC' }));
expect(getColumnValues(0)).to.deep.equal(['Adidas', 'Nike', 'Puma']);
});
it('should close the menu of a column when resizing this column', () => {
render(
<div style={{ width: 300, height: 500 }}>
<DataGridPro
{...baselineProps}
columns={[
{ field: 'brand', resizable: true },
{ field: 'foundationYear', resizable: true },
]}
/>
</div>,
);
const columnCell = getColumnHeaderCell(0);
const menuIconButton = columnCell.querySelector('button[aria-label="Menu"]');
fireEvent.click(menuIconButton);
clock.runToLast();
expect(screen.queryByRole('menu')).not.to.equal(null);
const separator = columnCell.querySelector('.MuiDataGrid-iconSeparator');
fireEvent.mouseDown(separator);
// TODO remove mouseUp once useGridColumnReorder will handle cleanup properly
fireEvent.mouseUp(separator);
clock.runToLast();
expect(screen.queryByRole('menu')).to.equal(null);
});
it('should close the menu of a column when resizing another column', () => {
render(
<div style={{ width: 300, height: 500 }}>
<DataGridPro
{...baselineProps}
columns={[
{ field: 'brand', resizable: true },
{ field: 'foundationYear', resizable: true },
]}
/>
</div>,
);
const columnWithMenuCell = getColumnHeaderCell(0);
const columnToResizeCell = getColumnHeaderCell(1);
const menuIconButton = columnWithMenuCell.querySelector('button[aria-label="Menu"]');
fireEvent.click(menuIconButton);
clock.runToLast();
expect(screen.queryByRole('menu')).not.to.equal(null);
const separator = columnToResizeCell.querySelector(
`.${gridClasses['columnSeparator--resizable']}`,
);
fireEvent.mouseDown(separator);
clock.runToLast();
expect(screen.queryByRole('menu')).to.equal(null);
});
it('should close the menu of a column when pressing the Escape key', () => {
render(
<div style={{ width: 300, height: 500 }}>
<DataGridPro {...baselineProps} columns={[{ field: 'brand' }]} />
</div>,
);
const columnCell = getColumnHeaderCell(0);
const menuIconButton = columnCell.querySelector('button[aria-label="Menu"]');
fireEvent.click(menuIconButton);
clock.runToLast();
expect(screen.queryByRole('menu')).not.to.equal(null);
/* eslint-disable material-ui/disallow-active-element-as-key-event-target */
fireEvent.keyDown(document.activeElement!, { key: 'Escape' });
clock.runToLast();
expect(screen.queryByRole('menu')).to.equal(null);
});
it('should remove the MuiDataGrid-menuOpen CSS class only after the transition has ended', () => {
render(
<div style={{ width: 300, height: 500 }}>
<DataGridPro {...baselineProps} columns={[{ field: 'brand' }]} />
</div>,
);
const columnCell = getColumnHeaderCell(0);
const menuIconButton = columnCell.querySelector('button[aria-label="Menu"]');
fireEvent.click(menuIconButton);
expect(menuIconButton?.parentElement).to.have.class(gridClasses.menuOpen);
clock.runToLast(); // Wait for the transition to run
fireEvent.keyDown(document.activeElement!, { key: 'Escape' });
expect(menuIconButton?.parentElement).to.have.class(gridClasses.menuOpen);
clock.runToLast(); // Wait for the transition to run
expect(menuIconButton?.parentElement).not.to.have.class(gridClasses.menuOpen);
});
it('should restore focus to the column header when dismissing the menu by selecting any item', () => {
const Test = (props: Partial<DataGridProProps>) => {
return (
<div style={{ width: 300, height: 500 }}>
<DataGridPro
{...baselineProps}
columns={[{ field: 'brand' }]}
initialState={{ sorting: { sortModel: [{ field: 'brand', sort: 'asc' }] } }}
{...props}
/>
</div>
);
};
render(<Test />);
const columnCell = getColumnHeaderCell(0);
const menuIconButton = columnCell.querySelector('button[aria-label="Menu"]');
fireEvent.click(menuIconButton);
clock.runToLast();
const menu = screen.queryByRole('menu');
const unsortMenuitem = screen.queryByRole('menuitem', { name: 'Sort by DESC' });
expect(menu).toHaveFocus();
fireEvent.keyDown(menu, { key: 'ArrowDown' });
expect(unsortMenuitem).toHaveFocus();
fireEvent.keyDown(unsortMenuitem, { key: 'Enter' });
expect(columnCell).toHaveFocus();
});
it('should restore focus to the column header when dismissing the menu without selecting any item', () => {
const Test = (props: Partial<DataGridProProps>) => {
return (
<div style={{ width: 300, height: 500 }}>
<DataGridPro {...baselineProps} columns={[{ field: 'brand' }]} {...props} />
</div>
);
};
render(<Test />);
const columnCell = getColumnHeaderCell(0);
const menuIconButton = columnCell.querySelector('button[aria-label="Menu"]');
fireEvent.click(menuIconButton);
clock.runToLast();
const menu = screen.queryByRole('menu');
expect(menu).toHaveFocus();
fireEvent.keyDown(menu, { key: 'Escape' });
expect(menu).not.toHaveFocus();
expect(columnCell).toHaveFocus();
});
});
});