|
| 1 | +import { describe, expect, it, vi } from 'vitest' |
| 2 | +import { |
| 3 | + ColumnDef, |
| 4 | + createTable, |
| 5 | + functionalUpdate, |
| 6 | + getCoreRowModel, |
| 7 | + PaginationState, |
| 8 | +} from '../src' |
| 9 | + |
| 10 | +type Person = { |
| 11 | + name: string |
| 12 | +} |
| 13 | + |
| 14 | +const data: Person[] = [{ name: 'Ada' }] |
| 15 | +const columns: ColumnDef<Person>[] = [{ accessorKey: 'name' }] |
| 16 | + |
| 17 | +function createPaginationTable( |
| 18 | + pagination: PaginationState, |
| 19 | + onPaginationChange = vi.fn(), |
| 20 | +) { |
| 21 | + const table = createTable<Person>({ |
| 22 | + data, |
| 23 | + columns, |
| 24 | + getCoreRowModel: getCoreRowModel(), |
| 25 | + onPaginationChange, |
| 26 | + onStateChange() {}, |
| 27 | + renderFallbackValue: '', |
| 28 | + state: { |
| 29 | + pagination, |
| 30 | + }, |
| 31 | + }) |
| 32 | + |
| 33 | + return { table, onPaginationChange } |
| 34 | +} |
| 35 | + |
| 36 | +function createStatefulPaginationTable(pagination: PaginationState) { |
| 37 | + const state = { pagination } |
| 38 | + const onStateChange = vi.fn() |
| 39 | + const table = createTable<Person>({ |
| 40 | + data, |
| 41 | + columns, |
| 42 | + getCoreRowModel: getCoreRowModel(), |
| 43 | + onStateChange, |
| 44 | + renderFallbackValue: '', |
| 45 | + state, |
| 46 | + }) |
| 47 | + |
| 48 | + return { table, onStateChange, state } |
| 49 | +} |
| 50 | + |
| 51 | +describe('RowPagination', () => { |
| 52 | + it('keeps pagination state identity when resetting to the current page index', () => { |
| 53 | + const { table, onPaginationChange } = createPaginationTable({ |
| 54 | + pageIndex: 0, |
| 55 | + pageSize: 10, |
| 56 | + }) |
| 57 | + const pagination = table.getState().pagination |
| 58 | + |
| 59 | + table.resetPageIndex() |
| 60 | + |
| 61 | + expect(onPaginationChange).toHaveBeenCalledOnce() |
| 62 | + expect( |
| 63 | + functionalUpdate(onPaginationChange.mock.calls[0][0], pagination), |
| 64 | + ).toBe(pagination) |
| 65 | + }) |
| 66 | + |
| 67 | + it('keeps table state identity when resetting to the current page index through the default state updater', () => { |
| 68 | + const { table, onStateChange, state } = createStatefulPaginationTable({ |
| 69 | + pageIndex: 0, |
| 70 | + pageSize: 10, |
| 71 | + }) |
| 72 | + |
| 73 | + table.resetPageIndex() |
| 74 | + |
| 75 | + expect(onStateChange).toHaveBeenCalledOnce() |
| 76 | + expect(functionalUpdate(onStateChange.mock.calls[0][0], state)).toBe(state) |
| 77 | + }) |
| 78 | + |
| 79 | + it('does not drop a reset queued after another page index update', () => { |
| 80 | + const { table, onStateChange, state } = createStatefulPaginationTable({ |
| 81 | + pageIndex: 0, |
| 82 | + pageSize: 10, |
| 83 | + }) |
| 84 | + |
| 85 | + table.setPageIndex(1) |
| 86 | + table.resetPageIndex() |
| 87 | + |
| 88 | + expect(onStateChange).toHaveBeenCalledTimes(2) |
| 89 | + |
| 90 | + const stateAfterPageChange = functionalUpdate( |
| 91 | + onStateChange.mock.calls[0][0], |
| 92 | + state, |
| 93 | + ) |
| 94 | + expect(stateAfterPageChange).not.toBe(state) |
| 95 | + expect(stateAfterPageChange.pagination.pageIndex).toBe(1) |
| 96 | + |
| 97 | + const stateAfterReset = functionalUpdate( |
| 98 | + onStateChange.mock.calls[1][0], |
| 99 | + stateAfterPageChange, |
| 100 | + ) |
| 101 | + expect(stateAfterReset).not.toBe(stateAfterPageChange) |
| 102 | + expect(stateAfterReset.pagination.pageIndex).toBe(0) |
| 103 | + }) |
| 104 | + |
| 105 | + it('emits a pagination change when resetting from a different page index', () => { |
| 106 | + const { table, onPaginationChange } = createPaginationTable({ |
| 107 | + pageIndex: 1, |
| 108 | + pageSize: 10, |
| 109 | + }) |
| 110 | + |
| 111 | + table.resetPageIndex() |
| 112 | + |
| 113 | + expect(onPaginationChange).toHaveBeenCalledOnce() |
| 114 | + expect( |
| 115 | + functionalUpdate(onPaginationChange.mock.calls[0][0], { |
| 116 | + pageIndex: 1, |
| 117 | + pageSize: 10, |
| 118 | + }), |
| 119 | + ).toEqual({ |
| 120 | + pageIndex: 0, |
| 121 | + pageSize: 10, |
| 122 | + }) |
| 123 | + }) |
| 124 | + |
| 125 | + it('keeps pagination state identity when auto-resetting an already reset page index', async () => { |
| 126 | + const { table, onPaginationChange } = createPaginationTable({ |
| 127 | + pageIndex: 0, |
| 128 | + pageSize: 10, |
| 129 | + }) |
| 130 | + const pagination = table.getState().pagination |
| 131 | + |
| 132 | + table._autoResetPageIndex() |
| 133 | + await Promise.resolve() |
| 134 | + |
| 135 | + table._autoResetPageIndex() |
| 136 | + await Promise.resolve() |
| 137 | + |
| 138 | + expect(onPaginationChange).toHaveBeenCalledOnce() |
| 139 | + expect( |
| 140 | + functionalUpdate(onPaginationChange.mock.calls[0][0], pagination), |
| 141 | + ).toBe(pagination) |
| 142 | + }) |
| 143 | + |
| 144 | + it('keeps pagination state identity when core row data changes and the page index is already reset', async () => { |
| 145 | + const { table, onPaginationChange } = createPaginationTable({ |
| 146 | + pageIndex: 0, |
| 147 | + pageSize: 10, |
| 148 | + }) |
| 149 | + const pagination = table.getState().pagination |
| 150 | + |
| 151 | + table.getRowModel() |
| 152 | + await Promise.resolve() |
| 153 | + onPaginationChange.mockClear() |
| 154 | + |
| 155 | + table.setOptions((old) => ({ |
| 156 | + ...old, |
| 157 | + data: [{ name: 'Grace' }], |
| 158 | + })) |
| 159 | + table.getRowModel() |
| 160 | + await Promise.resolve() |
| 161 | + |
| 162 | + expect(onPaginationChange).toHaveBeenCalledOnce() |
| 163 | + expect( |
| 164 | + functionalUpdate(onPaginationChange.mock.calls[0][0], pagination), |
| 165 | + ).toBe(pagination) |
| 166 | + }) |
| 167 | +}) |
0 commit comments