test(web): lock the sidebar highlighting behind tests
It has been reported twice and fixed twice by reading the code, which is how it came back. The decision is now a module of its own — the shell only asks it which entry is current — and twelve cases pin it down: a detail page, a query string, every spelling of the home route, and the original regression where home stayed lit everywhere. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
3 files changed
+151
-12
No files matched your search
+3
-12
@@ -1,6 +1,7 @@
|
|||||||
import { api } from './core/api.js';
|
import { api } from './core/api.js';
|
||||||
import { h, mount, qs } from './core/dom.js';
|
import { h, mount, qs } from './core/dom.js';
|
||||||
import { onStatus, resetSubscriptions, startLive } from './core/live.js';
|
import { onStatus, resetSubscriptions, startLive } from './core/live.js';
|
||||||
|
import { activePath, highlight } from './core/nav.js';
|
||||||
import { ACCESS_LABELS, button, spinner, toast } from './core/ui.js';
|
import { ACCESS_LABELS, button, spinner, toast } from './core/ui.js';
|
||||||
|
|
||||||
/** Shell, navigation and hash routing of the dashboard (M16-T11). */
|
/** Shell, navigation and hash routing of the dashboard (M16-T11). */
|
||||||
@@ -138,7 +139,7 @@ const currentRoute = () => {
|
|||||||
const raw = window.location.hash.replace(/^#\/?/, '');
|
const raw = window.location.hash.replace(/^#\/?/, '');
|
||||||
const [path] = raw.split('?');
|
const [path] = raw.split('?');
|
||||||
const segments = path.split('/').filter(Boolean);
|
const segments = path.split('/').filter(Boolean);
|
||||||
return { segments, path };
|
return { segments, path, active: activePath(window.location.hash) };
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -148,18 +149,8 @@ const currentRoute = () => {
|
|||||||
* its own — otherwise it keeps pointing at the page you arrived on.
|
* its own — otherwise it keeps pointing at the page you arrived on.
|
||||||
*/
|
*/
|
||||||
function highlightNav(scope = document) {
|
function highlightNav(scope = document) {
|
||||||
const { segments } = currentRoute();
|
const active = highlight(scope, window.location.hash);
|
||||||
const active = segments[0] ?? '';
|
|
||||||
const route = ROUTES.find((entry) => entry.path === active);
|
const route = ROUTES.find((entry) => entry.path === active);
|
||||||
|
|
||||||
for (const link of scope.querySelectorAll('.nav a')) {
|
|
||||||
// Compare the route the link carries, not its href: a trailing slash or a
|
|
||||||
// query string in the hash must not make an entry look inactive.
|
|
||||||
const current = link.dataset.route === active;
|
|
||||||
link.classList.toggle('active', current);
|
|
||||||
if (current) link.setAttribute('aria-current', 'page');
|
|
||||||
else link.removeAttribute('aria-current');
|
|
||||||
}
|
|
||||||
document.title = route ? `${route.label} · FWS-Bot` : 'FWS-Bot';
|
document.title = route ? `${route.label} · FWS-Bot` : 'FWS-Bot';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
/**
|
||||||
|
* Which navigation entry is the current one (M16-T11).
|
||||||
|
*
|
||||||
|
* Kept apart from the shell so it can be tested: getting this wrong leaves the
|
||||||
|
* sidebar pointing at a page you are not on, which is the kind of bug that
|
||||||
|
* looks like nothing and gets reported three times.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* First segment of a hash route, which is what a sidebar entry stands for.
|
||||||
|
* `#/repos/FWS/Arch` and `#/repos` are the same entry; `#/releases?repo=x` too.
|
||||||
|
*/
|
||||||
|
export function activePath(hash = '') {
|
||||||
|
const raw = String(hash).replace(/^#\/?/, '');
|
||||||
|
const [path] = raw.split('?');
|
||||||
|
const [first = ''] = path.split('/').filter(Boolean);
|
||||||
|
return first;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Marks the matching link inside `scope` and clears the others.
|
||||||
|
* Returns the active path, so a caller can retitle the page from it.
|
||||||
|
*/
|
||||||
|
export function highlight(scope, hash = '') {
|
||||||
|
const active = activePath(hash);
|
||||||
|
|
||||||
|
for (const link of scope.querySelectorAll('.nav a')) {
|
||||||
|
// Compare the route a link carries, not its href: a trailing slash or a
|
||||||
|
// query string must not make an entry look inactive.
|
||||||
|
const current = (link.dataset.route ?? '') === active;
|
||||||
|
link.classList.toggle('active', current);
|
||||||
|
if (current) link.setAttribute('aria-current', 'page');
|
||||||
|
else link.removeAttribute('aria-current');
|
||||||
|
}
|
||||||
|
return active;
|
||||||
|
}
|
||||||
@@ -0,0 +1,112 @@
|
|||||||
|
import assert from 'node:assert/strict';
|
||||||
|
import { describe, it } from 'node:test';
|
||||||
|
import { activePath, highlight } from '../src/web/public/core/nav.js';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sidebar highlighting (M16-T11).
|
||||||
|
*
|
||||||
|
* Tested against a tiny stand-in for the DOM, because getting it wrong leaves
|
||||||
|
* the sidebar pointing at a page you are not on — a bug that looks like
|
||||||
|
* nothing and has been reported more than once.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/** Just enough element to satisfy `highlight`: classes, dataset, attributes. */
|
||||||
|
function link(route) {
|
||||||
|
const classes = new Set();
|
||||||
|
const attributes = {};
|
||||||
|
return {
|
||||||
|
dataset: { route },
|
||||||
|
attributes,
|
||||||
|
classList: {
|
||||||
|
toggle: (name, on) => (on ? classes.add(name) : classes.delete(name)),
|
||||||
|
contains: (name) => classes.has(name),
|
||||||
|
},
|
||||||
|
setAttribute: (name, value) => (attributes[name] = value),
|
||||||
|
removeAttribute: (name) => delete attributes[name],
|
||||||
|
get active() {
|
||||||
|
return classes.has('active');
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const sidebar = (...routes) => {
|
||||||
|
const links = routes.map(link);
|
||||||
|
return { links, querySelectorAll: () => links };
|
||||||
|
};
|
||||||
|
|
||||||
|
const activeNames = (bar) => bar.links.filter((entry) => entry.active).map((entry) => entry.dataset.route);
|
||||||
|
|
||||||
|
describe('active route', () => {
|
||||||
|
it('reads the first segment of a hash route', () => {
|
||||||
|
assert.equal(activePath('#/repos'), 'repos');
|
||||||
|
assert.equal(activePath('#/settings'), 'settings');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('treats a detail page as its section', () => {
|
||||||
|
assert.equal(activePath('#/repos/FWS/FWS-Arch'), 'repos');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('ignores a query string', () => {
|
||||||
|
assert.equal(activePath('#/releases?repo=FWS/Arch'), 'releases');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('maps every spelling of the home page to the same entry', () => {
|
||||||
|
for (const hash of ['', '#', '#/', '#//']) {
|
||||||
|
assert.equal(activePath(hash), '', `« ${hash} » should be the home page`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('sidebar highlighting', () => {
|
||||||
|
it('marks the current entry and only it', () => {
|
||||||
|
const bar = sidebar('', 'repos', 'settings');
|
||||||
|
highlight(bar, '#/repos');
|
||||||
|
assert.deepEqual(activeNames(bar), ['repos']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('marks the home entry on the home page', () => {
|
||||||
|
const bar = sidebar('', 'repos');
|
||||||
|
highlight(bar, '#/');
|
||||||
|
assert.deepEqual(activeNames(bar), ['']);
|
||||||
|
});
|
||||||
|
|
||||||
|
/** The regression that started all this: home stayed lit on every page. */
|
||||||
|
it('clears the home entry once elsewhere', () => {
|
||||||
|
const bar = sidebar('', 'repos');
|
||||||
|
highlight(bar, '#/');
|
||||||
|
highlight(bar, '#/repos');
|
||||||
|
assert.deepEqual(activeNames(bar), ['repos']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('keeps the section lit on a detail page', () => {
|
||||||
|
const bar = sidebar('', 'repos', 'releases');
|
||||||
|
highlight(bar, '#/repos/FWS/FWS-Arch');
|
||||||
|
assert.deepEqual(activeNames(bar), ['repos']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('keeps the section lit despite a query string', () => {
|
||||||
|
const bar = sidebar('', 'releases', 'deploy');
|
||||||
|
highlight(bar, '#/releases?repo=FWS/Arch');
|
||||||
|
assert.deepEqual(activeNames(bar), ['releases']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('lights nothing on an unknown route', () => {
|
||||||
|
const bar = sidebar('', 'repos');
|
||||||
|
highlight(bar, '#/inexistant');
|
||||||
|
assert.deepEqual(activeNames(bar), []);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('announces the current entry to screen readers', () => {
|
||||||
|
const bar = sidebar('', 'repos');
|
||||||
|
highlight(bar, '#/repos');
|
||||||
|
|
||||||
|
const [home, repos] = bar.links;
|
||||||
|
assert.equal(repos.attributes['aria-current'], 'page');
|
||||||
|
assert.equal(home.attributes['aria-current'], undefined);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('survives a link that carries no route', () => {
|
||||||
|
const bar = { links: [], querySelectorAll: () => [{ ...link(undefined) }] };
|
||||||
|
assert.doesNotThrow(() => highlight(bar, '#/repos'));
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in new issue
Block a user