|
| 1 | +/** @jest-environment jsdom */ |
| 2 | +import { shallowMount } from '@vue/test-utils' |
| 3 | +import { DataGrid } from '@/components/grid' |
| 4 | + |
| 5 | +jest.mock('@tauri-apps/api/core', () => ({ invoke: jest.fn() })) |
| 6 | +jest.mock('@vueuse/core', () => ({ useResizeObserver: jest.fn() })) |
| 7 | +jest.mock('@/composables/useNotifications', () => ({ toast: { error: jest.fn() } })) |
| 8 | +jest.mock('@/composables/useDataGridCopy', () => { |
| 9 | + const copyUtil = { copyRowsAs: jest.fn(), exportToFile: jest.fn() } |
| 10 | + return { useDataGridCopy: () => copyUtil } |
| 11 | +}) |
| 12 | +jest.mock('vue-i18n', () => ({ |
| 13 | + useI18n: () => ({ t: (key: string, params?: Record<string, unknown>) => `${key} ${JSON.stringify(params ?? {})}` }), |
| 14 | +})) |
| 15 | +jest.mock('@tanstack/vue-virtual', () => ({ |
| 16 | + useVirtualizer: (options: { value: { count: number } }) => ({ |
| 17 | + getTotalSize: () => options.value.count * 32, |
| 18 | + getVirtualItems: () => Array.from({ length: options.value.count }, (_, index) => ({ index, size: 32, start: index * 32 })), |
| 19 | + }), |
| 20 | +})) |
| 21 | +jest.mock('@/components/grid/DataGridPagination.vue', () => ({ |
| 22 | + __esModule: true, |
| 23 | + default: { name: 'DataGridPagination', props: ['currentPage', 'pageCount', 'pageSize', 'disabled'], template: '<nav />' }, |
| 24 | +})) |
| 25 | +jest.mock('@/components/grid/EditRowDialog.vue', () => ({ __esModule: true, default: { template: '<div />' } })) |
| 26 | +jest.mock('@/components/grid/RowDetailsDialog.vue', () => ({ |
| 27 | + __esModule: true, |
| 28 | + default: { name: 'RowDetailsDialog', props: ['row'], template: '<div />' }, |
| 29 | +})) |
| 30 | +jest.mock('@/components/grid/JsonViewDialog.vue', () => ({ __esModule: true, default: { template: '<div />' } })) |
| 31 | +jest.mock('@/components/grid/CellContextMenu.vue', () => ({ __esModule: true, default: { template: '<div />' } })) |
| 32 | +jest.mock('@/components/grid/ColumnHeaderContextMenu.vue', () => ({ __esModule: true, default: { template: '<div />' } })) |
| 33 | +jest.mock('@/components/grid/BatchActionBar.vue', () => ({ __esModule: true, default: { template: '<div />' } })) |
| 34 | +jest.mock('@/components/grid/FilterBar.vue', () => ({ __esModule: true, default: { template: '<div />' } })) |
| 35 | +jest.mock('@/components/ui/button', () => ({ Button: { name: 'Button', template: '<button><slot /></button>' } })) |
| 36 | +jest.mock('@/components/ui/checkbox', () => ({ Checkbox: { name: 'Checkbox', template: '<input type="checkbox">' } })) |
| 37 | +jest.mock('@/components/ui/dropdown-menu', () => ({})) |
| 38 | +jest.mock('@/components/ui/alert-dialog', () => Object.fromEntries( |
| 39 | + ['AlertDialog', 'AlertDialogAction', 'AlertDialogCancel', 'AlertDialogContent', 'AlertDialogDescription', 'AlertDialogFooter', 'AlertDialogHeader', 'AlertDialogTitle'] |
| 40 | + .map(name => [name, { name, template: '<div><slot /></div>' }]), |
| 41 | +)) |
| 42 | + |
| 43 | +const rows = Array.from({ length: 235 }, (_, id) => ({ id: 235 - id })) |
| 44 | +const mountGrid = () => shallowMount(DataGrid, { |
| 45 | + props: { columns: ['id'], rows, rowCount: rows.length, columnTypes: { id: 'integer' } }, |
| 46 | + global: { renderStubDefaultSlot: true, mocks: { $t: (key: string) => key } }, |
| 47 | +}) |
| 48 | + |
| 49 | +describe('paged result grid', () => { |
| 50 | + it('renders the requested page and sorts only its rows without mutating the result', async () => { |
| 51 | + const wrapper = mountGrid() |
| 52 | + const pagination = wrapper.findComponent({ name: 'DataGridPagination' }) |
| 53 | + expect(wrapper.findAll('[data-index]')).toHaveLength(100) |
| 54 | + expect(pagination.props('pageCount')).toBe(3) |
| 55 | + pagination.vm.$emit('pageChange', 3) |
| 56 | + await wrapper.vm.$nextTick() |
| 57 | + expect(wrapper.findAll('[data-index]')).toHaveLength(35) |
| 58 | + expect(wrapper.find('[data-index="0"]').text()).toContain('35') |
| 59 | + expect(wrapper.find('[role="status"]').text()).toContain('"start":201,"end":235,"total":"235"') |
| 60 | + const sortHeader = wrapper.findAll('button').find(button => button.text().includes('id'))! |
| 61 | + await sortHeader.trigger('click') |
| 62 | + expect(wrapper.find('[data-index="0"]').text()).toContain('1') |
| 63 | + expect(rows[0].id).toBe(235) |
| 64 | + expect(rows[200].id).toBe(35) |
| 65 | + const { invoke } = jest.requireMock('@tauri-apps/api/core') |
| 66 | + expect(invoke).not.toHaveBeenCalled() |
| 67 | + }) |
| 68 | + |
| 69 | + it('resets selection and scroll when changing pages', async () => { |
| 70 | + const wrapper = mountGrid() |
| 71 | + const firstRow = wrapper.get('[data-index="0"]') |
| 72 | + firstRow.findComponent({ name: 'Checkbox' }).vm.$emit('update:checked', true) |
| 73 | + await wrapper.vm.$nextTick() |
| 74 | + expect(wrapper.get('[data-index="0"]').classes()).toContain('bg-primary/[0.08]') |
| 75 | + const scroller = wrapper.find('.overflow-auto').element as HTMLDivElement |
| 76 | + scroller.scrollTop = 320 |
| 77 | + wrapper.findComponent({ name: 'DataGridPagination' }).vm.$emit('pageChange', 2) |
| 78 | + await wrapper.vm.$nextTick() |
| 79 | + expect(scroller.scrollTop).toBe(0) |
| 80 | + expect(wrapper.get('[data-index="0"]').classes()).not.toContain('bg-primary/[0.08]') |
| 81 | + expect(wrapper.get('[data-index="0"]').text()).toContain('135') |
| 82 | + }) |
| 83 | + |
| 84 | + it('copies and exports every returned row while viewing a later page', async () => { |
| 85 | + const wrapper = mountGrid() |
| 86 | + wrapper.findComponent({ name: 'DataGridPagination' }).vm.$emit('pageChange', 3) |
| 87 | + await wrapper.vm.$nextTick() |
| 88 | + const buttons = wrapper.findAllComponents({ name: 'Button' }) |
| 89 | + buttons[0].vm.$emit('click') |
| 90 | + buttons[3].vm.$emit('click') |
| 91 | + const copyUtil = jest.requireMock('@/composables/useDataGridCopy').useDataGridCopy() |
| 92 | + expect(copyUtil.copyRowsAs).toHaveBeenCalledWith(rows, ['id'], 'csv', undefined) |
| 93 | + expect(copyUtil.exportToFile).toHaveBeenCalledWith(rows, ['id'], 'csv', undefined) |
| 94 | + await wrapper.setProps({ truncated: true }) |
| 95 | + expect(wrapper.get('[role="status"]').text()).toContain('status.showingLimited') |
| 96 | + expect(wrapper.get('[role="status"]').text()).toContain('"start":201,"end":235,"loaded":"235"') |
| 97 | + }) |
| 98 | + |
| 99 | + it('shows all rows and resets to the first page when results are replaced', async () => { |
| 100 | + const wrapper = mountGrid() |
| 101 | + const pagination = wrapper.findComponent({ name: 'DataGridPagination' }) |
| 102 | + pagination.vm.$emit('pageSizeChange', 'all') |
| 103 | + await wrapper.vm.$nextTick() |
| 104 | + expect(wrapper.findAll('[data-index]')).toHaveLength(235) |
| 105 | + pagination.vm.$emit('pageSizeChange', 50) |
| 106 | + pagination.vm.$emit('pageChange', 4) |
| 107 | + await wrapper.vm.$nextTick() |
| 108 | + expect(pagination.props('currentPage')).toBe(4) |
| 109 | + await wrapper.setProps({ rows: [{ id: 7 }], rowCount: 1 }) |
| 110 | + expect(pagination.props('currentPage')).toBe(1) |
| 111 | + expect(pagination.props('pageSize')).toBe(50) |
| 112 | + expect(wrapper.findAll('[data-index]')).toHaveLength(1) |
| 113 | + await wrapper.setProps({ rows: [], rowCount: 0 }) |
| 114 | + expect(pagination.exists()).toBe(true) |
| 115 | + expect(wrapper.get('[role="status"]').text()).toContain('"start":0,"end":0,"total":"0"') |
| 116 | + await wrapper.setProps({ loading: true }) |
| 117 | + expect(pagination.props('disabled')).toBe(true) |
| 118 | + await wrapper.setProps({ loading: false, error: 'Failed' }) |
| 119 | + expect(pagination.props('disabled')).toBe(true) |
| 120 | + }) |
| 121 | +}) |
0 commit comments