-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathIrisGrid.test.tsx
More file actions
513 lines (437 loc) · 14.6 KB
/
IrisGrid.test.tsx
File metadata and controls
513 lines (437 loc) · 14.6 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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
import React, { useRef } from 'react';
import { act, fireEvent, render } from '@testing-library/react';
import dh from '@deephaven/jsapi-shim';
import { DateUtils, type Settings } from '@deephaven/jsapi-utils';
import { TestUtils } from '@deephaven/test-utils';
import { type TypeValue } from '@deephaven/filters';
import {
type ExpandableColumnGridModel,
isExpandableColumnGridModel,
} from '@deephaven/grid';
import IrisGrid from './IrisGrid';
import IrisGridTestUtils from './IrisGridTestUtils';
import type IrisGridProxyModel from './IrisGridProxyModel';
jest.mock('@deephaven/grid', () => ({
...jest.requireActual('@deephaven/grid'),
isExpandableColumnGridModel: jest.fn(),
}));
const { asMock } = TestUtils;
const VIEW_SIZE = 500;
const DEFAULT_SETTINGS: Settings = {
timeZone: 'America/New_York',
defaultDateTimeFormat: DateUtils.FULL_DATE_FORMAT,
showTimeZone: false,
showTSeparator: true,
formatter: [],
truncateNumbersWithPound: false,
};
const irisGridTestUtils = new IrisGridTestUtils(dh);
jest
.spyOn(Element.prototype, 'getBoundingClientRect')
.mockReturnValue(new DOMRect(0, 0, VIEW_SIZE, VIEW_SIZE));
jest.spyOn(Element.prototype, 'clientWidth', 'get').mockReturnValue(VIEW_SIZE);
jest.spyOn(Element.prototype, 'clientHeight', 'get').mockReturnValue(VIEW_SIZE);
function makeComponent(
model = irisGridTestUtils.makeModel(),
settings = DEFAULT_SETTINGS,
props = {}
) {
let ref: React.RefObject<IrisGrid>;
function IrisGridWithRef() {
ref = useRef<IrisGrid>(null);
// eslint-disable-next-line react/jsx-props-no-spreading
return <IrisGrid model={model} settings={settings} ref={ref} {...props} />;
}
render(<IrisGridWithRef />);
return ref!.current!;
}
function keyDown(
key: string,
component: IrisGrid,
extraArgs?: Partial<KeyboardEventInit>
) {
const args = { key, ...extraArgs };
fireEvent.keyDown(component.grid!.canvas!, args);
}
it('renders without crashing', () => {
makeComponent();
});
it('handles ctrl+shift+e to clear filters', () => {
const component = makeComponent();
component.clearAllFilters = jest.fn();
keyDown('e', component);
keyDown('e', component, { ctrlKey: true });
keyDown('e', component, { shiftKey: true });
expect(component.clearAllFilters).not.toHaveBeenCalled();
keyDown('e', component, { ctrlKey: true, shiftKey: true });
expect(component.clearAllFilters).toHaveBeenCalled();
});
it('handles reverse key shortcut', () => {
const component = makeComponent();
component.reverse = jest.fn();
keyDown('i', component);
expect(component.reverse).not.toHaveBeenCalled();
keyDown('i', component, { ctrlKey: true });
expect(component.reverse).toHaveBeenCalled();
});
it('handles copy key handler', () => {
const component = makeComponent();
component.copyRanges = jest.fn();
keyDown('c', component);
expect(component.copyRanges).not.toHaveBeenCalled();
keyDown('c', component, { ctrlKey: true });
expect(component.copyRanges).toHaveBeenCalled();
});
it('handles value: undefined in setFilterMap, clears column filter', () => {
const component = makeComponent();
component.setQuickFilter = jest.fn();
component.removeQuickFilter = jest.fn();
component.setFilterMap(
new Map([
[
'2',
{
columnType: IrisGridTestUtils.DEFAULT_TYPE,
filterList: [
{
operator: 'eq',
text: 'any',
value: undefined,
startColumnIndex: 0,
},
],
},
],
])
);
expect(component.setQuickFilter).not.toHaveBeenCalled();
expect(component.removeQuickFilter).toHaveBeenCalledWith(2);
});
it('handles value: null in setFilterMap', () => {
const component = makeComponent();
component.setQuickFilter = jest.fn();
component.setFilterMap(
new Map([
[
'2',
{
columnType: IrisGridTestUtils.DEFAULT_TYPE,
filterList: [
{ operator: 'eq', text: 'null', value: null, startColumnIndex: 0 },
],
},
],
])
);
expect(component.setQuickFilter).toHaveBeenCalledWith(
2,
expect.anything(),
'=null'
);
});
it('handles undefined operator, should default to eq', () => {
const component = makeComponent();
component.setQuickFilter = jest.fn();
component.setFilterMap(
new Map([
[
'2',
{
columnType: IrisGridTestUtils.DEFAULT_TYPE,
filterList: [
{
operator: undefined as unknown as TypeValue,
text: 'any',
value: 'any',
startColumnIndex: 0,
},
],
},
],
])
);
expect(component.setQuickFilter).toHaveBeenCalledWith(
2,
expect.anything(),
'any'
);
});
it('should set gotoValueSelectedColumnName to empty string if no columns are given', () => {
const component = makeComponent(
irisGridTestUtils.makeModel(
irisGridTestUtils.makeTable({
columns: [],
})
)
);
expect(component.state.gotoValueSelectedColumnName).toEqual('');
});
describe('handleResizeColumn', () => {
let irisGrid;
let metricCalculator;
beforeAll(() => {
irisGrid = makeComponent(
irisGridTestUtils.makeModel(
irisGridTestUtils.makeTable({
columns: irisGridTestUtils.makeColumns(1),
})
)
);
metricCalculator = irisGrid.state.metricCalculator;
});
it('should set column width to content width if undefined user width', async () => {
const modelIndex = 0;
const mockMetricCalculator = {
...metricCalculator,
userColumnWidths: new Map(),
setColumnWidth: jest.fn((column, size) => {
mockMetricCalculator.userColumnWidths.set(column, size);
}),
};
Object.assign(irisGrid.state.metricCalculator, mockMetricCalculator);
const contentWidth =
irisGrid.state.metrics.contentColumnWidths.get(modelIndex);
expect(contentWidth).toBeDefined();
act(() => irisGrid.handleResizeColumn(modelIndex));
expect(mockMetricCalculator.userColumnWidths.get(modelIndex)).toEqual(
contentWidth
);
});
it('should reset user width & set calculated width to content width if column has defined user width', () => {
const modelIndex = 0;
const mockMetricCalculator = {
...metricCalculator,
userColumnWidths: new Map([[modelIndex, 100]]),
setCalculatedColumnWidth: jest.fn((column, size) => {
mockMetricCalculator.calculatedColumnWidths.set(column, size);
}),
resetColumnWidth: jest.fn(() => {
mockMetricCalculator.userColumnWidths.delete(modelIndex);
}),
};
Object.assign(irisGrid.state.metricCalculator, mockMetricCalculator);
const contentWidth =
irisGrid.state.metrics.contentColumnWidths.get(modelIndex);
expect(contentWidth).toBeDefined();
act(() => irisGrid.handleResizeColumn(modelIndex));
expect(
mockMetricCalculator.userColumnWidths.get(modelIndex)
).toBeUndefined();
expect(mockMetricCalculator.calculatedColumnWidths.get(modelIndex)).toEqual(
contentWidth
);
});
});
describe('handleRollupChange', () => {
it('un-hides group-by columns', () => {
const columns = irisGridTestUtils.makeColumns(3);
const irisGrid = makeComponent(
irisGridTestUtils.makeModel(irisGridTestUtils.makeTable({ columns }))
);
const { metricCalculator } = irisGrid.state;
const resetColumnWidth = jest.spyOn(metricCalculator, 'resetColumnWidth');
const groupByNames = [columns[1].name, columns[2].name];
act(() => {
irisGrid.handleRollupChange({
columns: groupByNames,
showConstituents: true,
showNonAggregatedColumns: true,
});
});
expect(resetColumnWidth).toHaveBeenCalledWith(1);
expect(resetColumnWidth).toHaveBeenCalledWith(2);
expect(irisGrid.state.rollupConfig?.columns).toEqual(groupByNames);
});
it('does not call resetColumnWidth when there are no group-by columns', () => {
const irisGrid = makeComponent(
irisGridTestUtils.makeModel(
irisGridTestUtils.makeTable({
columns: irisGridTestUtils.makeColumns(3),
})
)
);
const { metricCalculator } = irisGrid.state;
const resetColumnWidth = jest.spyOn(metricCalculator, 'resetColumnWidth');
act(() => {
irisGrid.handleRollupChange({
columns: [],
showConstituents: true,
showNonAggregatedColumns: true,
});
});
expect(resetColumnWidth).not.toHaveBeenCalled();
});
});
// auto resize -> reset user width and set calculated width to content width
// manual resize -> set user width to content width
describe('handleResizeAllColumns', () => {
let irisGrid;
let metricCalculator;
beforeAll(() => {
irisGrid = makeComponent(
irisGridTestUtils.makeModel(
irisGridTestUtils.makeTable({
columns: irisGridTestUtils.makeColumns(3),
})
)
);
metricCalculator = irisGrid.state.metricCalculator;
});
it('should auto resize all columns if all were manually sized', () => {
const mockMetricCalculator = {
...metricCalculator,
userColumnWidths: new Map([
[0, 100],
[1, 100],
[2, 100],
]),
setCalculatedColumnWidth: jest.fn((column, size) => {
mockMetricCalculator.calculatedColumnWidths.set(column, size);
}),
resetColumnWidth: jest.fn(column => {
mockMetricCalculator.userColumnWidths.delete(column);
}),
};
Object.assign(irisGrid.state.metricCalculator, mockMetricCalculator);
const contentWidths = irisGrid.state.metrics.contentColumnWidths;
act(() => irisGrid.handleResizeAllColumns());
expect(mockMetricCalculator.userColumnWidths.size).toEqual(0);
contentWidths.forEach((contentWidth, modelIndex) => {
expect(
mockMetricCalculator.calculatedColumnWidths.get(modelIndex)
).toEqual(contentWidth);
});
});
it('should manual resize all columns if not all were manually sized', () => {
const mockMetricCalculator = {
...metricCalculator,
userColumnWidths: new Map([
[0, 100],
[1, 100],
]),
setColumnWidth: jest.fn((column, size) => {
mockMetricCalculator.userColumnWidths.set(column, size);
}),
resetColumnWidth: jest.fn(column => {
mockMetricCalculator.userColumnWidths.delete(column);
}),
};
Object.assign(irisGrid.state.metricCalculator, mockMetricCalculator);
const contentWidths = irisGrid.state.metrics.contentColumnWidths;
act(() => irisGrid.handleResizeAllColumns());
contentWidths.forEach((contentWidth, modelIndex) => {
expect(mockMetricCalculator.userColumnWidths.get(modelIndex)).toEqual(
contentWidth
);
});
});
describe('rebuildFilters', () => {
it('updates state if filters not empty', () => {
const component = makeComponent(undefined, undefined, {
quickFilters: [
[
'2',
{
columnType: IrisGridTestUtils.DEFAULT_TYPE,
filterList: [
{
operator: 'eq',
text: 'null',
value: null,
startColumnIndex: 0,
},
],
},
],
],
});
jest.spyOn(component, 'setState');
expect(component.setState).not.toBeCalled();
act(() => {
component.rebuildFilters();
});
expect(component.setState).toBeCalled();
});
it('does not update state for empty filters', () => {
const component = makeComponent();
jest.spyOn(component, 'setState');
act(() => {
component.rebuildFilters();
});
expect(component.setState).not.toBeCalled();
});
});
describe('column expand/collapse', () => {
let model: IrisGridProxyModel & ExpandableColumnGridModel;
let component: IrisGrid;
beforeEach(() => {
model = irisGridTestUtils.makeModel() as IrisGridProxyModel &
ExpandableColumnGridModel;
component = makeComponent(model);
model.setColumnExpanded = jest.fn();
model.isColumnExpanded = jest.fn(() => false);
model.expandAllColumns = jest.fn();
model.collapseAllColumns = jest.fn();
});
afterEach(() => {
jest.clearAllMocks();
});
it('calls setColumnExpanded if model supports expandable columns', () => {
asMock(isExpandableColumnGridModel).mockReturnValue(true);
model.hasExpandableColumns = true;
component.toggleExpandColumn(0);
expect(model.setColumnExpanded).toHaveBeenCalled();
});
it('ignores setColumnExpanded and expand/collapse all if model does not support expandable columns', () => {
asMock(isExpandableColumnGridModel).mockReturnValue(false);
component.toggleExpandColumn(0);
expect(model.setColumnExpanded).not.toHaveBeenCalled();
component.expandAllColumns();
expect(model.expandAllColumns).not.toHaveBeenCalled();
component.collapseAllColumns();
expect(model.collapseAllColumns).not.toHaveBeenCalled();
});
it('calls expandAllColumns if model supports expandable columns and expand all', () => {
asMock(isExpandableColumnGridModel).mockReturnValue(true);
model.isExpandAllColumnsAvailable = true;
component.expandAllColumns();
expect(model.expandAllColumns).toHaveBeenCalled();
component.collapseAllColumns();
expect(model.collapseAllColumns).toHaveBeenCalled();
});
it('ignores expandAllColumns if model does not support expand all', () => {
asMock(isExpandableColumnGridModel).mockReturnValue(true);
model.isExpandAllColumnsAvailable = false;
component.expandAllColumns();
expect(model.expandAllColumns).not.toHaveBeenCalled();
component.collapseAllColumns();
expect(model.collapseAllColumns).not.toHaveBeenCalled();
});
});
});
describe('Advanced Filter', () => {
it.each([
{ columnIndex: -1, expectedVisibility: false },
{ columnIndex: 0, expectedVisibility: true },
{ columnIndex: 1, expectedVisibility: true },
])(
'advanced filter button visibility is $expectedVisibility for column index $columnIndex',
({ columnIndex, expectedVisibility }) => {
const model = irisGridTestUtils.makeModel();
const ref = React.createRef<IrisGrid>();
const { container } = render(
<IrisGrid ref={ref} model={model} settings={DEFAULT_SETTINGS} />
);
act(() => {
ref.current?.setState({
focusedFilterBarColumn: columnIndex,
isFilterBarShown: true,
});
});
const advancedFilterButtons = container.querySelectorAll(
'.advanced-filter-button'
);
expect(advancedFilterButtons.length > 0).toBe(expectedVisibility);
}
);
});