forked from mui/mui-x
-
Notifications
You must be signed in to change notification settings - Fork 1
/
columnsVisibility.DataGrid.test.tsx
334 lines (290 loc) · 10.8 KB
/
columnsVisibility.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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import * as React from 'react';
import { expect } from 'chai';
import { spy } from 'sinon';
import { createRenderer, fireEvent, screen } from '@mui/monorepo/test/utils';
import { DataGrid, DataGridProps, GridRowsProp, GridColDef, GridToolbar } from '@mui/x-data-grid';
import { getColumnHeadersTextContent } from '../../../../../test/utils/helperFn';
const isJSDOM = /jsdom/.test(window.navigator.userAgent);
const rows: GridRowsProp = [{ id: 1, idBis: 1 }];
const columns: GridColDef[] = [{ field: 'id' }, { field: 'idBis' }];
describe('<DataGridPro /> - Columns Visibility', () => {
const { render } = createRenderer();
function TestDataGrid(
props: Omit<DataGridProps, 'columns' | 'rows'> &
Partial<Pick<DataGridProps, 'rows' | 'columns'>>,
) {
return (
<div style={{ width: 300, height: 300 }}>
<DataGrid columns={columns} rows={rows} {...props} autoHeight={isJSDOM} />
</div>
);
}
describe('prop: columnVisibilityModel and onColumnVisibilityModelChange', () => {
it('should allow to set the columnVisibilityModel prop', () => {
render(<TestDataGrid columnVisibilityModel={{ idBis: false }} />);
expect(getColumnHeadersTextContent()).to.deep.equal(['id']);
});
it('should allow to update the columnVisibilityModel prop from the outside', () => {
const { setProps } = render(<TestDataGrid columnVisibilityModel={{ idBis: false }} />);
expect(getColumnHeadersTextContent()).to.deep.equal(['id']);
setProps({
columnVisibilityModel: {},
});
expect(getColumnHeadersTextContent()).to.deep.equal(['id', 'idBis']);
});
it('should update the visible columns when props.onColumnVisibilityModelChange and props.columnVisibilityModel are not defined', () => {
render(
<TestDataGrid
components={{
Toolbar: GridToolbar,
}}
/>,
);
expect(getColumnHeadersTextContent()).to.deep.equal(['id', 'idBis']);
fireEvent.click(screen.getByRole('button', { name: 'Select columns' }));
fireEvent.click(screen.getByRole('checkbox', { name: 'id' }));
expect(getColumnHeadersTextContent()).to.deep.equal(['idBis']);
});
it('should call onColumnVisibilityModelChange and update the visible columns when props.columnVisibilityModel is not defined', () => {
const onColumnVisibilityModelChange = spy();
render(
<TestDataGrid
components={{
Toolbar: GridToolbar,
}}
onColumnVisibilityModelChange={onColumnVisibilityModelChange}
/>,
);
expect(getColumnHeadersTextContent()).to.deep.equal(['id', 'idBis']);
fireEvent.click(screen.getByRole('button', { name: 'Select columns' }));
fireEvent.click(screen.getByRole('checkbox', { name: 'id' }));
expect(getColumnHeadersTextContent()).to.deep.equal(['idBis']);
expect(onColumnVisibilityModelChange.callCount).to.equal(1);
expect(onColumnVisibilityModelChange.lastCall.firstArg).to.deep.equal({
id: false,
});
});
it('should call onColumnVisibilityModelChange with the new model when columnVisibilityModel is controlled', () => {
const onColumnVisibilityModelChange = spy();
render(
<TestDataGrid
components={{
Toolbar: GridToolbar,
}}
columnVisibilityModel={{ idBis: false }}
onColumnVisibilityModelChange={onColumnVisibilityModelChange}
/>,
);
expect(getColumnHeadersTextContent()).to.deep.equal(['id']);
fireEvent.click(screen.getByRole('button', { name: 'Select columns' }));
fireEvent.click(screen.getByRole('checkbox', { name: 'id' }));
expect(getColumnHeadersTextContent()).to.deep.equal(['id']);
expect(onColumnVisibilityModelChange.callCount).to.equal(1);
expect(onColumnVisibilityModelChange.lastCall.firstArg).to.deep.equal({
id: false,
idBis: false,
});
});
it('should call onColumnVisibilityModelChange with the new model when toggling all columns', () => {
const onColumnVisibilityModelChange = spy();
render(
<TestDataGrid
components={{
Toolbar: GridToolbar,
}}
columnVisibilityModel={{ idBis: false }}
onColumnVisibilityModelChange={onColumnVisibilityModelChange}
/>,
);
expect(getColumnHeadersTextContent()).to.deep.equal(['id']);
fireEvent.click(screen.getByRole('button', { name: 'Select columns' }));
fireEvent.click(screen.getByText('Hide all'));
expect(onColumnVisibilityModelChange.callCount).to.equal(1);
expect(onColumnVisibilityModelChange.lastCall.firstArg).to.deep.equal({
idBis: false,
id: false,
});
fireEvent.click(screen.getByText('Show all'));
expect(onColumnVisibilityModelChange.callCount).to.equal(2);
expect(onColumnVisibilityModelChange.lastCall.firstArg).to.deep.equal({});
});
it('should not hide non hideable columns when toggling all columns', () => {
render(
<TestDataGrid
components={{
Toolbar: GridToolbar,
}}
columns={[{ field: 'id' }, { field: 'idBis', hideable: false }]}
/>,
);
fireEvent.click(screen.getByRole('button', { name: 'Select columns' }));
fireEvent.click(screen.getByText('Hide all'));
expect(getColumnHeadersTextContent()).to.deep.equal(['idBis']);
});
// Fixes (1) in in https://github.com/mui/mui-x/issues/7393#issuecomment-1372129661
it('should not show hidden non hideable columns when "Show all" is clicked', () => {
render(
<TestDataGrid
components={{ Toolbar: GridToolbar }}
columns={[{ field: 'id' }, { field: 'idBis', hideable: false }]}
initialState={{
columns: {
columnVisibilityModel: { idBis: false },
},
}}
/>,
);
fireEvent.click(screen.getByRole('button', { name: 'Select columns' }));
fireEvent.click(screen.getByText('Show all'));
expect(getColumnHeadersTextContent()).to.deep.equal(['id']);
});
// Fixes (2) in in https://github.com/mui/mui-x/issues/7393#issuecomment-1372129661
it('should not show non-hideable columns when "Hide all" is clicked', () => {
render(
<TestDataGrid
components={{ Toolbar: GridToolbar }}
columns={[{ field: 'id' }, { field: 'idBis', hideable: false }]}
initialState={{
columns: {
columnVisibilityModel: { idBis: false },
},
}}
/>,
);
fireEvent.click(screen.getByRole('button', { name: 'Select columns' }));
fireEvent.click(screen.getByText('Hide all'));
expect(getColumnHeadersTextContent()).to.deep.equal([]);
});
});
describe('prop: initialState.columns.columnVisibilityModel', () => {
it('should allow to initialize the columnVisibilityModel', () => {
render(
<TestDataGrid
initialState={{
columns: {
columnVisibilityModel: { idBis: false },
},
}}
/>,
);
expect(getColumnHeadersTextContent()).to.deep.equal(['id']);
});
it('should use the control state upon the initialize state when both are defined', () => {
render(
<TestDataGrid
columnVisibilityModel={{}}
initialState={{
columns: {
columnVisibilityModel: { idBis: false },
},
}}
/>,
);
expect(getColumnHeadersTextContent()).to.deep.equal(['id', 'idBis']);
});
it('should not update the visible columns when updating the initial state', () => {
const { setProps } = render(
<TestDataGrid
initialState={{
columns: {
columnVisibilityModel: { idBis: false },
},
}}
/>,
);
setProps({
initialState: {
columns: {
columnVisibilityModel: {},
},
},
});
expect(getColumnHeadersTextContent()).to.deep.equal(['id']);
});
it('should allow to update the visible columns through the UI when initialized with initialState', () => {
render(
<TestDataGrid
initialState={{
columns: {
columnVisibilityModel: { idBis: false },
},
}}
components={{
Toolbar: GridToolbar,
}}
/>,
);
expect(getColumnHeadersTextContent()).to.deep.equal(['id']);
fireEvent.click(screen.getByRole('button', { name: 'Select columns' }));
fireEvent.click(screen.getByRole('checkbox', { name: 'id' }));
expect(getColumnHeadersTextContent()).to.deep.equal([]);
});
});
it('should autofocus the first switch element in columns panel when `autoFocusSearchField` disabled', () => {
render(
<TestDataGrid
components={{
Toolbar: GridToolbar,
}}
componentsProps={{
columnsPanel: {
autoFocusSearchField: false,
},
}}
/>,
);
fireEvent.click(screen.getByRole('button', { name: 'Select columns' }));
expect(screen.getByRole('checkbox', { name: columns[0].field })).toHaveFocus();
});
it('should hide `Hide all` in columns panel when `disableHideAllButton` is `true`', () => {
render(
<TestDataGrid
components={{
Toolbar: GridToolbar,
}}
componentsProps={{
columnsPanel: {
disableHideAllButton: true,
},
}}
/>,
);
fireEvent.click(screen.getByRole('button', { name: 'Select columns' }));
expect(screen.queryByRole('button', { name: 'Hide all' })).to.equal(null);
});
it('should hide `Show all` in columns panel when `disableShowAllButton` is `true`', () => {
render(
<TestDataGrid
components={{
Toolbar: GridToolbar,
}}
componentsProps={{
columnsPanel: {
disableShowAllButton: true,
},
}}
/>,
);
fireEvent.click(screen.getByRole('button', { name: 'Select columns' }));
expect(screen.queryByRole('button', { name: 'Show all' })).to.equal(null);
});
it('should control columns shown in columns panel using `getTogglableColumns` prop', () => {
const getTogglableColumns = (cols: GridColDef[]) =>
cols.filter((column) => column.field !== 'idBis').map((column) => column.field);
render(
<TestDataGrid
slots={{
toolbar: GridToolbar,
}}
slotProps={{
columnsPanel: {
getTogglableColumns,
},
}}
/>,
);
fireEvent.click(screen.getByRole('button', { name: 'Select columns' }));
expect(screen.queryByRole('checkbox', { name: 'id' })).not.to.equal(null);
expect(screen.queryByRole('checkbox', { name: 'idBis' })).to.equal(null);
});
});