25 lines
1.9 KiB
TypeScript
25 lines
1.9 KiB
TypeScript
import { beforeEach, expect, it, vi } from 'vitest'
|
|||
|
|
import { useOwnerDirectory } from './useOwnerDirectory'
|
||
|
|
import type { QueryContext } from './types'
|
||
|
|
const api = vi.hoisted(() => ({ ownerDirectory: vi.fn() }))
|
||
|
|
vi.mock('./api/analytics', () => ({ analyticsApi: api }))
|
||
|
|
const context = { queryAsOf: '2026-10-14T08:00:00Z', salesRunId: '11', inventoryRunId: '12' } as QueryContext
|
||
|
|
beforeEach(() => vi.clearAllMocks())
|
||
|
|
it('uses fixed local context and names while preserving code identity', async () => {
|
||
|
|
api.ownerDirectory.mockResolvedValue([{ customerCode: 'A', customerName: '客户名称' }, { customerCode: 'B', customerName: null }])
|
||
|
|
const directory = useOwnerDirectory(); await directory.load(context, 'W')
|
||
|
|
expect(directory.options.value).toEqual([{ value: 'A', label: '客户名称 (A)' }, { value: 'B', label: 'B' }])
|
||
|
|
expect(api.ownerDirectory.mock.calls[0]![0]).toMatchObject({ warehouseCode: 'W', queryAsOf: context.queryAsOf, salesRunId: '11' })
|
||
|
|
await directory.load(context, 'W'); expect(api.ownerDirectory).toHaveBeenCalledTimes(1)
|
||
|
|
})
|
||
|
|
it('ignores an older response and offers retry after a failed read', async () => {
|
||
|
|
let resolve!: (value: unknown) => void
|
||
|
|
api.ownerDirectory.mockReturnValueOnce(new Promise(r => { resolve = r })).mockResolvedValueOnce([{ customerCode: 'B', customerName: '新名称' }])
|
||
|
|
const directory = useOwnerDirectory(), old = directory.load(context, 'A')
|
||
|
|
await directory.load(context, 'B'); resolve([{ customerCode: 'A', customerName: '旧名称' }]); await old
|
||
|
|
expect(directory.options.value[0]?.value).toBe('B')
|
||
|
|
api.ownerDirectory.mockRejectedValueOnce(new Error('offline')); await directory.load(context, 'C')
|
||
|
|
expect(directory.options.value).toEqual([]); expect(directory.error.value).toContain('读取失败')
|
||
|
|
api.ownerDirectory.mockResolvedValueOnce([]); await directory.load(context, 'C'); expect(directory.error.value).toBe('')
|
||
|
|
})
|