Files
zyt/admin/tests/first-visit-registration-rankings.test.cjs
T

70 lines
3.4 KiB
JavaScript

const assert = require('node:assert/strict')
const fs = require('node:fs')
const path = require('node:path')
const test = require('node:test')
const { parse, compileScript } = require('@vue/compiler-sfc')
const ts = require('typescript')
const vue = require('vue')
const { renderToString } = require('vue/server-renderer')
const filename = path.join(__dirname, '../src/views/first_visit/registration_stats/index.vue')
const { descriptor, errors } = parse(fs.readFileSync(filename, 'utf8'), { filename })
assert.deepEqual(errors, [])
const { content } = compileScript(descriptor, { id: 'a05', inlineTemplate: true, templateOptions: { ssr: true } })
const { outputText } = ts.transpileModule(content, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 } })
const groups = ['performance', 'registrations', 'appointments']
// Render the real page, replacing its HTTP response and running the mount fetch before SSR.
async function rankings(samples) {
const rankings = Object.fromEntries(groups.map((key, i) => [key, samples[i].map((value, j) =>
({ admin_id: j + 1, name: '员工' + j, count: 1, value }))]))
const stub = { render: () => null }
const imports = {
vue: { ...vue, onMounted: vue.onServerPrefetch },
'@/api/first_visit': { firstVisitRegistrationStatsOverview: async () => ({ rankings }) },
'@element-plus/icons-vue': new Proxy({}, { get: () => stub }),
'element-plus': {}, 'vue-echarts': { default: stub }
}
const mod = { exports: {} }
new Function('require', 'module', 'exports', outputText)(id => imports[id] || require(id), mod, mod.exports)
const app = vue.createSSRApp(mod.exports.default)
for (const [tag] of descriptor.template.content.matchAll(/<el-[\w-]+/g)) {
const name = tag.slice(1)
if (!app.component(name)) app.component(name, stub)
}
app.directive('loading', {})
const html = await renderToString(app)
const panels = [...html.matchAll(/<article class="panel ranking-panel"[^>]*>([\s\S]*?)<\/article>/g)]
assert.equal(panels.length, 3)
return panels.map(([, panel]) => [...panel.matchAll(/<div class="ranking-row"[^>]*><b[^>]*>(\d+)<\/b>.*?<i style="width:([^;"]+);?"[^>]*><\/i><\/div><strong[^>]*>([^<]+)<\/strong>/gs)]
.map(([, rank, width, label]) => ({ rank: Number(rank), width, label })))
}
const cases = [
[[2, 1, 0], ['100%', '50%', '0%']],
[[100, 1, 0], ['100%', '1%', '0%']],
[[0, 0, 0], ['0%', '0%', '0%']]
]
for (const [values, expected] of cases) {
test(`three rankings: ${values.join('/')} retains rank and label with proportional fill`, async () => {
const panels = await rankings(groups.map(() => values))
for (let i = 0; i < panels.length; i++) {
assert.deepEqual(panels[i], values.map((value, j) => ({
rank: j + 1, width: expected[j],
label: [`¥${value.toFixed(2)}`, `${value} 笔`, `${value} 个`][i]
})), groups[i])
}
})
}
test('each group ignores invalid and non-positive values and scales independently without overflow', async () => {
const panels = await rankings([
[Number.MAX_VALUE, Number.MAX_VALUE / 2, 0],
['4', '1', 'n/a', -2, true, [4], null, '1e999'],
[Infinity, 0, '0.25', 1]
])
assert.deepEqual(panels.map(panel => panel.map(row => row.width)), [
['100%', '50%', '0%'], ['100%', '25%', '0%', '0%', '0%', '0%', '0%', '0%'], ['0%', '0%', '25%', '100%']
])
})