Skip to content

Conversation

tchak
Copy link

@tchak tchak commented Apr 29, 2025

https://developer.mozilla.org/en-US/docs/Web/API/NavigateEvent/sourceElement

This is very much a WIP. I am still trying to figure out how and where to add tests. However, I wanted to open a conversation about it and get your early feedback in case I am going down the wrong path regarding how to implement this.

@fabiancook
Copy link
Contributor

Hi, tests are added into the src/tests folder, the tests are ran across multiple environments so a single testing framework hasn't yet been included.

The idea with the test files is that if they exit successfully, its good, exit with an error, its a failing test.

For this PR, a test would be added to the navigation.scope tests, these run when we're in a browser with some form of navigation, which could also be the real implementation of navigation from the browser.

There is an anchor event test here, which should be extendable to check that sourceElement was added too

const anchor = document.createElement("a");
const randomUrl = `/random/${Math.random()}`;
anchor.href = randomUrl;
anchor.download = `named-${Math.random()}.png`;
anchor.id = "test-click-me";
const anchorPromise = new Promise<NavigateEvent>(
resolve => {
collection.addEventListener(
"navigate",
event => {
if (new URL(event.destination.url).pathname === randomUrl) {
resolve(event);
}
}
)
}
);
document.body.appendChild(anchor);
anchor.click();
const anchorEvent = await anchorPromise;
ok<NavigateEvent>(anchorEvent);
console.log("Anchor event seen:", anchorEvent.type, anchorEvent.originalEvent?.type, anchorEvent.destination.url);

e.g.

ok(anchorEvent.sourceElement, "Expected source element")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants