a8c5d357e6
新增 users-filter.ts 页面局部状态机:keyword/createdByAdminId/page/pageSize 默认值、改条件重置第一页(withFilterReset)、翻页钳到>=1、空白关键字不算激活、 toUserListParams 映射查询参数;UsersPage 改用它,搜索按钮/回车重置页码。 筛选状态不入 Pinia。8 用例全过,307 单测 + build 绿。
72 lines
3.2 KiB
TypeScript
72 lines
3.2 KiB
TypeScript
import test from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import { readSource } from './helpers.ts'
|
|
import {
|
|
createUserFilterState,
|
|
isFilterActive,
|
|
toUserListParams,
|
|
withFilterReset,
|
|
withPage,
|
|
} from '../src/pages/account/users-filter.ts'
|
|
import { USER_PAGE_DEFAULT_SIZE } from '../src/api/users-dto.ts'
|
|
|
|
test('test_task_043_user_filter_state_normal_primary_path', () => {
|
|
// 正常主路径:初始筛选状态默认空关键字、无管理员过滤、回到首页。
|
|
const s = createUserFilterState()
|
|
assert.equal(s.keyword, '')
|
|
assert.equal(s.createdByAdminId, null)
|
|
assert.equal(s.page, 1)
|
|
assert.equal(s.pageSize, USER_PAGE_DEFAULT_SIZE)
|
|
assert.equal(isFilterActive(s), false)
|
|
})
|
|
|
|
test('test_task_043_user_filter_state_normal_variant_input', () => {
|
|
// 正常变体:填关键字/管理员过滤后为激活态。
|
|
const s = { ...createUserFilterState(), keyword: '张', createdByAdminId: 3 }
|
|
assert.equal(isFilterActive(s), true)
|
|
})
|
|
|
|
test('test_task_043_user_filter_state_normal_repeated_operation_is_idempotent', () => {
|
|
// 正常重复:工厂每次返回独立等价状态;序列化不改输入。
|
|
assert.deepEqual(createUserFilterState(), createUserFilterState())
|
|
const s = withPage(createUserFilterState(), 3)
|
|
assert.deepEqual(s, s)
|
|
})
|
|
|
|
test('test_task_043_user_filter_state_boundary_empty_input', () => {
|
|
// 边界空值:空关键字在序列化时不下发空串。
|
|
const params = toUserListParams(createUserFilterState())
|
|
assert.equal(params.keyword, undefined)
|
|
})
|
|
|
|
test('test_task_043_user_filter_state_boundary_single_item', () => {
|
|
// 边界单元素:改关键字自动回到第一页,保留分页大小。
|
|
const s = withFilterReset({ ...createUserFilterState(), page: 4 }, { keyword: 'a' })
|
|
assert.equal(s.page, 1)
|
|
assert.equal(s.keyword, 'a')
|
|
assert.equal(s.pageSize, USER_PAGE_DEFAULT_SIZE)
|
|
})
|
|
|
|
test('test_task_043_user_filter_state_boundary_limit_or_missing_field', () => {
|
|
// 边界上限/越界页码:翻页钳到 >=1;大于当前也保留。
|
|
assert.equal(withPage(createUserFilterState(), 0).page, 1)
|
|
assert.equal(withPage(createUserFilterState(), 99).page, 99)
|
|
})
|
|
|
|
test('test_task_043_user_filter_state_invalid_input_rejected', () => {
|
|
// 异常输入:仅空白关键字不视为激活;筛选与分页参数映射正确。
|
|
assert.equal(isFilterActive({ ...createUserFilterState(), keyword: ' ' }), false)
|
|
const params = toUserListParams({ ...createUserFilterState(), keyword: ' x ', page: 2, pageSize: 20 })
|
|
assert.deepEqual(params, { page: 2, pageSize: 20, keyword: 'x', createdById: null })
|
|
})
|
|
|
|
test('test_task_043_user_filter_state_dependency_failure_returns_actionable_message', () => {
|
|
// 依赖失败:页面把筛选/分页状态局部化到 users-filter,不入 Pinia。
|
|
const page = readSource('src/pages/account/UsersPage.vue')
|
|
assert.match(page, /users-filter/, '页面复用局部筛选状态模块')
|
|
assert.equal(/defineStore|useAdminSessionStore/.test(page), false, '筛选状态不落在 session store')
|
|
const mod = readSource('src/pages/account/users-filter.ts')
|
|
assert.match(mod, /export function createUserFilterState/)
|
|
assert.match(mod, /users-dto/, '筛选模块复用分页 DTO 常量')
|
|
})
|