master
parent
c1052046fd
commit
79f4e138f3
@ -0,0 +1,22 @@
|
||||
// 使用后端实际返回的分页信息,避免固定大页数造成遗漏。
|
||||
export async function fetchAllForExport(fetchPage, filters) {
|
||||
const params = { ...filters, page: 1, page_size: 500 }
|
||||
const rows = []
|
||||
let total
|
||||
while (total === undefined || rows.length < total) {
|
||||
const result = await fetchPage({ ...params })
|
||||
if (!result || !Array.isArray(result.data) || !Number.isFinite(Number(result.total))) {
|
||||
throw new Error('导出数据格式异常')
|
||||
}
|
||||
if (total === undefined) total = Number(result.total)
|
||||
if (Number(result.total) !== total) {
|
||||
throw new Error('导出期间数据发生变化,请重试')
|
||||
}
|
||||
rows.push(...result.data)
|
||||
if ((!result.data.length && rows.length < total) || rows.length > total) {
|
||||
throw new Error('导出数据不完整,请重试')
|
||||
}
|
||||
params.page += 1
|
||||
}
|
||||
return rows
|
||||
}
|
||||
@ -0,0 +1,43 @@
|
||||
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('导出期间数据发生变化')
|
||||
})
|
||||
})
|
||||
Loading…
Reference in new issue