Files
xbcr/tests/test_settings_navigation.cjs

81 lines
3.6 KiB
JavaScript

const { test } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const vm = require('node:vm');
const source = fs.readFileSync(require('node:path').join(__dirname, '../static/js/settings-pages.js'), 'utf8');
function environment(hash = '') {
const elements = new Map(), events = {};
const document = { getElementById(id) {
if (!elements.has(id)) elements.set(id, {
hidden: true, scrollTop: 0, textContent: '', attributes: {},
classList: { add() {}, remove() {} },
setAttribute(key, value) { this.attributes[key] = value; }, removeAttribute(key) { delete this.attributes[key]; },
focus() {}, addEventListener() {}
});
return elements.get(id);
} };
const entries = [{ hash, state: null }];
let index = 0;
const location = { pathname: '/', search: '', get hash() { return entries[index].hash; } };
const history = {
get state() { return entries[index].state; },
pushState(state, _, url) { entries.splice(index + 1); entries.push({ state, hash: url.startsWith('#') ? url : '' }); index++; },
replaceState(state, _, url) { entries[index] = { state, hash: url.startsWith('#') ? url : '' }; },
back() { this.go(-1); },
go(delta) { index = Math.max(0, index + delta); events.popstate?.(); }
};
const window = { addEventListener: (name, fn) => { events[name] = fn; } };
vm.runInNewContext(source, { document, window, location, history, Map, Object });
const api = window.SettingsPages;
window.openSettingsPage = () => api.open('settings-modal');
window.openOrdersModal = () => api.open('orders-modal');
window.openShareModal = () => api.open('share-modal');
return { api, window, history, location, get: id => document.getElementById(id) };
}
test('root and children use one page shell; header Back restores the prior list and scroll', () => {
const e = environment();
e.api.open('settings-modal');
e.get('settings-page-main').scrollTop = 240;
e.api.open('orders-modal');
assert.equal(e.get('settings-modal').hidden, true);
assert.equal(e.get('orders-modal').hidden, false);
assert.equal(e.get('settings-page-title').textContent, '订单历史');
e.api.open('order-traj-modal');
e.api.back();
assert.equal(e.location.hash, '#settings/orders');
e.api.back();
assert.equal(e.location.hash, '#settings');
assert.equal(e.get('settings-page-main').scrollTop, 240);
e.api.back();
assert.equal(e.location.hash, '');
assert.equal(e.api.isOpen(), false);
});
test('return to map closes the entire hierarchy in one action', () => {
const e = environment();
e.api.open('settings-modal'); e.api.open('share-modal'); e.api.open('share-config-modal');
e.api.close();
assert.equal(e.location.hash, '');
assert.equal(e.api.isOpen(), false);
});
test('direct links to parameter-dependent pages safely restore their parent', () => {
const e = environment('#settings/orders/trajectory');
e.api.restore();
assert.equal(e.location.hash, '#settings/orders');
assert.equal(e.get('orders-modal').hidden, false);
e.api.back();
assert.equal(e.location.hash, '#settings');
});
test('the common Back button runs nested-page cleanup before navigating', () => {
const e = environment(); let cleanup = 0;
e.window.closeShareRemark = () => { cleanup++; e.api.back(); };
e.api.open('settings-modal'); e.api.open('share-modal'); e.api.open('share-remark-modal');
e.api.requestBack();
assert.equal(cleanup, 1);
assert.equal(e.location.hash, '#settings/sharing');
});