import { readFileSync } from 'node:fs' import { join } from 'node:path' import ElementPlus from 'element-plus' import { createPinia, setActivePinia } from 'pinia' import { enableAutoUnmount, flushPromises, mount } from '@vue/test-utils' import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { useAccessPreviewStore } from '@/core/access/accessPreview' import { useAuthStore } from '@/core/auth/auth' import { listPlatformFeeItems } from '@/modules/fee-items/api/platformFeeItems' import FeeItemsView from './FeeItemsView.vue' const source = readFileSync(join(process.cwd(), 'src/modules/fee-items/pages/FeeItemsView.vue'), 'utf8') vi.mock('@/modules/fee-items/api/platformFeeItems', () => ({ listPlatformFeeItems: vi.fn(), getPlatformFeeItem: vi.fn(), createPlatformFeeItem: vi.fn(), updatePlatformFeeItem: vi.fn(), changePlatformFeeItemStatus: vi.fn(), deletePlatformFeeItem: vi.fn(), })) class ResizeObserverStub implements ResizeObserver { observe() {} unobserve() {} disconnect() {} } function createMemoryStorage(): Storage { const values = new Map() return { get length() { return values.size }, clear: () => values.clear(), getItem: (key) => values.get(key) ?? null, key: (index) => [...values.keys()][index] ?? null, removeItem: (key) => values.delete(key), setItem: (key, value) => values.set(key, value), } } async function mountView(permissions: string[]) { const pinia = createPinia() setActivePinia(pinia) const auth = useAuthStore() auth.token = 'token' auth.permissions = permissions useAccessPreviewStore().initializeAfterLogin() const wrapper = mount(FeeItemsView, { attachTo: document.body, global: { plugins: [pinia, ElementPlus] } }) await flushPromises() return wrapper } enableAutoUnmount(afterEach) afterAll(() => vi.unstubAllGlobals()) describe('FeeItemsView public master data and access', () => { beforeEach(() => { vi.stubGlobal('ResizeObserver', ResizeObserverStub) vi.stubGlobal('localStorage', createMemoryStorage()) vi.stubGlobal('sessionStorage', createMemoryStorage()) document.body.innerHTML = '' vi.mocked(listPlatformFeeItems).mockReset().mockResolvedValue({ records: [], page: 1, size: 20, total: 0, pages: 0 }) }) it('loads public fee items immediately without customer selection', async () => { const wrapper = await mountView(['firstleg:fee-item:view', 'firstleg:fee-item:create']) expect(wrapper.find('.query-toolbar').exists()).toBe(true) expect(wrapper.find('.data-table').exists()).toBe(true) expect(wrapper.find('.query-toolbar__actions').text()).not.toContain('新增费用项目') expect(wrapper.find('.data-table__business-actions').text()).toContain('新增费用项目') expect(wrapper.find('.data-table__toolbar-start').exists()).toBe(false) expect(wrapper.find('.data-table__pagination-left').text()).toContain('平台公共主数据 · 共 0 项') expect(wrapper.find('.list-page-layout__state').exists()).toBe(false) expect(wrapper.text()).toContain('平台公共主数据') expect(listPlatformFeeItems).toHaveBeenCalledWith(expect.objectContaining({ page: 1, size: 20 })) const vm = wrapper.vm as unknown as { applyFilters(model: Record): void } vm.applyFilters({ keyword: '', category: '', tradeScope: '', status: '' }) await flushPromises() expect(listPlatformFeeItems).toHaveBeenCalledWith(expect.objectContaining({ page: 1, size: 20 })) }) it('renders the load error instead of an empty table panel', async () => { vi.mocked(listPlatformFeeItems).mockRejectedValueOnce(new Error('Gateway unavailable')) const wrapper = await mountView(['firstleg:fee-item:view']) expect(wrapper.find('.list-page-layout__state').exists()).toBe(true) expect(wrapper.find('.table-panel').exists()).toBe(false) expect(wrapper.text()).toContain('费用项目加载失败') expect(wrapper.text()).toContain('Gateway unavailable') }) it('renders an explicit no-access state without loading customers or fee items', async () => { const wrapper = await mountView([]) expect(wrapper.text()).toContain('无权查看费用项目') expect(listPlatformFeeItems).not.toHaveBeenCalled() }) it('keeps all three row actions visible without a premature more menu', () => { expect(source).toContain('编辑') expect(source).toContain('') expect(source).toContain('删除') expect(source).not.toContain('MoreHorizontal') }) })