优化
This commit is contained in:
@@ -1,7 +1,12 @@
|
||||
import test from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { readFile } from 'node:fs/promises'
|
||||
import { MODULES } from '../src/data/overseas-workbench.js'
|
||||
import { navigateBackOr, navigateToUnique } from '../src/utils/navigation.js'
|
||||
|
||||
const read = (path) => readFile(new URL(path, import.meta.url), 'utf8')
|
||||
const component = (source, dependencies) => new Function(...Object.keys(dependencies), source.match(/<script>([\s\S]*?)<\/script>/)[1].replace(/^\s*import .*$/gm, '').replace('export default', 'return'))(...Object.values(dependencies))
|
||||
|
||||
test('navigateBackOr returns through the stack and falls back when the stack is empty', () => {
|
||||
const calls = []
|
||||
globalThis.uni = {
|
||||
@@ -36,3 +41,46 @@ test('navigateToUnique does not stack the current utility page twice', () => {
|
||||
navigateToUnique('/pages/history/history')
|
||||
assert.deepEqual(calls, [{ url: '/pages/history/history' }])
|
||||
})
|
||||
|
||||
test('every registered page header returns home after a reload without page history', async (t) => {
|
||||
const previousUni = globalThis.uni
|
||||
const previousPages = globalThis.getCurrentPages
|
||||
t.after(() => { globalThis.uni = previousUni; globalThis.getCurrentPages = previousPages })
|
||||
const { pages } = JSON.parse(await read('../src/pages.json'))
|
||||
const header = component(await read('../src/components/PortalHeader.vue'), { PopupHeader: {}, navigateBackOr, navigateToUnique })
|
||||
const calls = []
|
||||
globalThis.uni = { reLaunch: (options) => calls.push(options), navigateBack: () => assert.fail('a reloaded page has no back stack') }
|
||||
let checked = 0
|
||||
for (const { path } of pages) {
|
||||
const source = await read(`../src/${path}.vue`)
|
||||
const tag = source.match(/<PortalHeader\b[^>]*\/>/)?.[0]
|
||||
if (!tag) continue
|
||||
assert.doesNotMatch(tag, /:fallback-url=/, path)
|
||||
const fallbackUrl = tag.match(/\bfallback-url="([^"]*)"/)?.[1] || header.props.fallbackUrl.default
|
||||
assert.equal(fallbackUrl, '/pages/home/home', path)
|
||||
globalThis.getCurrentPages = () => [{ route: path }]
|
||||
header.methods.goBack.call({ fallbackUrl })
|
||||
assert.deepEqual(calls.at(-1), { url: '/pages/home/home' }, path)
|
||||
checked++
|
||||
}
|
||||
assert.ok(checked > 0)
|
||||
assert.equal(calls.length, checked)
|
||||
})
|
||||
|
||||
test('home opens every enabled operation directly on a registered page', async (t) => {
|
||||
const previousUni = globalThis.uni
|
||||
t.after(() => { globalThis.uni = previousUni })
|
||||
const { pages } = JSON.parse(await read('../src/pages.json'))
|
||||
const registered = new Set(pages.map(({ path }) => `/${path}`))
|
||||
const home = component(await read('../src/pages/home/home.vue'), { PortalHeader: {}, MODULES })
|
||||
const calls = []
|
||||
globalThis.uni = { navigateTo: (options) => calls.push(options) }
|
||||
for (const group of MODULES) {
|
||||
for (const operation of group.operations.filter((item) => item.available)) {
|
||||
const count = calls.length
|
||||
home.methods.openOperation.call({ groups: MODULES }, operation)
|
||||
assert.equal(calls.length, count + 1, operation.name)
|
||||
assert.ok(registered.has(calls.at(-1).url.split('?')[0]), operation.name)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user