You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
2.1 KiB

import { fetchAllForExport } from '@/utils/exportList'
describe('fetchAllForExport', () => {
it('exports every matching page using the server pagination without changing the table filters', async() => {
const filters = { page: 4, page_size: 20, keyword: '环境', user_name: '张三' }
const fetchPage = jest.fn()
.mockResolvedValueOnce({ total: 3, data: [{ id: 3 }, { id: 2 }] })
.mockResolvedValueOnce({ total: 3, data: [{ id: 1 }] })
expect(await fetchAllForExport(fetchPage, filters)).toEqual([{ id: 3 }, { id: 2 }, { id: 1 }])
expect(fetchPage.mock.calls.map(call => call[0])).toEqual([
{ page: 1, page_size: 500, keyword: '环境', user_name: '张三' },
{ page: 2, page_size: 500, keyword: '环境', user_name: '张三' }
])
expect(filters.page).toBe(4)
expect(filters.page_size).toBe(20)
})
it('returns an empty list for no matches', async() => {
expect(await fetchAllForExport(jest.fn().mockResolvedValue({ total: 0, data: [] }), {})).toEqual([])
})
it('rejects failed requests instead of returning a partial export', async() => {
const fetchPage = jest.fn()
.mockResolvedValueOnce({ total: 2, data: [{ id: 2 }] })
.mockRejectedValueOnce(new Error('network failure'))
await expect(fetchAllForExport(fetchPage, {})).rejects.toThrow('network failure')
})
it('rejects incomplete or malformed responses', async() => {
const fetchPage = jest.fn()
.mockResolvedValueOnce({ total: 2, data: [{ id: 2 }] })
.mockResolvedValueOnce({ total: 2, data: [] })
await expect(fetchAllForExport(fetchPage, {})).rejects.toThrow('导出数据不完整')
await expect(fetchAllForExport(jest.fn().mockResolvedValue({}), {})).rejects.toThrow('导出数据格式异常')
})
it('rejects a changed total during export', async() => {
const fetchPage = jest.fn()
.mockResolvedValueOnce({ total: 2, data: [{ id: 2 }] })
.mockResolvedValueOnce({ total: 3, data: [{ id: 1 }] })
await expect(fetchAllForExport(fetchPage, {})).rejects.toThrow('导出期间数据发生变化')
})
})