|
| 1 | +import { describe, expect, test, vi } from 'vitest' |
| 2 | +import { computed, effect, isSignal, signal } from '@angular/core' |
| 3 | +import { TestBed } from '@angular/core/testing' |
| 4 | +import { injectTable, stockFeatures } from '../src' |
| 5 | +import { getFnReactiveCache, testShouldBeComputedProperty } from './test-utils' |
| 6 | +import type { WritableSignal } from '@angular/core' |
| 7 | +import type { ColumnDef } from '../src' |
| 8 | + |
| 9 | +describe('angularReactivityFeature', () => { |
| 10 | + type Data = { id: string; title: string } |
| 11 | + const data = signal<Array<Data>>([{ id: '1', title: 'Title' }]) |
| 12 | + const columns: Array<ColumnDef<typeof stockFeatures, Data>> = [ |
| 13 | + { |
| 14 | + id: 'id', |
| 15 | + header: 'Id', |
| 16 | + accessorKey: 'id', |
| 17 | + cell: (context) => context.getValue(), |
| 18 | + }, |
| 19 | + { |
| 20 | + id: 'title', |
| 21 | + header: 'Title', |
| 22 | + accessorKey: 'title', |
| 23 | + cell: (context) => context.getValue(), |
| 24 | + }, |
| 25 | + ] |
| 26 | + |
| 27 | + function createTestTable(_data: WritableSignal<Array<Data>> = data) { |
| 28 | + return TestBed.runInInjectionContext(() => |
| 29 | + injectTable(() => ({ |
| 30 | + data: _data(), |
| 31 | + _features: { ...stockFeatures }, |
| 32 | + columns: columns, |
| 33 | + getRowId: (row) => row.id, |
| 34 | + reactivity: { |
| 35 | + column: true, |
| 36 | + cell: true, |
| 37 | + row: true, |
| 38 | + header: true, |
| 39 | + }, |
| 40 | + })), |
| 41 | + ) |
| 42 | + } |
| 43 | + |
| 44 | + const table = createTestTable() |
| 45 | + const tablePropertyKeys = Object.keys(table) |
| 46 | + |
| 47 | + describe('Table property reactivity', () => { |
| 48 | + test.each( |
| 49 | + tablePropertyKeys.map((property) => [ |
| 50 | + property, |
| 51 | + testShouldBeComputedProperty(table, property), |
| 52 | + ]), |
| 53 | + )('property (%s) is computed -> (%s)', (name, expected) => { |
| 54 | + const tableProperty = table[name as keyof typeof table] |
| 55 | + expect(isSignal(tableProperty)).toEqual(expected) |
| 56 | + }) |
| 57 | + |
| 58 | + describe('will create a computed for non detectable computed properties', () => { |
| 59 | + test('getIsSomeRowsPinned', () => { |
| 60 | + table.getIsSomeRowsPinned('top') |
| 61 | + table.getIsSomeRowsPinned('bottom') |
| 62 | + table.getIsSomeRowsPinned() |
| 63 | + |
| 64 | + expect(getFnReactiveCache(table.getIsSomeRowsPinned)).toHaveProperty( |
| 65 | + '["top"]', |
| 66 | + ) |
| 67 | + expect(getFnReactiveCache(table.getIsSomeRowsPinned)).toHaveProperty( |
| 68 | + '["bottom"]', |
| 69 | + ) |
| 70 | + expect(getFnReactiveCache(table.getIsSomeRowsPinned)).toHaveProperty( |
| 71 | + '[]', |
| 72 | + ) |
| 73 | + }) |
| 74 | + }) |
| 75 | + }) |
| 76 | + |
| 77 | + describe('Header property reactivity', () => { |
| 78 | + const headers = table.getHeaderGroups() |
| 79 | + headers.forEach((headerGroup, index) => { |
| 80 | + const headerPropertyKeys = Object.keys(headerGroup) |
| 81 | + test.each( |
| 82 | + headerPropertyKeys.map((property) => [ |
| 83 | + property, |
| 84 | + testShouldBeComputedProperty(headerGroup, property), |
| 85 | + ]), |
| 86 | + )( |
| 87 | + `HeaderGroup ${headerGroup.id} (${index}) - property (%s) is computed -> (%s)`, |
| 88 | + (name, expected) => { |
| 89 | + const tableProperty = headerGroup[name as keyof typeof headerGroup] |
| 90 | + expect(isSignal(tableProperty)).toEqual(expected) |
| 91 | + }, |
| 92 | + ) |
| 93 | + |
| 94 | + const headers = headerGroup.headers |
| 95 | + headers.forEach((header, cellIndex) => { |
| 96 | + const headerPropertyKeys = Object.keys(header) |
| 97 | + test.each( |
| 98 | + headerPropertyKeys.map((property) => [ |
| 99 | + property, |
| 100 | + testShouldBeComputedProperty(header, property), |
| 101 | + ]), |
| 102 | + )( |
| 103 | + `HeaderGroup ${headerGroup.id} (${index}) / Header ${header.id} - property (%s) is computed -> (%s)`, |
| 104 | + (name, expected) => { |
| 105 | + const tableProperty = header[name as keyof typeof header] |
| 106 | + expect(isSignal(tableProperty)).toEqual(expected) |
| 107 | + }, |
| 108 | + ) |
| 109 | + }) |
| 110 | + }) |
| 111 | + }) |
| 112 | + |
| 113 | + describe('Column property reactivity', () => { |
| 114 | + const columns = table.getAllColumns() |
| 115 | + columns.forEach((column, index) => { |
| 116 | + const columnPropertyKeys = Object.keys(column) |
| 117 | + test.each( |
| 118 | + columnPropertyKeys.map((property) => [ |
| 119 | + property, |
| 120 | + testShouldBeComputedProperty(column, property), |
| 121 | + ]), |
| 122 | + )( |
| 123 | + `Column ${column.id} (${index}) - property (%s) is computed -> (%s)`, |
| 124 | + (name, expected) => { |
| 125 | + const tableProperty = column[name as keyof typeof column] |
| 126 | + expect(isSignal(tableProperty)).toEqual(expected) |
| 127 | + }, |
| 128 | + ) |
| 129 | + }) |
| 130 | + }) |
| 131 | + |
| 132 | + describe('Row property reactivity', () => { |
| 133 | + const flatRows = table.getRowModel().flatRows |
| 134 | + flatRows.forEach((row, index) => { |
| 135 | + const rowsPropertyKeys = Object.keys(row) |
| 136 | + test.each( |
| 137 | + rowsPropertyKeys.map((property) => [ |
| 138 | + property, |
| 139 | + testShouldBeComputedProperty(row, property), |
| 140 | + ]), |
| 141 | + )( |
| 142 | + `Row ${row.id} (${index}) - property (%s) is computed -> (%s)`, |
| 143 | + (name, expected) => { |
| 144 | + const tableProperty = row[name as keyof typeof row] |
| 145 | + expect(isSignal(tableProperty)).toEqual(expected) |
| 146 | + }, |
| 147 | + ) |
| 148 | + |
| 149 | + const cells = row.getAllCells() |
| 150 | + cells.forEach((cell, cellIndex) => { |
| 151 | + const cellPropertyKeys = Object.keys(cell) |
| 152 | + test.each( |
| 153 | + cellPropertyKeys.map((property) => [ |
| 154 | + property, |
| 155 | + testShouldBeComputedProperty(cell, property), |
| 156 | + ]), |
| 157 | + )( |
| 158 | + `Row ${row.id} (${index}) / Cell ${cell.id} - property (%s) is computed -> (%s)`, |
| 159 | + (name, expected) => { |
| 160 | + const tableProperty = cell[name as keyof typeof cell] |
| 161 | + expect(isSignal(tableProperty)).toEqual(expected) |
| 162 | + }, |
| 163 | + ) |
| 164 | + }) |
| 165 | + }) |
| 166 | + }) |
| 167 | + |
| 168 | + describe('Integration', () => { |
| 169 | + test('methods works will be reactive effects', () => { |
| 170 | + const data = signal<Array<Data>>([{ id: '1', title: 'Title' }]) |
| 171 | + const table = createTestTable(data) |
| 172 | + const isSelectedRow1Captor = vi.fn<(val: boolean) => void>() |
| 173 | + const cellGetValueCaptor = vi.fn<(val: unknown) => void>() |
| 174 | + const columnIsVisibleCaptor = vi.fn<(val: boolean) => void>() |
| 175 | + |
| 176 | + // This will test a case where you put in the effect a single cell property method |
| 177 | + // which will trigger effect reschedule only when the value changes, acting like |
| 178 | + // its a computed value |
| 179 | + const cell = computed( |
| 180 | + () => table.getRowModel().rows[0]!.getAllCells()[0]!, |
| 181 | + ) |
| 182 | + |
| 183 | + TestBed.runInInjectionContext(() => { |
| 184 | + effect(() => { |
| 185 | + isSelectedRow1Captor(cell().row.getIsSelected()) |
| 186 | + }) |
| 187 | + effect(() => { |
| 188 | + cellGetValueCaptor(cell().getValue()) |
| 189 | + }) |
| 190 | + effect(() => { |
| 191 | + columnIsVisibleCaptor(cell().column.getIsVisible()) |
| 192 | + }) |
| 193 | + }) |
| 194 | + |
| 195 | + TestBed.tick() |
| 196 | + expect(isSelectedRow1Captor).toHaveBeenCalledTimes(1) |
| 197 | + expect(cellGetValueCaptor).toHaveBeenCalledTimes(1) |
| 198 | + expect(columnIsVisibleCaptor).toHaveBeenCalledTimes(1) |
| 199 | + |
| 200 | + cell().row.toggleSelected(true) |
| 201 | + TestBed.tick() |
| 202 | + expect(isSelectedRow1Captor).toHaveBeenCalledTimes(2) |
| 203 | + expect(cellGetValueCaptor).toHaveBeenCalledTimes(1) |
| 204 | + expect(columnIsVisibleCaptor).toHaveBeenCalledTimes(1) |
| 205 | + |
| 206 | + data.set([{ id: '1', title: 'Title 3' }]) |
| 207 | + TestBed.tick() |
| 208 | + // In this case it will be called twice since `data` will change and |
| 209 | + // the cell instance will be recreated |
| 210 | + expect(isSelectedRow1Captor).toHaveBeenCalledTimes(3) |
| 211 | + expect(cellGetValueCaptor).toHaveBeenCalledTimes(2) |
| 212 | + expect(columnIsVisibleCaptor).toHaveBeenCalledTimes(2) |
| 213 | + |
| 214 | + cell().column.toggleVisibility(false) |
| 215 | + TestBed.tick() |
| 216 | + expect(isSelectedRow1Captor).toHaveBeenCalledTimes(3) |
| 217 | + expect(cellGetValueCaptor).toHaveBeenCalledTimes(2) |
| 218 | + expect(columnIsVisibleCaptor).toHaveBeenCalledTimes(3) |
| 219 | + |
| 220 | + expect(isSelectedRow1Captor.mock.calls).toEqual([[false], [true], [true]]) |
| 221 | + expect(cellGetValueCaptor.mock.calls).toEqual([['1'], ['1']]) |
| 222 | + expect(columnIsVisibleCaptor.mock.calls).toEqual([ |
| 223 | + [true], |
| 224 | + [true], |
| 225 | + [false], |
| 226 | + ]) |
| 227 | + }) |
| 228 | + }) |
| 229 | +}) |
0 commit comments