Files
admin-web/src/modules/fee-items/pages/FeeItemsView.test.ts
T

145 lines
7.0 KiB
TypeScript
Raw Normal View History

2026-08-10 16:24:54 +08:00
import { readFileSync } from 'node:fs'
import { join } from 'node:path'
2026-08-10 15:36:53 +08:00
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'
2026-10-19 17:56:31 +08:00
import { createPlatformFeeItem, listPlatformFeeItems, updatePlatformFeeItem } from '@/modules/fee-items/api/platformFeeItems'
2026-08-10 15:36:53 +08:00
import FeeItemsView from './FeeItemsView.vue'
const source = readFileSync(join(process.cwd(), 'src/modules/fee-items/pages/FeeItemsView.vue'), 'utf8').replace(/\r\n/g, '\n')
2026-08-10 16:24:54 +08:00
2026-08-10 15:36:53 +08:00
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<string, string>()
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', () => {
2026-10-19 17:56:31 +08:00
it('saves configured and zero prices and clears the default on edit', async () => {
const wrapper = await mountView(['firstleg:fee-item:view', 'firstleg:fee-item:create', 'firstleg:fee-item:edit'])
const state = (wrapper.vm.$ as unknown as { setupState: {
openCreate(): void; saveFeeItem(): Promise<void>
form: { id: string; category: string; feeTypeName: string; defaultPrice: number | null }
} }).setupState
state.openCreate(); await flushPromises()
Object.assign(state.form, { category: '操作费', feeTypeName: '加封费', defaultPrice: 60 })
await state.saveFeeItem(); await flushPromises()
expect(createPlatformFeeItem).toHaveBeenLastCalledWith(expect.objectContaining({ defaultPrice: 60 }))
state.openCreate(); await flushPromises()
Object.assign(state.form, { category: '操作费', feeTypeName: '零元费', defaultPrice: 0 })
await state.saveFeeItem(); await flushPromises()
expect(createPlatformFeeItem).toHaveBeenLastCalledWith(expect.objectContaining({ defaultPrice: 0 }))
state.openCreate(); await flushPromises()
Object.assign(state.form, { id: '1', category: '操作费', feeTypeName: '加封费', defaultPrice: null })
await state.saveFeeItem(); await flushPromises()
expect(updatePlatformFeeItem).toHaveBeenCalledWith('1', expect.objectContaining({ defaultPrice: null }))
})
2026-08-10 15:36:53 +08:00
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)
2026-08-10 16:24:54 +08:00
expect(wrapper.find('.query-toolbar__actions').text()).not.toContain('新增费用项目')
expect(wrapper.find('.data-table__business-actions').text()).toContain('新增费用项目')
2026-09-30 14:01:58 +08:00
expect(wrapper.get('.data-table__toolbar-start h1').text()).toBe('费用项目')
2026-08-10 16:24:54 +08:00
expect(wrapper.find('.data-table__pagination-left').text()).toContain('平台公共主数据 · 共 0 项')
expect(wrapper.find('.list-page-layout__state').exists()).toBe(false)
2026-08-10 15:36:53 +08:00
expect(wrapper.text()).toContain('平台公共主数据')
expect(listPlatformFeeItems).toHaveBeenCalledWith(expect.objectContaining({ page: 1, size: 20 }))
const vm = wrapper.vm as unknown as { applyFilters(model: Record<string, string>): void }
vm.applyFilters({ keyword: '', category: '', tradeScope: '', status: '' })
await flushPromises()
expect(listPlatformFeeItems).toHaveBeenCalledWith(expect.objectContaining({ page: 1, size: 20 }))
})
2026-08-10 16:24:54 +08:00
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'])
2026-09-30 14:01:58 +08:00
expect(wrapper.find('.data-table__notices').exists()).toBe(true)
expect(wrapper.find('.el-table').exists()).toBe(false)
2026-08-10 16:24:54 +08:00
expect(wrapper.text()).toContain('费用项目加载失败')
2026-08-12 12:35:32 +08:00
expect(wrapper.text()).not.toContain('Gateway unavailable')
2026-08-10 16:24:54 +08:00
})
2026-08-10 15:36:53 +08:00
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()
})
2026-08-10 16:24:54 +08:00
it('keeps all three row actions visible without a premature more menu', () => {
2026-09-15 09:58:15 +08:00
const actions = source.slice(source.indexOf('<template #actions='), source.indexOf('</template>', source.indexOf('<template #actions=')))
expect(actions.match(/class="density-action"/g)).toHaveLength(3)
expect(actions).toContain('>编辑</el-button>')
expect(actions).toContain('>删除</el-button>')
expect(actions).toContain('data-tone="danger"')
expect(actions).toContain(':data-tone=')
expect(actions).not.toMatch(/<(?:el-dropdown|Pencil|Power|Eye|Trash2|Download|CircleX)\b/)
2026-08-10 16:24:54 +08:00
})
2026-08-12 18:00:26 +08:00
it('keeps the prototype table treatment and mobile form fallback without inventing a pricing mode', () => {
2026-10-21 13:22:42 +08:00
expect(source).toContain('appearance="integrated"')
expect(source).not.toContain(':stripe="false"')
expect(source).not.toContain(':stripe="false"')
2026-09-30 14:01:58 +08:00
expect(source).toContain(':page-sizes="[20, 50, 100]"')
2026-08-12 18:00:26 +08:00
expect(source).toContain('@media (max-width: 720px)')
expect(source).toContain('.fee-item-editor-table tr { display: grid; }')
expect(source).toContain('class="density-table density-table--editable fee-item-editor-table"')
2026-08-12 18:00:26 +08:00
expect(source).not.toContain('pricingMode')
expect(source).not.toContain('计价模式')
expect(source).not.toContain('固定费用')
2026-08-17 19:52:04 +08:00
for (const label of ['创建人', '创建日期', '更新人', '更新日期']) expect(source).toContain(`label: '${label}'`)
2026-08-12 18:00:26 +08:00
})
2026-08-10 15:36:53 +08:00
})