Files
admin-web/src/modules/master-data/express-companies/pages/ExpressCompaniesView.test.ts
T

131 lines
5.3 KiB
TypeScript
Raw Normal View History

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'
2026-08-03 18:49:18 +08:00
import { listPlatformExpressCompanies, type PlatformExpressCompanyResource } from '@/modules/master-data/express-companies/api/platformExpressCompanies'
import { useAccessPreviewStore } from '@/core/access/accessPreview'
import { useAuthStore } from '@/core/auth/auth'
2026-08-04 21:01:39 +08:00
import QueryToolbar from '@/shared/components/QueryToolbar.vue'
2026-08-04 09:05:19 +08:00
import { listPlatformTenants, type PlatformTenantResource } from '../../../iam/api/platformIam'
import ExpressCompaniesView from './ExpressCompaniesView.vue'
2026-08-03 18:49:18 +08:00
vi.mock('@/modules/master-data/express-companies/api/platformExpressCompanies', () => ({
listPlatformExpressCompanies: vi.fn(),
createPlatformExpressCompany: vi.fn(),
updatePlatformExpressCompany: vi.fn(),
changePlatformExpressCompanyStatus: vi.fn(),
}))
2026-08-04 09:05:19 +08:00
vi.mock('../../../iam/api/platformIam', () => ({ listPlatformTenants: 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),
}
}
const company: PlatformExpressCompanyResource = {
id: '19001',
tenantId: 10001,
companyCode: 'SF',
companyName: '顺丰速运',
servicePhone: '95338',
website: 'https://www.sf-express.com',
trackingUrlTemplate: 'https://example.com/track/{trackingNo}',
status: 'ACTIVE',
remark: '测试数据',
updatedAt: '2026-07-31T10:20:00',
}
2026-08-04 09:05:19 +08:00
const tenant = (id: number | string, tenantName: string): PlatformTenantResource => ({
id, tenantCode: String(id), tenantName, status: 'ACTIVE', legalName: tenantName,
legalRepresentative: null, unifiedSocialCreditCode: null, contactName: null,
contactPhone: null, contactEmail: null, servicePackage: null, contactWechat: null,
salesOwnerName: null, paymentTermDays: null, enterpriseType: null, establishedDate: null,
registeredCapital: null, registeredCapitalCurrency: null, registeredAddress: null,
internalRemark: null, createdAt: null, approvedAt: null,
})
const snowflakeTenantId = '9223372036854775806'
async function mountView() {
vi.stubGlobal('localStorage', createMemoryStorage())
vi.stubGlobal('sessionStorage', createMemoryStorage())
const pinia = createPinia()
setActivePinia(pinia)
const auth = useAuthStore()
auth.token = 'token'
auth.permissions = ['platform:express:list', 'platform:express:create', 'platform:express:edit', 'platform:express:status']
useAccessPreviewStore().initializeAfterLogin()
const wrapper = mount(ExpressCompaniesView, {
attachTo: document.body,
global: { plugins: [pinia, ElementPlus] },
})
await flushPromises()
return wrapper
}
enableAutoUnmount(afterEach)
afterAll(() => vi.unstubAllGlobals())
describe('ExpressCompaniesView query and table migration', () => {
beforeEach(() => {
vi.stubGlobal('ResizeObserver', ResizeObserverStub)
document.body.innerHTML = ''
2026-08-04 09:05:19 +08:00
vi.mocked(listPlatformTenants).mockReset().mockResolvedValue({
records: [tenant(10001, '默认企业'), tenant(snowflakeTenantId, '雪花企业')], page: 1, size: 200, total: 2, pages: 1,
})
vi.mocked(listPlatformExpressCompanies).mockReset().mockResolvedValue({
records: [company], page: 1, size: 20, total: 1, pages: 1,
})
})
it('renders the shared toolbar and data table without a summary strip', async () => {
const wrapper = await mountView()
expect(wrapper.find('.query-toolbar').exists()).toBe(true)
2026-08-04 21:01:39 +08:00
expect(wrapper.find('input[aria-label="所属企业"]').exists()).toBe(true)
expect(wrapper.find('.data-table').exists()).toBe(true)
expect(wrapper.text()).toContain('顺丰速运')
2026-08-04 21:01:39 +08:00
expect(wrapper.text()).toContain('全部企业 · 共 1 家快递公司')
expect(wrapper.find('.metric-strip').exists()).toBe(false)
2026-08-04 21:01:39 +08:00
expect(listPlatformExpressCompanies).toHaveBeenCalledWith(undefined, 1, 20, '', '')
})
it('does not request with edited filters until search is submitted', async () => {
const wrapper = await mountView()
await wrapper.find('input[placeholder="快递公司名称或编码"]').setValue('顺丰')
await flushPromises()
expect(listPlatformExpressCompanies).toHaveBeenCalledTimes(1)
const searchButton = wrapper.findAll('button').find((button) => button.text().trim() === '查询')
await searchButton?.trigger('click')
await flushPromises()
2026-08-04 21:01:39 +08:00
expect(listPlatformExpressCompanies).toHaveBeenLastCalledWith(undefined, 1, 20, '顺丰', '')
})
2026-08-04 09:05:19 +08:00
it('preserves a snowflake tenant id as a string when switching enterprises', async () => {
const wrapper = await mountView()
2026-08-04 21:01:39 +08:00
const toolbar = wrapper.findComponent(QueryToolbar)
const model = { tenantId: snowflakeTenantId, keyword: '', status: '' }
toolbar.vm.$emit('update:modelValue', model)
toolbar.vm.$emit('search', model)
2026-08-04 09:05:19 +08:00
await flushPromises()
expect(listPlatformExpressCompanies).toHaveBeenLastCalledWith(snowflakeTenantId, 1, 20, '', '')
})
})