Files
crawler-plugin/admin-frontend-vue/tests/task-43.test.ts
T

74 lines
3.4 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 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。
// module 13 起页面允许读 session store 取操作者角色/当前用户,但筛选状态仍不落 store。)
const page = readSource('src/pages/account/UsersPage.vue')
assert.match(page, /users-filter/, '页面复用局部筛选状态模块')
assert.equal(/defineStore/.test(page), false, '页面不得自行 defineStore 存放筛选状态')
assert.match(page, /createUserFilterState/, '页面以局部状态机创建筛选状态')
const mod = readSource('src/pages/account/users-filter.ts')
assert.match(mod, /export function createUserFilterState/)
assert.match(mod, /users-dto/, '筛选模块复用分页 DTO 常量')
})