|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { beforeEach, describe, expect, it } from 'vitest' |
| 5 | +import { useTableUndoStore } from '@/stores/table/store' |
| 6 | +import type { TableUndoAction } from '@/stores/table/types' |
| 7 | + |
| 8 | +const TABLE = 'tbl-1' |
| 9 | + |
| 10 | +const reorder: TableUndoAction = { |
| 11 | + type: 'reorder-columns', |
| 12 | + previousOrder: ['a', 'b'], |
| 13 | + newOrder: ['b', 'a'], |
| 14 | +} |
| 15 | +const updateCell: TableUndoAction = { |
| 16 | + type: 'update-cell', |
| 17 | + rowId: 'r1', |
| 18 | + columnName: 'a', |
| 19 | + previousValue: 1, |
| 20 | + newValue: 2, |
| 21 | +} |
| 22 | + |
| 23 | +describe('pruneLayoutActions', () => { |
| 24 | + beforeEach(() => { |
| 25 | + useTableUndoStore.getState().clear(TABLE) |
| 26 | + }) |
| 27 | + |
| 28 | + it('drops a layout action recorded under a different view', () => { |
| 29 | + const store = useTableUndoStore.getState() |
| 30 | + store.push(TABLE, reorder, 'view-a') |
| 31 | + |
| 32 | + store.pruneLayoutActions(TABLE, 'view-b') |
| 33 | + |
| 34 | + expect(useTableUndoStore.getState().stacks[TABLE]?.undo).toHaveLength(0) |
| 35 | + }) |
| 36 | + |
| 37 | + it('keeps a layout action when the owning view is still active', () => { |
| 38 | + const store = useTableUndoStore.getState() |
| 39 | + store.push(TABLE, reorder, 'view-a') |
| 40 | + |
| 41 | + store.pruneLayoutActions(TABLE, 'view-a') |
| 42 | + |
| 43 | + expect(useTableUndoStore.getState().stacks[TABLE]?.undo).toHaveLength(1) |
| 44 | + }) |
| 45 | + |
| 46 | + it('keeps row actions across a view switch — rows are table-scoped', () => { |
| 47 | + const store = useTableUndoStore.getState() |
| 48 | + store.push(TABLE, updateCell, 'view-a') |
| 49 | + |
| 50 | + store.pruneLayoutActions(TABLE, 'view-b') |
| 51 | + |
| 52 | + const undo = useTableUndoStore.getState().stacks[TABLE]?.undo |
| 53 | + expect(undo).toHaveLength(1) |
| 54 | + expect(undo?.[0].action.type).toBe('update-cell') |
| 55 | + }) |
| 56 | + |
| 57 | + it('prunes the redo stack too, so redo cannot replay into the wrong view', () => { |
| 58 | + const store = useTableUndoStore.getState() |
| 59 | + store.push(TABLE, reorder, 'view-a') |
| 60 | + store.popUndo(TABLE) |
| 61 | + expect(useTableUndoStore.getState().stacks[TABLE]?.redo).toHaveLength(1) |
| 62 | + |
| 63 | + store.pruneLayoutActions(TABLE, 'view-b') |
| 64 | + |
| 65 | + expect(useTableUndoStore.getState().stacks[TABLE]?.redo).toHaveLength(0) |
| 66 | + }) |
| 67 | + |
| 68 | + it('treats All (null) as its own owner', () => { |
| 69 | + const store = useTableUndoStore.getState() |
| 70 | + store.push(TABLE, reorder, null) |
| 71 | + |
| 72 | + store.pruneLayoutActions(TABLE, 'view-a') |
| 73 | + expect(useTableUndoStore.getState().stacks[TABLE]?.undo).toHaveLength(0) |
| 74 | + }) |
| 75 | +}) |
0 commit comments