Repository navigation
test: cover client canonical metadata after hydration - #197
Conversation
shigahi
left a comment
There was a problem hiding this comment.
Thanks for following up on #195 with this. Running the generated client script in node:vm works well, and the test passes locally on Node 24 and 26.
To see what it guards against, I reintroduced a few regressions in src/code.ts. The test catches the MY_DOMAIN reference from #195, updating only the first matching tag, dropping twitter:url, and the pre-#194 client. It misses three:
- Using
location.pathnameinstead of the page ID for pages without a slug. The test opens the unmapped page at/<id>, where both give the same URL. Notion's own paths are/Title-<id>, so/Some-Page-${UNMAPPED}should be canonical at/${UNMAPPED}, as in the server-side test above. - Dropping the
PAGE_TO_SLUG[getPage()]lookup. A mapped page opened by page ID, such as/Some-Page-${OTHER}, should be canonical at/chapter. - Removing
'href'from the head observer'sattributeFilter. The test calls the callback directly, so the observe options are never checked.
Picking the head observer by its target, rather than by observers.length === 2 and observers[0], also makes the test independent of how many observers the script creates and in which order. With this, all three regressions fail the test:
class TestMutationObserver {
constructor(callback) { this.callback = callback; observers.push(this); }
observe(target, options) { this.target = target; this.options = options; }
}
// ...
const headObserver = observers.find((observer) => observer.target === document.head);
assert.ok(headObserver.options.attributeFilter.includes('href'));
// ...and call headObserver.callback([]) in simulateNotionHeadRewrite()
for (const [pathname, canonical] of [
['/', ''],
['/chapter', '/chapter'],
[`/Some-Page-${OTHER}`, '/chapter'],
[`/Some-Page-${UNMAPPED}`, `/${UNMAPPED}`],
]) {
location.pathname = pathname;
simulateNotionHeadRewrite();
expectCanonical(canonical);
}Two smaller points:
- Most of the stubs are never reached: the
pushState/replaceStatebodies,body.classList, thecreateElementmethods and the#notion-applookup. This is enough:The test setsconst history = {}; const window = { history, XMLHttpRequest: { prototype: {} } }; const document = { head: {}, title: '', createElement: () => ({}), querySelector: () => null, querySelectorAll: (selector) => selectors.get(selector) || [], };
location.pathnamedirectly and doesn't go through the history API, so please also adjust the PR description, which says it exercises observer callbacks after navigation. - There are two blank lines before the new test; one is enough.
Please run npm test locally before pushing the update.
…coverage Removed unused tests and updated document properties.
The previous commit removed two server-side canonical tests from classmethod#192 and the title assertion for pages without a slug, which failed CI. They cover the worker's HTML rewriting; the new test covers the client script. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks for applying the suggestions. The new test now catches all the regressions from the review. The last commit also removed two existing tests from #192 ("a mapped page opened by page ID is canonical at its slug" and "the home page keeps its canonical URL and metadata") and I pushed 4974002 to restore them. |
|
Thanks @shigahi! I've updated the test to target the head observer directly by its |
shigahi
left a comment
There was a problem hiding this comment.
Thanks, this adds the first tests for the client script.
Summary
test/generated-worker.test.mjsverifying that the generated client script correctly restores canonical and social URL tags when Notion client-side rewrites occur across root, slug, mapped title-id, and unmapped title-id routes.document.headand validates thatattributeFilterincludes'href'.This separate regression-test follow-up was invited by the maintainer on #195 after the behavior fix merged in #194.
Validation
AI assistance
Prepared with OpenAI Codex assistance.