-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathresizable.test.tsx
202 lines (182 loc) · 5.69 KB
/
resizable.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
import { commands, userEvent } from '@vitest/browser/context';
import type { Column } from '../../../src';
import { resizeHandleClassname } from '../../../src/HeaderCell';
import { getGrid, getHeaderCells, setup } from '../utils';
interface Row {
readonly col1: number;
readonly col2: string;
}
function queryResizeHandle(column: Element) {
return column.querySelector(`.${resizeHandleClassname}`);
}
function getResizeHandle(column: Element) {
const resizeHandle = column.querySelector(`.${resizeHandleClassname}`);
if (resizeHandle === null) {
throw new Error('Resize handle not found');
}
return resizeHandle;
}
interface ResizeArgs {
readonly column: Element;
readonly resizeBy: number;
}
async function resize({ column, resizeBy }: ResizeArgs) {
expect(getResizeHandle(column)).toBeInTheDocument();
// @ts-expect-error
await commands.resizeColumn(resizeBy);
}
async function autoResize(column: Element) {
const resizeHandle = getResizeHandle(column);
await userEvent.dblClick(resizeHandle);
}
const columns: readonly Column<Row>[] = [
{
key: 'col1',
name: 'col1',
width: 100
},
{
key: 'col2',
name: 'col2',
minWidth: 100,
width: 200,
maxWidth: 400,
resizable: true
}
];
test('cannot not resize or auto resize column when resizable is not specified', () => {
setup<Row, unknown>({ columns, rows: [] });
const [col1] = getHeaderCells();
expect(queryResizeHandle(col1)).not.toBeInTheDocument();
});
test('should resize column when dragging the handle', async () => {
const onColumnResize = vi.fn();
setup<Row, unknown>({ columns, rows: [], onColumnResize });
const grid = getGrid();
expect(onColumnResize).not.toHaveBeenCalled();
await expect.element(grid).toHaveStyle({ gridTemplateColumns: '100px 200px' });
const [, col2] = getHeaderCells();
await resize({ column: col2, resizeBy: -50 });
await expect.element(grid).toHaveStyle({ gridTemplateColumns: '100px 150px' });
expect(onColumnResize).toHaveBeenCalledExactlyOnceWith(expect.objectContaining(columns[1]), 150);
});
test('should use the maxWidth if specified', async () => {
const onColumnResize = vi.fn();
setup<Row, unknown>({ columns, rows: [], onColumnResize });
const grid = getGrid();
expect(onColumnResize).not.toHaveBeenCalled();
await expect.element(grid).toHaveStyle({ gridTemplateColumns: '100px 200px ' });
const [, col2] = getHeaderCells();
await resize({ column: col2, resizeBy: 1000 });
await expect.element(grid).toHaveStyle({ gridTemplateColumns: '100px 400px' });
expect(onColumnResize).toHaveBeenCalledWith(expect.objectContaining(columns[1]), 400);
});
test('should use the minWidth if specified', async () => {
setup<Row, unknown>({ columns, rows: [] });
const grid = getGrid();
await expect.element(grid).toHaveStyle({ gridTemplateColumns: '100px 200px' });
const [, col2] = getHeaderCells();
await resize({ column: col2, resizeBy: -150 });
await expect.element(grid).toHaveStyle({ gridTemplateColumns: '100px 100px' });
});
test('should auto resize column when resize handle is double clicked', async () => {
const onColumnResize = vi.fn();
setup<Row, unknown>({
columns,
rows: [
{
col1: 1,
col2: 'a'.repeat(50)
}
],
onColumnResize
});
const grid = getGrid();
await expect.element(grid).toHaveStyle({ gridTemplateColumns: '100px 200px' });
const [, col2] = getHeaderCells();
await autoResize(col2);
await expect.element(grid).toHaveStyle({ gridTemplateColumns: '100px 327.703px' });
expect(onColumnResize).toHaveBeenCalledExactlyOnceWith(
expect.objectContaining(columns[1]),
327.703125
);
});
test('should use the maxWidth if specified on auto resize', async () => {
setup<Row, unknown>({
columns,
rows: [
{
col1: 1,
col2: 'a'.repeat(500)
}
]
});
const grid = getGrid();
await expect.element(grid).toHaveStyle({ gridTemplateColumns: '100px 200px' });
const [, col2] = getHeaderCells();
await autoResize(col2);
await expect.element(grid).toHaveStyle({ gridTemplateColumns: '100px 400px' });
});
test('should use the minWidth if specified on auto resize', async () => {
setup<Row, unknown>({
columns,
rows: [
{
col1: 1,
col2: 'a'
}
]
});
const grid = getGrid();
await expect.element(grid).toHaveStyle({ gridTemplateColumns: '100px 200px' });
const [, col2] = getHeaderCells();
await autoResize(col2);
await expect.element(grid).toHaveStyle({ gridTemplateColumns: '100px 100px' });
});
test('should remeasure flex columns when resizing a column', async () => {
const onColumnResize = vi.fn();
setup<
{
readonly col1: string;
readonly col2: string;
readonly col3: string;
},
unknown
>({
columns: [
{
key: 'col1',
name: 'col1',
resizable: true
},
{
key: 'col2',
name: 'col2',
resizable: true
},
{
key: 'col3',
name: 'col3',
resizable: true
}
],
rows: [
{
col1: 'a'.repeat(10),
col2: 'a'.repeat(10),
col3: 'a'.repeat(10)
}
],
onColumnResize
});
const grid = getGrid();
await expect.element(grid).toHaveStyle({ gridTemplateColumns: '639.328px 639.328px 639.344px' });
const [col1] = getHeaderCells();
await autoResize(col1);
await expect.element(grid).toHaveStyle({ gridTemplateColumns: '79.1406px 919.422px 919.438px' });
expect(onColumnResize).toHaveBeenCalledOnce();
// onColumnResize is not called if width is not changed
await autoResize(col1);
await expect.element(grid).toHaveStyle({ gridTemplateColumns: '79.1406px 919.422px 919.438px' });
expect(onColumnResize).toHaveBeenCalledOnce();
});