77 lines
2.2 KiB
JavaScript
77 lines
2.2 KiB
JavaScript
import { createPinia, setActivePinia } from 'pinia'
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import { materialsApi } from '../api/materials'
|
|
import { useMaterialsStore } from './materials'
|
|
|
|
vi.mock('../api/materials', () => ({
|
|
materialsApi: {
|
|
getAll: vi.fn()
|
|
}
|
|
}))
|
|
|
|
describe('materials store selection options', () => {
|
|
beforeEach(() => {
|
|
setActivePinia(createPinia())
|
|
vi.clearAllMocks()
|
|
})
|
|
|
|
it('loads every page so a selector can find materials outside the first page', async () => {
|
|
const firstPage = Array.from({ length: 100 }, (_, index) => ({
|
|
id: 120 - index,
|
|
material_code: String(120 - index).padStart(3, '0'),
|
|
name: `物料 ${120 - index}`
|
|
}))
|
|
materialsApi.getAll
|
|
.mockResolvedValueOnce({
|
|
data: {
|
|
items: firstPage,
|
|
limit: 100,
|
|
offset: 0,
|
|
total: 101,
|
|
has_more: true
|
|
}
|
|
})
|
|
.mockResolvedValueOnce({
|
|
data: {
|
|
items: [{ id: 1, material_code: '001', name: '早期物料' }],
|
|
limit: 100,
|
|
offset: 100,
|
|
total: 101,
|
|
has_more: false
|
|
}
|
|
})
|
|
|
|
const store = useMaterialsStore()
|
|
await store.fetchMaterialOptions()
|
|
|
|
expect(store.materialOptions).toHaveLength(101)
|
|
expect(store.materialOptions.at(-1)).toEqual({
|
|
id: 1,
|
|
material_code: '001',
|
|
name: '早期物料'
|
|
})
|
|
})
|
|
|
|
it('loads obsolete materials for historical record selectors only', async () => {
|
|
materialsApi.getAll.mockResolvedValue({
|
|
data: {
|
|
items: [
|
|
{ id: 1, material_code: 'A-001', name: '有效物料', status: 'active' },
|
|
{ id: 2, material_code: 'B-002', name: '历史物料', status: 'obsolete' }
|
|
],
|
|
total: 2
|
|
}
|
|
})
|
|
|
|
const store = useMaterialsStore()
|
|
await store.fetchHistoricalMaterialOptions()
|
|
|
|
expect(materialsApi.getAll).toHaveBeenCalledWith({ status: 'all', limit: 100, offset: 0 })
|
|
expect(store.historicalMaterialOptions).toEqual([
|
|
{ id: 1, material_code: 'A-001', name: '有效物料', status: 'active' },
|
|
{ id: 2, material_code: 'B-002', name: '历史物料', status: 'obsolete' }
|
|
])
|
|
expect(store.materialOptions).toEqual([])
|
|
})
|
|
})
|