-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathRowSorting.test.ts
More file actions
207 lines (178 loc) · 5.78 KB
/
RowSorting.test.ts
File metadata and controls
207 lines (178 loc) · 5.78 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
import { describe, expect, it } from 'vitest'
import { createTable, getCoreRowModel, getSortedRowModel } from '../src'
import type { Person } from './makeTestData'
import { makeData } from './makeTestData'
const defaultData = makeData(3)
const defaultColumns = [
{
accessorKey: 'firstName' as keyof Person,
id: 'firstName',
},
{
accessorKey: 'lastName' as keyof Person,
id: 'lastName',
},
{
accessorKey: 'age' as keyof Person,
id: 'age',
},
{
accessorKey: 'visits' as keyof Person,
id: 'visits',
},
]
describe('RowSorting', () => {
it('should clear multi-sort when clicking column without shift key', () => {
let sorting = [
{ id: 'firstName', desc: false },
{ id: 'lastName', desc: true },
]
const table = createTable<Person>({
data: defaultData,
columns: defaultColumns,
getCoreRowModel: getCoreRowModel(),
getSortedRowModel: getSortedRowModel(),
onStateChange() {},
renderFallbackValue: '',
state: {
sorting,
},
onSortingChange: updater => {
sorting = typeof updater === 'function' ? updater(sorting) : updater
},
})
expect(sorting).toHaveLength(2)
expect(sorting[0]).toEqual({ id: 'firstName', desc: false })
expect(sorting[1]).toEqual({ id: 'lastName', desc: true })
const ageColumn = table.getColumn('age')
ageColumn?.toggleSorting(false, false)
expect(sorting).toHaveLength(1)
expect(sorting[0]).toEqual({ id: 'age', desc: false })
})
it('should maintain multi-sort when clicking column with shift key', () => {
let sorting = [
{ id: 'firstName', desc: false },
{ id: 'lastName', desc: true },
]
const table = createTable<Person>({
data: defaultData,
columns: defaultColumns,
getCoreRowModel: getCoreRowModel(),
getSortedRowModel: getSortedRowModel(),
onStateChange() {},
renderFallbackValue: '',
state: {
sorting,
},
onSortingChange: updater => {
sorting = typeof updater === 'function' ? updater(sorting) : updater
},
})
const ageColumn = table.getColumn('age')
ageColumn?.toggleSorting(false, true)
expect(sorting).toHaveLength(3)
expect(sorting[0]).toEqual({ id: 'firstName', desc: false })
expect(sorting[1]).toEqual({ id: 'lastName', desc: true })
expect(sorting[2]).toEqual({ id: 'age', desc: false })
})
it('should toggle sort direction when clicking same column without shift key in single sort mode', () => {
let sorting = [{ id: 'firstName', desc: false }]
const table = createTable<Person>({
data: defaultData,
columns: defaultColumns,
getCoreRowModel: getCoreRowModel(),
getSortedRowModel: getSortedRowModel(),
onStateChange() {},
renderFallbackValue: '',
state: {
sorting,
},
onSortingChange: updater => {
sorting = typeof updater === 'function' ? updater(sorting) : updater
},
})
const firstNameColumn = table.getColumn('firstName')
firstNameColumn?.toggleSorting(undefined, false)
expect(sorting).toHaveLength(1)
expect(sorting[0]).toEqual({ id: 'firstName', desc: true })
})
it('should replace multi-sort when clicking different column without shift key', () => {
let sorting = [
{ id: 'firstName', desc: false },
{ id: 'lastName', desc: true },
{ id: 'age', desc: false },
]
const table = createTable<Person>({
data: defaultData,
columns: defaultColumns,
getCoreRowModel: getCoreRowModel(),
getSortedRowModel: getSortedRowModel(),
onStateChange() {},
renderFallbackValue: '',
state: {
sorting,
},
onSortingChange: updater => {
sorting = typeof updater === 'function' ? updater(sorting) : updater
},
})
const visitsColumn = table.getColumn('visits')
visitsColumn?.toggleSorting(false, false)
expect(sorting).toHaveLength(1)
expect(sorting[0]).toEqual({ id: 'visits', desc: false })
})
it('should work with getToggleSortingHandler', () => {
let sorting = [
{ id: 'firstName', desc: false },
{ id: 'lastName', desc: true },
]
const table = createTable<Person>({
data: defaultData,
columns: defaultColumns,
getCoreRowModel: getCoreRowModel(),
getSortedRowModel: getSortedRowModel(),
onStateChange() {},
renderFallbackValue: '',
state: {
sorting,
},
onSortingChange: updater => {
sorting = typeof updater === 'function' ? updater(sorting) : updater
},
})
const ageColumn = table.getColumn('age')
const handler = ageColumn?.getToggleSortingHandler()
const mockEvent = { shiftKey: false }
handler?.(mockEvent)
expect(sorting).toHaveLength(1)
expect(sorting[0]).toEqual({ id: 'age', desc: true })
})
it('should work with getToggleSortingHandler with shift key', () => {
let sorting = [
{ id: 'firstName', desc: false },
{ id: 'lastName', desc: true },
]
const table = createTable<Person>({
data: defaultData,
columns: defaultColumns,
getCoreRowModel: getCoreRowModel(),
getSortedRowModel: getSortedRowModel(),
onStateChange() {},
renderFallbackValue: '',
state: {
sorting,
},
onSortingChange: updater => {
sorting = typeof updater === 'function' ? updater(sorting) : updater
},
})
const ageColumn = table.getColumn('age')
const handler = ageColumn?.getToggleSortingHandler()
const mockEvent = { shiftKey: true }
handler?.(mockEvent)
expect(sorting).toHaveLength(3)
expect(sorting[0]).toEqual({ id: 'firstName', desc: false })
expect(sorting[1]).toEqual({ id: 'lastName', desc: true })
expect(sorting[2]).toEqual({ id: 'age', desc: true })
})
})