Files
photo-live/apps/admin/src/views/system/users/composables/useAdminUserPages.spec.ts
T
2026-09-22 18:10:53 +08:00

54 lines
1.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, expect, it, vi } from 'vitest'
import { useAdminUserPages } from './useAdminUserPages'
const pageOf = (accounts: string[], total: number) => ({
items: accounts.map((account, i) => ({ id: `u${i}`, account })) as never,
nextCursor: null,
hasMore: false,
total
})
describe('useAdminUserPages 分页状态', () => {
it('loadPage 推进页码并落 items/total(透传 page/pageSize 给 fetch)', async () => {
const fetch = vi.fn().mockResolvedValue(pageOf(['a', 'b'], 35))
const state = useAdminUserPages(fetch)
await state.loadPage(2)
expect(fetch).toHaveBeenCalledWith(2, 20)
expect(state.page.value).toBe(2)
expect(state.items.value).toHaveLength(2)
expect(state.total.value).toBe(35)
})
it('total 缺省(旧响应形态)回退 items 长度,不抛错', async () => {
const fetch = vi.fn().mockResolvedValue({
items: [],
nextCursor: null,
hasMore: false
})
const state = useAdminUserPages(fetch)
await state.loadPage()
expect(state.total.value).toBe(0)
})
it('fetch 抛错 → error 捕获、loading 复位、页码不推进', async () => {
const fetch = vi.fn().mockRejectedValue(new Error('boom'))
const state = useAdminUserPages(fetch)
await state.loadPage(3)
expect(state.error.value?.message).toBe('boom')
expect(state.loading.value).toBe(false)
expect(state.page.value).toBe(1)
})
it('onSizeChange 回第 1 页;replaceItem 原地替换当前页条目', async () => {
const fetch = vi.fn().mockResolvedValue(pageOf(['a'], 5))
const state = useAdminUserPages(fetch)
await state.loadPage(2)
state.onSizeChange()
await Promise.resolve()
await Promise.resolve()
expect(fetch).toHaveBeenLastCalledWith(1, 20)
state.replaceItem(0, { id: 'u0', account: 'a2' } as never)
expect(state.items.value[0]?.account).toBe('a2')
})
})