142 lines
6.9 KiB
TypeScript
142 lines
6.9 KiB
TypeScript
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 { createPlatformFeeItem, listPlatformFeeItems, updatePlatformFeeItem } 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').replace(/\r\n/g, '\n')
|
|
|
|
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', () => {
|
|
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 }))
|
|
})
|
|
|
|
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.get('.data-table__toolbar-start h1').text()).toBe('费用项目')
|
|
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<string, string>): 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('.data-table__notices').exists()).toBe(true)
|
|
expect(wrapper.find('.el-table').exists()).toBe(false)
|
|
expect(wrapper.text()).toContain('费用项目加载失败')
|
|
expect(wrapper.text()).not.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', () => {
|
|
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/)
|
|
})
|
|
it('keeps the prototype table treatment and mobile form fallback without inventing a pricing mode', () => {
|
|
expect(source).toContain('appearance="integrated" :stripe="false"')
|
|
expect(source).toContain(':page-sizes="[20, 50, 100]"')
|
|
expect(source).toContain('@media (max-width: 720px)')
|
|
expect(source).toContain('.direction-grid, .form-grid.two-cols { grid-template-columns: 1fr; }')
|
|
expect(source).not.toContain('pricingMode')
|
|
expect(source).not.toContain('计价模式')
|
|
expect(source).not.toContain('固定费用')
|
|
for (const label of ['创建人', '创建日期', '更新人', '更新日期']) expect(source).toContain(`label: '${label}'`)
|
|
})
|
|
})
|