87 lines
3.9 KiB
JavaScript
87 lines
3.9 KiB
JavaScript
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 = {
|
|
navigateBack: (options) => calls.push(['back', options]),
|
|
reLaunch: (options) => calls.push(['reLaunch', options])
|
|
}
|
|
|
|
globalThis.getCurrentPages = () => [{ route: 'pages/home/home' }, { route: 'pages/profile/profile' }]
|
|
navigateBackOr('/pages/home/home')
|
|
assert.equal(calls[0][0], 'back')
|
|
|
|
calls.length = 0
|
|
globalThis.getCurrentPages = () => [{ route: 'pages/profile/profile' }]
|
|
navigateBackOr('/pages/home/home')
|
|
assert.deepEqual(calls, [['reLaunch', { url: '/pages/home/home' }]])
|
|
|
|
calls.length = 0
|
|
globalThis.getCurrentPages = () => [{ route: 'pages/home/home' }, { route: 'pages/profile/profile' }]
|
|
globalThis.uni.navigateBack = ({ fail }) => fail()
|
|
navigateBackOr('/pages/home/home')
|
|
assert.deepEqual(calls, [['reLaunch', { url: '/pages/home/home' }]])
|
|
})
|
|
|
|
test('navigateToUnique does not stack the current utility page twice', () => {
|
|
const calls = []
|
|
globalThis.uni = { navigateTo: (options) => calls.push(options) }
|
|
globalThis.getCurrentPages = () => [{ route: 'pages/profile/profile' }]
|
|
|
|
navigateToUnique('/pages/profile/profile')
|
|
assert.deepEqual(calls, [])
|
|
|
|
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)
|
|
}
|
|
}
|
|
})
|