-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathColumnHeader.test.tsx
More file actions
344 lines (303 loc) · 13.6 KB
/
Copy pathColumnHeader.test.tsx
File metadata and controls
344 lines (303 loc) · 13.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
import { act, fireEvent } from '@testing-library/react'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import ColumnHeader from '../../src/components/ColumnHeader.js'
import { ColumnNamesContext } from '../../src/contexts/DataContext.js'
import { SortInfoAndActionsByColumnContext } from '../../src/contexts/OrderByContext.js'
import { getOffsetWidth } from '../../src/helpers/width.js'
import { ColumnParametersProvider } from '../../src/providers/ColumnParametersProvider.js'
import { ColumnWidthsProvider } from '../../src/providers/ColumnWidthsProvider.js'
import { render } from '../../src/utils/userEvent.js'
vi.mock('../../src/helpers/width.js', { spy: true })
vi.stubGlobal('localStorage', (() => {
const store = new Map<string, string>()
return {
getItem: (key: string) => store.get(key) ?? null,
removeItem: (key: string) => { store.delete(key) },
setItem: (key: string, value: string) => { store.set(key, value) },
clear: () => { store.clear() },
get length() { return store.size },
}
})())
// delete the local storage before each test
beforeEach(() => {
localStorage.clear()
})
const defaultProps = {
columnIndex: 0,
ariaColIndex: 1,
ariaRowIndex: 1,
columnConfig: {},
}
describe('ColumnHeader', () => {
const cacheKey = 'key'
beforeEach(() => {
vi.clearAllMocks()
})
it('renders column header correctly', () => {
const content = 'test'
const { getByRole } = render(<table><thead><tr><ColumnHeader columnName={content} {...defaultProps}>{content}</ColumnHeader></tr></thead></table>)
const element = getByRole('columnheader')
expect(element.textContent).toEqual(content)
expect(getOffsetWidth).not.toHaveBeenCalled()
})
it('renders headerComponent from columnConfiguration if present', () => {
const key = 'test'
const content = 'component'
const columnConfig = { headerComponent: <span>{content}</span> }
const props = { ...defaultProps, columnConfig }
const { getByRole } = render(<table><thead><tr><ColumnHeader columnName={key} {...props}>{content}</ColumnHeader></tr></thead></table>)
const element = getByRole('columnheader')
expect(element.textContent).toEqual(content)
expect(getOffsetWidth).not.toHaveBeenCalled()
})
it('measures the width if canMeasureWidth is true', () => {
render(
<ColumnNamesContext.Provider value={['test']}>
<ColumnWidthsProvider minWidth={10}>
<table><thead><tr><ColumnHeader columnName="test" {...defaultProps} canMeasureWidth={true} /></tr></thead></table>
</ColumnWidthsProvider>
</ColumnNamesContext.Provider>
)
expect(getOffsetWidth).toHaveBeenCalled()
})
// TODO(SL): the widths must be reset if the dataframe changes (or if the cache key changes, maybe), not if canMeasureWidth changes
// -> move the test elsewhere?
// it('measures the width again if canMeasureWidth toggles to true', () => {
// const { rerender } = render(<ColumnWidthsProvider numColumns={1} minWidth={10}>
// <table><thead><tr><ColumnHeader columnName="test" {...defaultProps} canMeasureWidth={true} /></tr></thead></table>
// </ColumnWidthsProvider>)
// expect(getOffsetWidth).toHaveBeenCalledTimes(1)
// // new data is being loaded
// rerender(<ColumnWidthsProvider numColumns={1} minWidth={10}><table><thead><tr><ColumnHeader columnName="test" {...defaultProps} canMeasureWidth={false} /></tr></thead></table>
// </ColumnWidthsProvider>)
// expect(getOffsetWidth).toHaveBeenCalledTimes(1)
// // new data is ready
// rerender(<ColumnWidthsProvider numColumns={1} minWidth={10}>
// <table><thead><tr><ColumnHeader columnName="test" {...defaultProps} canMeasureWidth={true} /></tr></thead></table>
// </ColumnWidthsProvider>)
// expect(getOffsetWidth).toHaveBeenCalledTimes(2)
// })
it('loads column width from localStorage when localStorageKey is provided', () => {
const savedWidth = 42
localStorage.setItem(cacheKey, JSON.stringify([savedWidth]))
const { getByRole } = render(
<ColumnNamesContext.Provider value={['test']}>
<ColumnWidthsProvider localStorageKey={cacheKey} minWidth={10}>
<table><thead><tr><ColumnHeader columnName="test" {...defaultProps} /></tr></thead></table>
</ColumnWidthsProvider>
</ColumnNamesContext.Provider>
)
const header = getByRole('columnheader')
expect(header.style.maxWidth).toEqual(`${savedWidth}px`)
})
it.for([
{ savedWidth: 9, minWidth: 10, expected: '' }, // saved widths smaller than minWidth are ignored
{ savedWidth: 10, minWidth: 10, expected: '10px' },
{ savedWidth: 11, minWidth: 10, expected: '11px' },
])('clamps loaded column width from localStorage when localStorageKey is provided', ({ savedWidth, minWidth, expected }) => {
localStorage.setItem(cacheKey, JSON.stringify([savedWidth]))
const { getByRole } = render(
<ColumnNamesContext.Provider value={['test']}>
<ColumnWidthsProvider localStorageKey={cacheKey} minWidth={minWidth}>
<table><thead><tr><ColumnHeader columnName="test" {...defaultProps} /></tr></thead></table>
</ColumnWidthsProvider>
</ColumnNamesContext.Provider>
)
const header = getByRole('columnheader')
expect(header.style.maxWidth).toEqual(expected)
})
it('handles double click on resize handle to auto resize', async () => {
// Set the initial width
const savedWidth = 42
const minWidth = 10
localStorage.setItem(cacheKey, JSON.stringify([savedWidth]))
const { user, getByRole } = render(
<ColumnNamesContext.Provider value={['test']}>
<ColumnWidthsProvider localStorageKey={cacheKey} minWidth={minWidth}>
<table><thead><tr><ColumnHeader columnName="test" canMeasureWidth={true} {...defaultProps} /></tr></thead></table>
</ColumnWidthsProvider>
</ColumnNamesContext.Provider>
)
const header = getByRole('columnheader')
const resizeHandle = getByRole('spinbutton')
expect(header.style.maxWidth).toEqual(`${savedWidth}px`)
expect(getOffsetWidth).toHaveBeenCalledTimes(0)
await user.dblClick(resizeHandle)
// the width is set to undefined, and should then be measured,
// but the measurement (.offsetWidth) can only run in a browser,
// so its value is 0
// The fixed width is then adjusted to the minimum width: 10px
expect(header.style.maxWidth).toEqual('10px')
expect(getOffsetWidth).toHaveBeenCalledTimes(1)
})
it('handles mouse click and drag on resize handle to resize', async () => {
// Set the initial width
const savedWidth = 42
localStorage.setItem(cacheKey, JSON.stringify([savedWidth]))
const { user, getByRole } = render(
<ColumnNamesContext.Provider value={['test']}>
<ColumnWidthsProvider localStorageKey={cacheKey} minWidth={10}>
<table><thead><tr><ColumnHeader columnName="test" {...defaultProps} /></tr></thead></table>
</ColumnWidthsProvider>
</ColumnNamesContext.Provider>
)
// Simulate resizing the column
const header = getByRole('columnheader')
const resizeHandle = getByRole('spinbutton')
const x = 150
const delta = 10
await user.pointer([
// press the left button on the resize handle, at x=150
{ keys: '[MouseLeft>]', target: resizeHandle, coords: { x, y: 0 } },
// move the pointer to x=160
{ coords: { x: x + delta, y: 0 } },
// release the left button
{ keys: '[/MouseLeft]' },
])
expect(header.style.maxWidth).toEqual(`${savedWidth + delta}px`)
})
it('respects column-specific minWidth when resizing', async () => {
const savedWidth = 50
const columnMinWidth = 30
localStorage.setItem(cacheKey, JSON.stringify([savedWidth]))
const columnConfiguration = { test: { minWidth: columnMinWidth } }
const { user, getByRole } = render(
<ColumnNamesContext.Provider value={['test']}>
<ColumnParametersProvider columnConfiguration={columnConfiguration}>
<ColumnWidthsProvider localStorageKey={cacheKey} minWidth={10}>
<table><thead><tr><ColumnHeader columnName="test" canMeasureWidth={true} {...defaultProps} /></tr></thead></table>
</ColumnWidthsProvider>
</ColumnParametersProvider>
</ColumnNamesContext.Provider>
)
const header = getByRole('columnheader')
const resizeHandle = getByRole('spinbutton')
expect(header.style.maxWidth).toEqual(`${savedWidth}px`)
// Try to resize to below column minWidth (but above global minWidth)
const x = 150
const delta = -30 // This would make width 20, which is below column minWidth of 30
await user.pointer([
{ keys: '[MouseLeft>]', target: resizeHandle, coords: { x, y: 0 } },
{ coords: { x: x + delta, y: 0 } },
{ keys: '[/MouseLeft]' },
])
// Should be clamped to column minWidth (30), not global minWidth (10)
expect(header.style.maxWidth).toEqual(`${columnMinWidth}px`)
})
it('uses column minWidth even if it is less than global minWidth', async () => {
const savedWidth = 50
const globalMinWidth = 30
const columnMinWidth = 20 // Less than global minWidth
localStorage.setItem(cacheKey, JSON.stringify([savedWidth]))
const columnConfiguration = { test: { minWidth: columnMinWidth } }
const { user, getByRole } = render(
<ColumnNamesContext.Provider value={['test']}>
<ColumnParametersProvider columnConfiguration={columnConfiguration}>
<ColumnWidthsProvider localStorageKey={cacheKey} minWidth={globalMinWidth}>
<table><thead><tr><ColumnHeader columnName="test" canMeasureWidth={true} {...defaultProps} /></tr></thead></table>
</ColumnWidthsProvider>
</ColumnParametersProvider>
</ColumnNamesContext.Provider>
)
const header = getByRole('columnheader')
const resizeHandle = getByRole('spinbutton')
// Try to resize to below both minWidths
const x = 150
const delta = -35 // This would make width 15, which is below both minWidths
await user.pointer([
{ keys: '[MouseLeft>]', target: resizeHandle, coords: { x, y: 0 } },
{ coords: { x: x + delta, y: 0 } },
{ keys: '[/MouseLeft]' },
])
// Should be clamped to global minWidth (30) since it's larger than column minWidth (20)
expect(header.style.maxWidth).toEqual(`${columnMinWidth}px`)
})
it('reloads column width when localStorageKey changes', () => {
const cacheKey2 = 'key-2'
const width1 = 150
localStorage.setItem(cacheKey, JSON.stringify([width1]))
const width2 = 300
localStorage.setItem(cacheKey2, JSON.stringify([width2]))
const columnNames = ['test']
const { rerender, getByRole } = render(
<ColumnNamesContext.Provider value={columnNames}>
<ColumnWidthsProvider localStorageKey={cacheKey} minWidth={10}>
<table><thead><tr><ColumnHeader columnName="test" {...defaultProps} /></tr></thead></table>
</ColumnWidthsProvider>
</ColumnNamesContext.Provider>
)
const header = getByRole('columnheader')
expect(header.style.maxWidth).toEqual(`${width1}px`)
rerender(
<ColumnNamesContext.Provider value={columnNames}>
<ColumnWidthsProvider localStorageKey={cacheKey2} minWidth={10}>
<table><thead><tr><ColumnHeader columnName="test" {...defaultProps} /></tr></thead></table>
</ColumnWidthsProvider>
</ColumnNamesContext.Provider>
)
expect(header.style.maxWidth).toEqual(`${width2}px`)
})
it('call toggleOrderBy (eg. to change orderBy) when clicking on the header, but not when clicking on the resize handle', async () => {
const toggleOrderBy = vi.fn()
const sortInfoAndActionsByColumn = new Map([['test', { toggleOrderBy }]])
const { user, getByRole } = render(
<table>
<thead>
<tr>
<SortInfoAndActionsByColumnContext.Provider value={sortInfoAndActionsByColumn}>
<ColumnHeader columnName="test" {...defaultProps} />
</SortInfoAndActionsByColumnContext.Provider>
</tr>
</thead>
</table>
)
const header = getByRole('columnheader')
const resizeHandle = getByRole('spinbutton')
await user.click(resizeHandle)
expect(toggleOrderBy).not.toHaveBeenCalled()
await user.click(header)
expect(toggleOrderBy).toHaveBeenCalled()
})
it.for(['{ }', '{Enter}'])('call toggleOrderBy (eg. to change orderBy) when pressing "%s" while the header is focused', async (key) => {
const toggleOrderBy = vi.fn()
const sortInfoAndActionsByColumn = new Map([['test', { toggleOrderBy }]])
const { user, getByRole } = render(
<table>
<thead>
<tr>
<SortInfoAndActionsByColumnContext.Provider value={sortInfoAndActionsByColumn}>
<ColumnHeader columnName="test" {...defaultProps} />
</SortInfoAndActionsByColumnContext.Provider>
</tr>
</thead>
</table>
)
const header = getByRole('columnheader')
header.focus()
await user.keyboard(key)
expect(toggleOrderBy).toHaveBeenCalled()
})
it('copies the column name to clipboard on copy event', async () => {
const { getByRole } = render(
<table>
<tbody>
<tr>
<ColumnHeader
columnName="test"
{...defaultProps}
/>
</tr>
</tbody>
</table>
)
const cell = getByRole('columnheader')
cell.focus()
act(() => {
// using fireEvent instead of userEvent - I cannot find how to do it with userEvent
fireEvent.copy(cell)
})
const text = await navigator.clipboard.readText()
expect(text).toBe('test')
// Note that the text is not copied if a selection exists. But I don't know how to test that yet.
})
})