Files
wxapp_escort/tools/home-ui.test.js
T
lik af666567c1 chore(tools): 新增页面结构与 UI 回归测试脚本
补齐 check-pages 与首页、预约记录、下单页的 node --test 用例,
并在 package.json 挂上对应命令,改动样式后可先跑脚本再进开发者工具。
2026-11-16 19:29:48 +08:00

86 lines
3.5 KiB
JavaScript

const { test } = require('node:test')
const assert = require('node:assert/strict')
const fs = require('node:fs')
const path = require('node:path')
const vm = require('node:vm')
const root = path.resolve(__dirname, '..')
test('当前预约使用紧凑三行摘要,时间、服务及状态共用首行', () => {
const wxml = fs.readFileSync(path.join(root, 'pages/home/index.wxml'), 'utf8')
assert.match(wxml, /pending-heading">当前预约/)
assert.doesNotMatch(wxml, /pending-service-row/)
const summary = /<view class="pending-summary">([\s\S]*?)<\/view>/.exec(wxml)[1]
for (const field of ['whenText', 'timeText', 'serviceName', 'statusText']) {
assert.ok(summary.includes(`item.${field}`))
}
assert.match(wxml, /pending-hospital">{{item.hospitalName}}/)
assert.match(wxml, /pending-patient">就诊人:{{item.patientName}}/)
})
function createHome(wx = {}) {
let page
let restored = 0
vm.runInNewContext(fs.readFileSync(path.join(root, 'pages/home/index.js'), 'utf8'), {
Page(value) { page = value },
require(name) {
if (name === '../../utils/api.js') return {}
if (name === '../../utils/auth.js') return {}
if (name === '../../utils/navigation.js') return { restoreLightNavigation() { restored++ } }
return require(path.resolve(root, 'pages/home', name))
},
wx,
console: { warn() {} }
})
page.data = JSON.parse(JSON.stringify(page.data))
page.setData = function (data) { Object.assign(this.data, data) }
return { page, restored: () => restored }
}
test('首页品牌导航根据状态栏与胶囊尺寸布局,兼容系统信息旧接口', () => {
for (const method of ['getWindowInfo', 'getSystemInfoSync']) {
const { page } = createHome({
[method]: () => ({ statusBarHeight: 44 }),
getMenuButtonBoundingClientRect: () => ({ top: 50, height: 32, left: 280 })
})
page.updateNavigationLayout()
assert.equal(page.data.topbarStyle, 'padding-top: 44px; height: 88px;')
assert.equal(page.data.brandStyle, 'height: 44px; max-width: 248px;')
}
})
test('导航信息不可用时保留默认布局,视图就绪恢复深色文字', () => {
const { page, restored } = createHome()
page.updateNavigationLayout()
assert.equal(page.data.topbarStyle, '')
page.onReady()
assert.equal(restored(), 1)
})
test('预约预览显示具体时间、状态和完整医院名,缺值使用可读提示', () => {
const { page } = createHome()
const result = page.formatPendingRecord({
_id: '1', status: 'pending',
schedule: { date: '2026-11-15', startTime: '13:00' },
hospital: { name: '中国人民解放军总医院第九医学中心' }
})
assert.equal(result.whenText, '11-15')
assert.equal(result.timeText, '13:00')
assert.equal(result.statusText, '待确认')
assert.equal(result.hospitalName, '中国人民解放军总医院第九医学中心')
const empty = page.formatPendingRecord({})
assert.equal(empty.timeText, '时间待定')
assert.equal(empty.patientName, '待补充')
})
test('预约预览按日期时间排序、最多三条,不修改原数组', () => {
const { page } = createHome()
const records = ['15:00', '08:00', '13:00', '10:00'].map((startTime, index) => ({
_id: String(index), schedule: { date: '2026-11-15', startTime }
}))
const before = JSON.stringify(records)
const result = page.buildPendingPreview(records)
assert.equal(result.length, 3)
assert.equal(result[0].timeText, '08:00')
assert.equal(result[2].timeText, '13:00')
assert.equal(JSON.stringify(records), before)
})