task-44(账号/权限页面): 实现用户分页边界行为
新增 user-pagination.ts 纯函数:totalPageCount(空数据=0、非整倍向上取整)、 clampPage(<1→1、超末页→末页、空态留第1页)、isPageOutOfRange;UsersPage 加载后若总页数收缩致当前页越界则回钳末页重新拉取一次。8 用例全过,315 单测 + build 绿。
This commit is contained in:
@@ -5,6 +5,7 @@ import { http } from '@/api/http'
|
||||
import type { AdminUser } from '@/types/admin'
|
||||
import { fetchUserList } from '@/api/users'
|
||||
import { createUserFilterState, toUserListParams } from './users-filter'
|
||||
import { totalPageCount } from './user-pagination'
|
||||
|
||||
const loading = ref(false)
|
||||
const rows = ref<AdminUser[]>([])
|
||||
@@ -17,6 +18,14 @@ async function loadUsers() {
|
||||
const page = await fetchUserList(toUserListParams(filters))
|
||||
rows.value = page.items
|
||||
total.value = page.total
|
||||
// 数据被删/筛选后总页数收缩:页码越界时回钳到末页并重新加载一次。
|
||||
const last = totalPageCount(page.total, filters.pageSize)
|
||||
if (last > 0 && filters.page > last) {
|
||||
filters.page = last
|
||||
const retried = await fetchUserList(toUserListParams(filters))
|
||||
rows.value = retried.items
|
||||
total.value = retried.total
|
||||
}
|
||||
} catch (error) {
|
||||
ElMessage.error(error instanceof Error ? error.message : '用户列表加载失败')
|
||||
} finally {
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
/** 用户列表分页边界(任务 44):纯函数,处理总页数、越界页码回钳与空数据。 */
|
||||
import { USER_PAGE_DEFAULT_SIZE } from '../../api/users-dto.ts'
|
||||
|
||||
/** 总页数;无数据返回 0,pageSize 非法时用默认。 */
|
||||
export function totalPageCount(total: number, pageSize: number): number {
|
||||
if (!Number.isFinite(total) || total <= 0) return 0
|
||||
const size = Number.isFinite(pageSize) && pageSize >= 1 ? Math.floor(pageSize) : USER_PAGE_DEFAULT_SIZE
|
||||
return Math.ceil(total / size)
|
||||
}
|
||||
|
||||
/** 请求页是否超过末页(无数据时不算越界)。 */
|
||||
export function isPageOutOfRange(page: number, total: number, pageSize: number): boolean {
|
||||
const requested = Number.isFinite(page) ? Math.floor(page) : 1
|
||||
if (requested < 1) return false
|
||||
const last = totalPageCount(total, pageSize)
|
||||
return last > 0 && requested > last
|
||||
}
|
||||
|
||||
/** 把请求页钳到合法范围:<1 回首页,超过末页回末页;无数据允许第 1 页空态。 */
|
||||
export function clampPage(page: number, total: number, pageSize: number): number {
|
||||
const requested = Number.isFinite(page) ? Math.floor(page) : 1
|
||||
if (requested < 1) return 1
|
||||
const last = totalPageCount(total, pageSize)
|
||||
if (last === 0) return 1
|
||||
return Math.min(requested, last)
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
import test from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { readSource } from './helpers.ts'
|
||||
import { clampPage, isPageOutOfRange, totalPageCount } from '../src/pages/account/user-pagination.ts'
|
||||
|
||||
test('test_task_044_user_pagination_normal_primary_path', () => {
|
||||
// 正常主路径:30 条 / 每页 15 → 2 页。
|
||||
assert.equal(totalPageCount(30, 15), 2)
|
||||
})
|
||||
|
||||
test('test_task_044_user_pagination_normal_variant_input', () => {
|
||||
// 正常变体:非整倍总数向上取整;最后一页在界内。
|
||||
assert.equal(totalPageCount(31, 15), 3)
|
||||
assert.equal(clampPage(2, 31, 15), 2)
|
||||
})
|
||||
|
||||
test('test_task_044_user_pagination_normal_repeated_operation_is_idempotent', () => {
|
||||
// 正常重复:纯函数结果稳定。
|
||||
assert.equal(totalPageCount(50, 10), totalPageCount(50, 10))
|
||||
assert.equal(clampPage(9, 50, 10), 5)
|
||||
})
|
||||
|
||||
test('test_task_044_user_pagination_boundary_empty_input', () => {
|
||||
// 边界空值:无数据时总页数为 0,仍允许第 1 页(空态)。
|
||||
assert.equal(totalPageCount(0, 15), 0)
|
||||
assert.equal(clampPage(1, 0, 15), 1)
|
||||
assert.equal(clampPage(3, 0, 15), 1)
|
||||
})
|
||||
|
||||
test('test_task_044_user_pagination_boundary_single_item', () => {
|
||||
// 边界单元素:1 条 / 每页 15 → 1 页。
|
||||
assert.equal(totalPageCount(1, 15), 1)
|
||||
assert.equal(clampPage(5, 1, 15), 1)
|
||||
})
|
||||
|
||||
test('test_task_044_user_pagination_boundary_limit_or_missing_field', () => {
|
||||
// 边界上限/越界请求:请求页超过末页被钳到末页。
|
||||
assert.equal(clampPage(99, 30, 15), 2)
|
||||
assert.equal(isPageOutOfRange(3, 30, 15), true)
|
||||
assert.equal(isPageOutOfRange(2, 30, 15), false)
|
||||
})
|
||||
|
||||
test('test_task_044_user_pagination_invalid_input_rejected', () => {
|
||||
// 异常输入:非法数字/负页不抛错,按首页或末页安全处理。
|
||||
assert.equal(clampPage(-5, 30, 15), 1)
|
||||
assert.equal(clampPage(Number.NaN, 30, 15), 1)
|
||||
assert.equal(isPageOutOfRange(Number.NaN, 30, 15), false)
|
||||
})
|
||||
|
||||
test('test_task_044_user_pagination_dependency_failure_returns_actionable_message', () => {
|
||||
// 依赖失败:删除/加载后若页码越界,页面按末页重新加载,不落在空列表。
|
||||
const page = readSource('src/pages/account/UsersPage.vue')
|
||||
assert.match(page, /user-pagination/, '页面复用分页边界纯函数')
|
||||
assert.match(page, /filters\.page\s*=\s*last|totalPageCount/, '加载后对越界页码回钳')
|
||||
const mod = readSource('src/pages/account/user-pagination.ts')
|
||||
assert.match(mod, /export function totalPageCount/)
|
||||
assert.match(mod, /export function clampPage/)
|
||||
})
|
||||
Reference in New Issue
Block a user