forked from mui/mui-x
-
Notifications
You must be signed in to change notification settings - Fork 1
/
columnHeaders.DataGrid.test.tsx
140 lines (121 loc) · 4.33 KB
/
columnHeaders.DataGrid.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
import * as React from 'react';
// @ts-ignore Remove once the test utils are typed
import { createRenderer, fireEvent, screen, within } from '@mui/monorepo/test/utils';
import { expect } from 'chai';
import { DataGrid } from '@mui/x-data-grid';
import { getColumnHeaderCell, getColumnHeadersTextContent } from 'test/utils/helperFn';
const isJSDOM = /jsdom/.test(window.navigator.userAgent);
describe('<DataGrid /> - Column Headers', () => {
const { render, clock } = createRenderer({ clock: 'fake' });
const baselineProps = {
autoHeight: isJSDOM,
rows: [
{
id: 0,
brand: 'Nike',
},
{
id: 1,
brand: 'Adidas',
},
{
id: 2,
brand: 'Puma',
},
],
};
describe('headerClassName', () => {
it('should append the CSS class defined in headerClassName', () => {
render(
<div style={{ width: 300, height: 500 }}>
<DataGrid {...baselineProps} columns={[{ field: 'brand', headerClassName: 'foobar' }]} />
</div>,
);
expect(getColumnHeaderCell(0)).to.have.class('foobar');
});
it('should append the CSS class returned by headerClassName', () => {
render(
<div style={{ width: 300, height: 500 }}>
<DataGrid
{...baselineProps}
columns={[{ field: 'brand', headerClassName: () => 'foobar' }]}
/>
</div>,
);
expect(getColumnHeaderCell(0)).to.have.class('foobar');
});
});
describe('GridColumnHeaderMenu', () => {
it('should allow to hide column', () => {
render(
<div style={{ width: 300, height: 300 }}>
<DataGrid
{...baselineProps}
columns={[{ field: 'id' }, { field: 'brand', headerClassName: 'foobar' }]}
/>
</div>,
);
expect(getColumnHeadersTextContent()).to.deep.equal(['id', 'brand']);
fireEvent.click(within(getColumnHeaderCell(0)).getByLabelText('Menu'));
fireEvent.click(screen.getByRole('menuitem', { name: 'Hide' }));
clock.runToLast();
expect(getColumnHeadersTextContent()).to.deep.equal(['brand']);
});
it('should not allow to hide the only visible column', () => {
render(
<div style={{ width: 300, height: 300 }}>
<DataGrid
{...baselineProps}
columns={[{ field: 'id' }, { field: 'brand', headerClassName: 'foobar' }]}
initialState={{
columns: {
columnVisibilityModel: { brand: false },
},
}}
/>
</div>,
);
expect(getColumnHeadersTextContent()).to.deep.equal(['id']);
fireEvent.click(within(getColumnHeaderCell(0)).getByLabelText('Menu'));
fireEvent.click(screen.getByRole('menuitem', { name: 'Hide' }));
clock.runToLast();
expect(getColumnHeadersTextContent()).to.deep.equal(['id']);
});
it('should not allow to hide the only visible column that has menu', () => {
render(
<div style={{ width: 300, height: 300 }}>
<DataGrid
{...baselineProps}
columns={[
{ field: 'id', disableColumnMenu: true },
{ field: 'brand', headerClassName: 'foobar' },
]}
/>
</div>,
);
expect(getColumnHeadersTextContent()).to.deep.equal(['id', 'brand']);
fireEvent.click(within(getColumnHeaderCell(1)).getByLabelText('Menu'));
fireEvent.click(screen.getByRole('menuitem', { name: 'Hide' }));
clock.runToLast();
expect(getColumnHeadersTextContent()).to.deep.equal(['id', 'brand']);
});
});
it('should display sort column menu items as per sortingOrder prop', () => {
render(
<div style={{ width: 300, height: 500 }}>
<DataGrid
{...baselineProps}
sortingOrder={['desc', 'asc']}
columns={[{ field: 'brand', headerClassName: 'foobar' }]}
/>
</div>,
);
const columnCell = getColumnHeaderCell(0);
const menuIconButton = columnCell.querySelector('button[aria-label="Menu"]');
fireEvent.click(menuIconButton);
clock.runToLast();
expect(screen.queryByRole('menuitem', { name: /asc/i })).not.to.equal(null);
expect(screen.queryByRole('menuitem', { name: /desc/i })).not.to.equal(null);
expect(screen.queryByRole('menuitem', { name: /unsort/i })).to.equal(null);
});
});