-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathangularReactivityFeature.test.ts
More file actions
250 lines (230 loc) · 8.34 KB
/
angularReactivityFeature.test.ts
File metadata and controls
250 lines (230 loc) · 8.34 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
import { describe, expect, test, vi } from 'vitest'
import { computed, effect, isSignal, signal } from '@angular/core'
import { TestBed } from '@angular/core/testing'
import { injectTable, stockFeatures } from '../src'
import { getFnReactiveCache, testShouldBeComputedProperty } from './test-utils'
import type { WritableSignal } from '@angular/core'
import type { ColumnDef } from '../src'
describe('angularReactivityFeature', () => {
type Data = { id: string; title: string }
const data = signal<Array<Data>>([{ id: '1', title: 'Title' }])
const columns: Array<ColumnDef<typeof stockFeatures, Data>> = [
{
id: 'id',
header: 'Id',
accessorKey: 'id',
cell: (context) => context.getValue(),
},
{
id: 'title',
header: 'Title',
accessorKey: 'title',
cell: (context) => context.getValue(),
},
]
function createTestTable(_data: WritableSignal<Array<Data>> = data) {
return TestBed.runInInjectionContext(() =>
injectTable(() => ({
data: _data(),
_features: { ...stockFeatures },
columns: columns,
getRowId: (row) => row.id,
reactivity: {
column: true,
cell: true,
row: true,
header: true,
},
})),
)
}
const table = createTestTable()
const tablePropertyKeys = Object.keys(table)
describe.skip('Table property reactivity', () => {
test.each(
tablePropertyKeys.map((property) => [
property,
testShouldBeComputedProperty(table, property),
]),
)('property (%s) is computed -> (%s)', (name, expected) => {
const tableProperty = table[name as keyof typeof table]
expect(isSignal(tableProperty)).toEqual(expected)
})
describe('will create a computed for non detectable computed properties', () => {
test('getIsSomeRowsPinned', () => {
table.getIsSomeRowsPinned('top')
table.getIsSomeRowsPinned('bottom')
table.getIsSomeRowsPinned()
expect(getFnReactiveCache(table.getIsSomeRowsPinned)).toHaveProperty(
'["top"]',
)
expect(getFnReactiveCache(table.getIsSomeRowsPinned)).toHaveProperty(
'["bottom"]',
)
expect(getFnReactiveCache(table.getIsSomeRowsPinned)).toHaveProperty(
'[]',
)
})
})
})
describe.skip('Header property reactivity', () => {
const headers = table.getHeaderGroups()
headers.forEach((headerGroup, index) => {
const headerPropertyKeys = Object.keys(headerGroup)
test.each(
headerPropertyKeys.map((property) => [
property,
testShouldBeComputedProperty(headerGroup, property),
]),
)(
`HeaderGroup ${headerGroup.id} (${index}) - property (%s) is computed -> (%s)`,
(name, expected) => {
const tableProperty = headerGroup[name as keyof typeof headerGroup]
expect(isSignal(tableProperty)).toEqual(expected)
},
)
const headers = headerGroup.headers
headers.forEach((header, cellIndex) => {
const headerPropertyKeys = Object.keys(header).concat(
Object.getOwnPropertyNames(Object.getPrototypeOf(header)),
)
test.each(
headerPropertyKeys.map((property) => [
property,
testShouldBeComputedProperty(header, property),
]),
)(
`HeaderGroup ${headerGroup.id} (${index}) / Header ${header.id} - property (%s) is computed -> (%s)`,
(name, expected) => {
const tableProperty = header[name as keyof typeof header]
expect(isSignal(tableProperty)).toEqual(expected)
},
)
})
})
})
describe.skip('Column property reactivity', () => {
const columns = table.getAllColumns()
columns.forEach((column, index) => {
const columnPropertyKeys = Object.keys(column).concat(
Object.getOwnPropertyNames(Object.getPrototypeOf(column)),
)
test.each(
columnPropertyKeys.map((property) => [
property,
testShouldBeComputedProperty(column, property),
]),
)(
`Column ${column.id} (${index}) - property (%s) is computed -> (%s)`,
(name, expected) => {
const tableProperty = column[name as keyof typeof column]
expect(isSignal(tableProperty)).toEqual(expected)
},
)
})
})
describe.skip('Row and cells property reactivity', () => {
const flatRows = table.getRowModel().flatRows
flatRows.forEach((row, index) => {
const rowsPropertyKeys = Object.keys(row).concat(
Object.getOwnPropertyNames(Object.getPrototypeOf(row)),
)
test.each(
rowsPropertyKeys.map((property) => [
property,
testShouldBeComputedProperty(row, property),
]),
)(
`Row ${row.id} (${index}) - property (%s) is computed -> (%s)`,
(name, expected) => {
const tableProperty = row[name as keyof typeof row]
expect(isSignal(tableProperty)).toEqual(expected)
},
)
const cells = row.getAllCells()
cells.forEach((cell, cellIndex) => {
const cellPropertyKeys = Object.keys(cell).concat(
Object.getOwnPropertyNames(Object.getPrototypeOf(cell)),
)
test.each(
cellPropertyKeys.map((property) => [
property,
testShouldBeComputedProperty(cell, property),
]),
)(
`Row ${row.id} (${index}) / Cell ${cell.id} - property (%s) is computed -> (%s)`,
(name, expected) => {
const tableProperty = cell[name as keyof typeof cell]
expect(isSignal(tableProperty)).toEqual(expected)
},
)
})
})
})
describe('Integration', () => {
test('methods within effect will be re-trigger when options/state changes', () => {
const data = signal<Array<Data>>([{ id: '1', title: 'Title' }])
const table = createTestTable(data)
const isSelectedRow1Captor = vi.fn<(val: boolean) => void>()
const cellGetValueCaptor = vi.fn<(val: unknown) => void>()
const cellGetValueMemoizedCaptor = vi.fn<(val: unknown) => void>()
const columnIsVisibleCaptor = vi.fn<(val: boolean) => void>()
// This will test a case where you put in the effect a single cell property method
// which will trigger effect reschedule only when the value changes, acting like
// its a computed value
const cell = computed(
() => table.getRowModel().rows[0]!.getAllCells()[0]!,
)
const cellGetValue = computed(() => cell().getValue())
TestBed.runInInjectionContext(() => {
effect(() => {
isSelectedRow1Captor(cell().row.getIsSelected())
})
effect(() => {
cellGetValueCaptor(cell().getValue())
})
effect(() => {
cellGetValueMemoizedCaptor(cellGetValue())
})
effect(() => {
columnIsVisibleCaptor(cell().column.getIsVisible())
})
})
TestBed.tick()
expect(isSelectedRow1Captor).toHaveBeenCalledTimes(1)
expect(cellGetValueMemoizedCaptor).toHaveBeenCalledTimes(1)
expect(cellGetValueCaptor).toHaveBeenCalledTimes(1)
expect(columnIsVisibleCaptor).toHaveBeenCalledTimes(1)
cell().row.toggleSelected(true)
TestBed.tick()
expect(isSelectedRow1Captor).toHaveBeenCalledTimes(2)
expect(cellGetValueCaptor).toHaveBeenCalledTimes(1)
expect(columnIsVisibleCaptor).toHaveBeenCalledTimes(2)
data.set([{ id: '1', title: 'Title 3' }])
TestBed.tick()
// In this case it will be called twice since `data` will change and
// the cell instance will be recreated
expect(isSelectedRow1Captor).toHaveBeenCalledTimes(3)
expect(cellGetValueCaptor).toHaveBeenCalledTimes(2)
expect(columnIsVisibleCaptor).toHaveBeenCalledTimes(3)
cell().column.toggleVisibility(false)
TestBed.tick()
expect(isSelectedRow1Captor).toHaveBeenCalledTimes(4)
expect(cellGetValueCaptor).toHaveBeenCalledTimes(2)
expect(columnIsVisibleCaptor).toHaveBeenCalledTimes(4)
expect(isSelectedRow1Captor.mock.calls).toEqual([
[false],
[true],
[true],
[true],
])
expect(cellGetValueCaptor.mock.calls).toEqual([['1'], ['1']])
expect(columnIsVisibleCaptor.mock.calls).toEqual([
[true],
[true],
[true],
[false],
])
})
})
})