f1fa5f7adf
lazy-config 暴露 loading 延迟/超时/文案与懒加载判定;lazy-page 用 defineAsyncComponent 提供 loading/error 组件,router 对所有注册路由统一包裹; main.css 增加 .page-loading/.page-error 兜底样式。
76 lines
3.1 KiB
TypeScript
76 lines
3.1 KiB
TypeScript
import test from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import { readSource } from './helpers.ts'
|
|
import {
|
|
isLazyLoader,
|
|
isPageModule,
|
|
PAGE_LOADING_DELAY,
|
|
PAGE_LOADING_TEXT,
|
|
PAGE_LOADING_TIMEOUT,
|
|
PAGE_LOAD_ERROR_TEXT,
|
|
} from '../src/router/lazy-config.ts'
|
|
import { adminPages } from '../src/router/routes.ts'
|
|
|
|
test('test_task_010_lazy_page_boundary_normal_primary_path', () => {
|
|
// 正常主路径:所有注册页面都是懒加载器,可被路由异步边界包裹。
|
|
assert.equal(adminPages.length, 3)
|
|
for (const page of adminPages) {
|
|
assert.equal(isLazyLoader(page.load), true, `页面 ${page.path} 必须是懒加载函数`)
|
|
}
|
|
})
|
|
|
|
test('test_task_010_lazy_page_boundary_normal_variant_input', async () => {
|
|
// 正常变体:加载器产物为页面模块(含 default)时识别成功。
|
|
const loader = async () => ({ default: {} })
|
|
const mod = await loader()
|
|
assert.equal(isPageModule(mod), true)
|
|
})
|
|
|
|
test('test_task_010_lazy_page_boundary_normal_repeated_operation_is_idempotent', () => {
|
|
// 正常重复:懒加载判定稳定。
|
|
assert.equal(isLazyLoader(() => Promise.resolve({})), true)
|
|
assert.equal(isLazyLoader(() => Promise.resolve({})), true)
|
|
})
|
|
|
|
test('test_task_010_lazy_page_boundary_boundary_empty_input', () => {
|
|
// 边界空值:缺省/空输入按非加载器/非页面模块处理,不崩溃。
|
|
assert.equal(isLazyLoader(undefined), false)
|
|
assert.equal(isLazyLoader(null), false)
|
|
assert.equal(isPageModule(null), false)
|
|
assert.equal(isPageModule(undefined), false)
|
|
})
|
|
|
|
test('test_task_010_lazy_page_boundary_boundary_single_item', () => {
|
|
// 边界单元素:单个含 default 的模块即可作为页面。
|
|
assert.equal(isPageModule({ default: () => null }), true)
|
|
})
|
|
|
|
test('test_task_010_lazy_page_boundary_boundary_limit_or_missing_field', () => {
|
|
// 边界上限:loading 延迟/超时为正且超时足够等待网络;文案不空。
|
|
assert.ok(PAGE_LOADING_DELAY > 0)
|
|
assert.ok(PAGE_LOADING_TIMEOUT > PAGE_LOADING_DELAY)
|
|
assert.ok(PAGE_LOADING_TIMEOUT <= 60000, '页面加载超时应处于合理上限内')
|
|
assert.ok(PAGE_LOADING_TEXT.length > 0)
|
|
})
|
|
|
|
test('test_task_010_lazy_page_boundary_invalid_input_rejected', () => {
|
|
// 异常输入:非函数/数组对象都不是懒加载器;无 default 模块不是页面模块。
|
|
assert.equal(isLazyLoader('load'), false)
|
|
assert.equal(isLazyLoader(123), false)
|
|
assert.equal(isLazyLoader({}), false)
|
|
assert.equal(isPageModule({}), false)
|
|
assert.equal(isPageModule([]), false)
|
|
})
|
|
|
|
test('test_task_010_lazy_page_boundary_dependency_failure_returns_actionable_message', () => {
|
|
// 依赖失败:router 对懒加载统一加异步边界;失败/加载中有样式与文案兜底。
|
|
const router = readSource('src/router/index.ts')
|
|
assert.match(router, /defineLazyPage/)
|
|
assert.equal(router.includes('adminRouteRecords.map'), true)
|
|
const lazyPage = readSource('src/router/lazy-page.ts')
|
|
assert.match(lazyPage, /PAGE_LOAD_ERROR_TEXT/)
|
|
const css = readSource('src/styles/main.css')
|
|
assert.match(css, /\.page-loading/)
|
|
assert.match(css, /\.page-error/)
|
|
})
|