ray-template/__test__/components/table-pro-toolbar.spec.tsx
XiaoDaiGua-Ray 911c403434
feat: refine admin workflows for v6.0.3
Improve theme consistency, keyboard navigation, and reusable frontend components.
Harden fullstack initialization and expose scoped lint and Vitest commands.
2026-12-01 11:04:13 +08:00

302 lines
9.5 KiB
TypeScript

import { DOMWrapper, flushPromises, mount } from '@vue/test-utils'
import { NDataTable, NDropdown } from 'naive-ui'
import type { DropdownOption } from 'naive-ui'
import { RTablePro, useTablePro } from '../../packages/ui/src/RTablePro'
import type { TableProRequest } from '../../packages/ui/src/RTablePro'
import * as csv from '../../packages/ui/src/RTablePro/src/csv'
import * as image from '../../packages/ui/src/RTablePro/src/image'
import * as printing from '../../packages/ui/src/RTablePro/src/printing'
const columns = [{ key: 'name', title: 'Name' }]
const data = [{ id: 1, name: 'Ray' }]
afterEach(() => vi.restoreAllMocks())
it.each([void 0, 'small', 'medium', 'large'] as const)(
'checks the effective density before any interaction when size is %s',
(size) => {
const wrapper = mount(RTablePro, { props: { columns, data, size } })
try {
const density = wrapper
.getComponent(NDropdown)
.props('options')
?.find((option) => option.key === 'density') as DropdownOption
const choices = density.children as DropdownOption[]
const expected = size ?? 'medium'
expect(wrapper.getComponent(NDataTable).props('size')).toBe(expected)
expect(
choices
.filter((option) => option.props?.['aria-checked'])
.map((option) => option.key),
).toEqual([`density:${expected}`])
} finally {
wrapper.unmount()
}
},
)
it('hides print by default and groups secondary actions under one menu', async () => {
const wrapper = mount(RTablePro, { props: { columns, data } })
try {
const menu = wrapper.getComponent(NDropdown)
const keys = menu.props('options')?.map((option) => option.key)
expect(keys).toContain('csv')
expect(keys).toContain('density')
expect(keys).not.toContain('print')
expect(wrapper.find('[aria-label="Print table"]').exists()).toBe(false)
expect(
wrapper.get('[aria-label="More actions"]').attributes('aria-haspopup'),
).toBe('menu')
await wrapper.get('[aria-label="More actions"]').trigger('click')
await flushPromises()
expect(document.body.textContent).not.toContain('Print table')
} finally {
wrapper.unmount()
}
})
it('puts image copying in the named menu on narrow panels without pagination', async () => {
let resize: ((width: number) => void) | undefined
vi.stubGlobal(
'ResizeObserver',
class {
constructor(private readonly callback: ResizeObserverCallback) {}
observe(target: Element) {
if (target.matches('.r-table'))
resize = (width) =>
this.callback(
[
{
target,
contentRect: new DOMRect(0, 0, width, 200),
} as ResizeObserverEntry,
],
this,
)
}
disconnect() {}
unobserve() {}
},
)
const wrapper = mount(RTablePro, {
props: { columns, data, pagination: false },
})
try {
await flushPromises()
const menu = wrapper.getComponent(NDropdown)
const copyOption = () =>
menu.props('options')?.find(({ key }) => key === 'copy-image')
expect(resize).toBeTypeOf('function')
resize?.(390)
await flushPromises()
expect(copyOption()).toMatchObject({ label: 'Copy table image' })
await wrapper.setProps({ loading: true })
expect(copyOption()).toMatchObject({ disabled: true })
await wrapper.setProps({ loading: false })
const copy = vi.spyOn(image, 'copyTableImage').mockResolvedValue(void 0)
menu.vm.$emit('select', 'copy-image')
await flushPromises()
expect(copy).toHaveBeenCalledOnce()
expect(wrapper.find('button[aria-label="Copy table image"]').exists()).toBe(
false,
)
await wrapper.setProps({ showCopyImage: false })
expect(copyOption()).toBeUndefined()
await wrapper.setProps({ showCopyImage: true })
resize?.(960)
await flushPromises()
expect(copyOption()).toBeUndefined()
expect(wrapper.find('button[aria-label="Copy table image"]').exists()).toBe(
true,
)
} finally {
wrapper.unmount()
vi.unstubAllGlobals()
}
})
it('opts into menu printing and retains CSV formatting and output options', async () => {
const download = vi
.spyOn(csv, 'downloadTableCsv')
.mockImplementation(() => {})
const print = vi.spyOn(printing, 'printTableContent').mockResolvedValue(true)
const wrapper = mount(RTablePro, {
props: {
columns,
data,
showPrint: true,
downloadCsvTableOptions: { fileName: 'people' },
printTableOptions: { title: 'People report' },
getCsvCell: (value) => `Person: ${String(value)}`,
},
})
try {
const menu = wrapper.getComponent(NDropdown)
expect(
menu.props('options')?.some((option) => option.key === 'print'),
).toBe(true)
menu.vm.$emit('select', 'csv')
await flushPromises()
expect(download).toHaveBeenCalledExactlyOnceWith(
'"Name"\r\n"Person: Ray"',
'people',
)
menu.vm.$emit('select', 'print')
await flushPromises()
expect(print).toHaveBeenCalledOnce()
expect(print.mock.calls[0][1]).toMatchObject({ title: 'People report' })
await wrapper.setProps({ showPrint: false })
expect(
menu.props('options')?.some((option) => option.key === 'print'),
).toBe(false)
menu.vm.$emit('select', 'print')
await flushPromises()
expect(print).toHaveBeenCalledOnce()
} finally {
wrapper.unmount()
}
})
it('updates density and appearance without changing caller defaults', async () => {
const wrapper = mount(RTablePro, {
props: { columns, data, size: 'medium', striped: false, bordered: true },
})
try {
const menu = wrapper.getComponent(NDropdown)
menu.vm.$emit('select', 'density:small')
menu.vm.$emit('select', 'striped')
menu.vm.$emit('select', 'bordered')
await flushPromises()
expect(wrapper.getComponent(NDataTable).props()).toMatchObject({
size: 'small',
striped: true,
bordered: false,
})
expect(wrapper.props()).toMatchObject({
size: 'medium',
striped: false,
bordered: true,
})
await wrapper.setProps({ size: 'large' })
expect(wrapper.getComponent(NDataTable).props('size')).toBe('large')
await wrapper.setProps({ size: void 0 })
expect(wrapper.getComponent(NDataTable).props('size')).toBe('medium')
const density = menu
.props('options')
?.find((option) => option.key === 'density') as DropdownOption
expect(
(density.children as DropdownOption[])
.filter((option) => option.props?.['aria-checked'])
.map((option) => option.key),
).toEqual(['density:medium'])
} finally {
wrapper.unmount()
}
})
it('keeps appearance icons visible when checked or unchecked', async () => {
const wrapper = mount(RTablePro, {
props: {
columns,
data,
size: 'medium',
striped: false,
bordered: true,
showPrint: true,
},
})
const page = new DOMWrapper(document.body)
const openMenu = async () => {
await wrapper.get('[aria-label="More actions"]').trigger('click')
await vi.dynamicImportSettled()
await flushPromises()
}
const appearanceOption = (label: string) =>
page
.findAll('[role="menuitemcheckbox"]')
.find((option) => option.text().includes(label))!
try {
await openMenu()
for (const label of ['Table density', 'Export CSV', 'Print table']) {
const option = page
.findAll('.n-dropdown-option-body')
.find((option) => option.text().includes(label))!
expect(option.get('svg').html()).toContain('<path')
}
for (const [label, initial] of [
['Striped rows', false],
['Table border', true],
] as const) {
const option = appearanceOption(label)
const icon = option.get('.n-dropdown-option-body__prefix svg').html()
expect(icon).toContain('<path')
expect(option.attributes('aria-checked')).toBe(String(initial))
expect(option.get('.r-table-action-label__check').text()).toBe(
initial ? '✓' : '',
)
await option.trigger('click')
await flushPromises()
await openMenu()
const updated = appearanceOption(label)
expect(updated.attributes('aria-checked')).toBe(String(!initial))
expect(updated.get('.r-table-action-label__check').text()).toBe(
initial ? '' : '✓',
)
expect(updated.get('.n-dropdown-option-body__prefix svg').html()).toBe(
icon,
)
}
} finally {
wrapper.unmount()
}
})
it('refreshes the current remote query and preserves the caller toolbar when tools are disabled', async () => {
const [register, actions] = useTablePro()
const request = vi.fn<TableProRequest>(async () => ({ data, total: 30 }))
const wrapper = mount(RTablePro, {
props: { columns, request, onRegister: register },
slots: { toolbar: 'Business action' },
})
try {
await flushPromises()
expect(request).toHaveBeenCalledOnce()
await actions.setPage(2)
await flushPromises()
await wrapper.get('[aria-label="Refresh"]').trigger('click')
await flushPromises()
expect(request.mock.calls.map(([params]) => params.page)).toEqual([1, 2, 2])
expect(request.mock.lastCall?.[1]).not.toBe(request.mock.calls[1][1])
expect(wrapper.get('.r-table__business-actions').text()).toBe(
'Business action',
)
await wrapper.setProps({ tool: false })
expect(wrapper.find('.r-table__utilities').exists()).toBe(false)
expect(wrapper.get('.r-table__business-actions').text()).toBe(
'Business action',
)
} finally {
wrapper.unmount()
}
})