// @vitest-environment jsdom /** * 「单行」那一档的列宽契约(换行那一档见 DataTable.wrap.spec.ts): * 1. 没动过手时一切照旧——表格跟着内容排(`min-w-max`),代码里一个像素都不必猜; * 2. 表头右边缘那条把手能拖,拖完这张表真按像素定宽(切固定布局 + 一组 ``); * 3. 拖到哪一格就是多少,且拖不出上下限;双击回浏览器排出来的那一版; * 4. 拖过的那一档存本地,下次进来还是它;没钥匙就不写盘。 * * 这些用例统一传 `wrap-text=false`:换行是默认档,会把排版接管过来, * 而这一份文件要钉的正是"没接管时那套老行为还在"。 */ import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest' import { mount } from '@vue/test-utils' import { createI18n } from 'vue-i18n' import zh from '@/i18n/locales/zh' import type { Column } from './types' import DataTable from './DataTable.vue' const i18n = createI18n({ legacy: false, locale: 'zh', messages: { zh } }) const COLUMNS: Column[] = [ { key: 'title', label: '标题' }, { key: 'status', label: '状态' }, { key: 'actions', label: '操作' } ] const ROWS = [{ id: 1, title: '一份报销单', status: '处理中' }] // 浏览器自己排出来的那一版:标题 400 / 状态 140 / 操作 200 const MEASURED: Record = { title: 400, status: 140, actions: 200 } /** 这一组列在「单行」那一档的账本键(列清单 + 档位)。 */ const SHAPE = 'title|status|actions#nowrap' /** jsdom 没有 PointerEvent,把手那一路全靠 pointer 事件,这里补一颗够用的。 */ class FakePointerEvent extends Event { clientX: number pointerId: number button: number constructor(type: string, init: { clientX?: number; pointerId?: number; button?: number } = {}) { super(type, { bubbles: true, cancelable: true }) this.clientX = init.clientX ?? 0 this.pointerId = init.pointerId ?? 1 this.button = init.button ?? 0 } } beforeAll(() => { vi.stubGlobal('PointerEvent', FakePointerEvent) window.matchMedia = ((query: string) => ({ matches: true, media: query, onchange: null, addEventListener: vi.fn(), removeEventListener: vi.fn(), addListener: vi.fn(), removeListener: vi.fn(), dispatchEvent: vi.fn() })) as unknown as typeof window.matchMedia }) beforeEach(() => { localStorage.clear() // jsdom 不排版,量到的永远是 0;这里代成上面那一版,模拟「浏览器排好的宽度」 Element.prototype.getBoundingClientRect = function getBoundingClientRect() { const cell = (this as HTMLElement).closest('th') const label = cell?.textContent?.trim() ?? '' const width = label === '标题' ? MEASURED.title : label === '状态' ? MEASURED.status : MEASURED.actions return { width, height: 40, top: 0, left: 0, right: width, bottom: 40, x: 0, y: 0, toJSON: () => ({}) } } }) function open(props: Record = {}) { return mount(DataTable, { props: { columns: COLUMNS, data: ROWS, wrapText: false, ...props }, global: { plugins: [i18n] } }) } const handle = (wrapper: ReturnType, key: string) => wrapper.get(`[data-test="resize-col-${key}"]`) async function drag(wrapper: ReturnType, key: string, deltaX: number) { const target = handle(wrapper, key).element target.dispatchEvent(new FakePointerEvent('pointerdown', { clientX: 500, button: 0 })) document.dispatchEvent(new FakePointerEvent('pointermove', { clientX: 500 + deltaX })) document.dispatchEvent(new FakePointerEvent('pointerup', { clientX: 500 + deltaX })) await wrapper.vm.$nextTick() } const colWidths = (wrapper: ReturnType) => wrapper.findAll('colgroup col').map((col) => col.attributes('style') ?? '') describe('没动手时照旧', () => { it('不传 table-class 就用 min-w-max,列跟着内容走', () => { const wrapper = open() expect(wrapper.find('table').classes()).toContain('min-w-max') expect(wrapper.find('colgroup').exists()).toBe(false) }) it('传了 table-class 就顶掉 min-w-max,两条不许同时挂着', () => { const wrapper = open({ tableClass: 'table-fixed min-w-[900px]' }) const classes = wrapper.find('table').classes() expect(classes).toContain('table-fixed') expect(classes).toContain('min-w-[900px]') expect(classes).not.toContain('min-w-max') expect(classes).toContain('w-full') }) }) describe('拖宽', () => { it('拖一下就按像素定宽:切固定布局、 跟着宽度走、整表最小宽度算得对', async () => { const wrapper = open() await drag(wrapper, 'title', 200) expect(wrapper.find('table').classes()).toContain('table-fixed') expect(wrapper.find('table').classes()).not.toContain('min-w-max') expect(colWidths(wrapper)).toEqual(['width: 600px;', 'width: 140px;', 'width: 200px;']) // 整表最小宽度 = 三列之和,窄屏放不下的时候表格自己横向滚 expect(wrapper.find('table').attributes('style')).toContain('min-width: 940px') }) it('勾选列也算进最小宽度', async () => { const originalWidth = window.innerWidth Object.defineProperty(window, 'innerWidth', { configurable: true, writable: true, value: 1440 }) const wrapper = open({ selectable: true, selectedKeys: [] }) await drag(wrapper, 'title', 200) expect(wrapper.findAll('colgroup col')).toHaveLength(4) expect(wrapper.find('table').attributes('style')).toContain('min-width: 996px') Object.defineProperty(window, 'innerWidth', { configurable: true, writable: true, value: originalWidth }) }) it('操作列没有把手:那一列的宽度跟着按钮走', () => { expect(open().find('[data-test="resize-col-actions"]').exists()).toBe(false) }) it('双击把手回到浏览器自己排的那一版,别列拖过的留着', async () => { const wrapper = open() await drag(wrapper, 'title', 200) await drag(wrapper, 'status', 60) handle(wrapper, 'title').element.dispatchEvent(new MouseEvent('dblclick', { bubbles: true })) await wrapper.vm.$nextTick() expect(colWidths(wrapper)).toEqual(['width: 400px;', 'width: 200px;', 'width: 200px;']) }) it('拖不出列上的上下界', async () => { const wrapper = open({ columns: [{ key: 'title', label: '标题', minWidth: 300, maxWidth: 700 }, COLUMNS[1], COLUMNS[2]] }) await drag(wrapper, 'title', 9999) expect(colWidths(wrapper)[0]).toBe('width: 700px;') await drag(wrapper, 'title', -9999) expect(colWidths(wrapper)[0]).toBe('width: 300px;') }) }) describe('记本地', () => { it('拖过一次就存进这一屏那本账,重开这一屏不必重拖', async () => { const wrapper = open({ columnWidthsKey: '/done' }) await drag(wrapper, 'title', 200) expect(JSON.parse(localStorage.getItem('unisoa_col_widths_/done') ?? '{}')).toHaveProperty(SHAPE) wrapper.unmount() const next = open({ columnWidthsKey: '/done' }) await next.vm.$nextTick() expect(colWidths(next)).toEqual(['width: 600px;', 'width: 140px;', 'width: 200px;']) }) it('没钥匙就不写本地(弹窗里的表、明细表那些)', async () => { const wrapper = open({ columnWidthsKey: '' }) await drag(wrapper, 'title', 200) expect(Object.keys(localStorage)).toEqual([]) // 这一屏仍然当场生效,只是不记 expect(colWidths(wrapper)[0]).toBe('width: 600px;') }) })