后端 GetMyApplications / GetAllApplications 只读 limit/offset,前端原先发 page/pageSize,无法识别的参数被静默忽略并退回默认的 limit=20 offset=0, 于是翻页不报错但始终返回第一页。 转换点固定在 api/application.js,对外签名 (status, page, pageSize) 不变, stores/application.js 与 stores/approval.js 两个调用点无需改动;同时对非法 页码兜底,避免算出负 offset。 application.test.js 覆盖换算结果与边界,其中「第 2 页不再是 offset=0」 是本次的回归点。
48 lines
1.4 KiB
JavaScript
48 lines
1.4 KiB
JavaScript
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||
import { applicationApi } from './application'
|
||
|
||
const request = vi.hoisted(() => ({
|
||
get: vi.fn()
|
||
}))
|
||
|
||
vi.mock('./index', () => ({ default: request }))
|
||
|
||
describe('application API 分页参数', () => {
|
||
beforeEach(() => {
|
||
vi.clearAllMocks()
|
||
request.get.mockResolvedValue({ data: { items: [], total: 0 } })
|
||
})
|
||
|
||
it('把页码换算成后端认识的 limit/offset,第 2 页不再是 offset=0', async () => {
|
||
await applicationApi.getMy('', 2, 20)
|
||
|
||
expect(request.get).toHaveBeenCalledWith('/applications/my', {
|
||
params: { status: '', limit: 20, offset: 20 }
|
||
})
|
||
})
|
||
|
||
it('审批页的 getAll 同样换算,且不落下 status 筛选', async () => {
|
||
await applicationApi.getAll('pending', 3, 10)
|
||
|
||
expect(request.get).toHaveBeenCalledWith('/applications', {
|
||
params: { status: 'pending', limit: 10, offset: 20 }
|
||
})
|
||
})
|
||
|
||
it('第 1 页从 offset=0 开始', async () => {
|
||
await applicationApi.getAll('', 1, 20)
|
||
|
||
expect(request.get).toHaveBeenCalledWith('/applications', {
|
||
params: { status: '', limit: 20, offset: 0 }
|
||
})
|
||
})
|
||
|
||
it('页码或每页条数非法时退回第 1 页的默认值,不产生负 offset', async () => {
|
||
await applicationApi.getMy('', 0, 0)
|
||
|
||
expect(request.get).toHaveBeenCalledWith('/applications/my', {
|
||
params: { status: '', limit: 20, offset: 0 }
|
||
})
|
||
})
|
||
})
|