Files
order_site/apps/frontend/tests/worker-consignment-render-stability.test.ts
T

42 lines
1.5 KiB
TypeScript

import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import test from 'node:test'
test('打手寄卖页:空规格集合必须用稳定引用,避免 React #185 无限渲染', () => {
const source = readFileSync(
fileURLToPath(new URL('../src/pages/worker/WorkerConsignmentPage.tsx', import.meta.url)),
'utf8',
)
assert.ok(
source.includes('const EMPTY_CONSIGNMENT_PROFILES: WorkerConsignmentProfile[] = []'),
'必须定义模块级稳定空数组',
)
assert.ok(
source.includes('consignmentProfilesQuery.data?.data.profiles ?? EMPTY_CONSIGNMENT_PROFILES'),
'接口未返回时不能每次渲染新建 []',
)
assert.ok(
!source.includes('consignmentProfilesQuery.data?.data.profiles || []'),
'禁止使用每次渲染都新建的空数组回退',
)
})
test('打手寄卖页:展开节点同步必须先比较内容,相同就不 setState', () => {
const source = readFileSync(
fileURLToPath(new URL('../src/pages/worker/WorkerConsignmentPage.tsx', import.meta.url)),
'utf8',
)
assert.ok(source.includes('function isSameKeyList('), '必须有 key 列表内容比较')
assert.ok(
source.includes('isSameKeyList(prev, profileGroupKeys) ? prev : profileGroupKeys'),
'内容相同必须返回原引用,避免无限更新',
)
assert.ok(
!/setProfileExpandedKeys\(profileGroupKeys\)\s*\r?\n\s*\}, \[profileGroupKeys\]/.test(source),
'不能无条件写入新数组',
)
})