feat(admin): standard pagination + synced profile columns on users list
Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
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')
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user