fix(history): a step back never leaves the app for an older page
Chrome keeps 50 entries a tab, and when the list is full a new entry drops the oldest one the page left without a user gesture, which can be the page's own entry under a sub-view or an overlay. A programmatic back() then left the document; once it landed on an older entry whose restored Plex player started playing on its own. Every programmatic back() now asks the Navigation API first and, with no entry of this document below, settles in place: the sub-view turns to its root, an overlay's entry drops its mark, the channel page and the YouTube search clear.
This commit is contained in:
@@ -55,6 +55,20 @@ afterEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
/** The Navigation API's view of the tab: `below` = whether each entry under the current one belongs
|
||||
* to this document, nearest first. jsdom has no Navigation API; Chrome's says this. */
|
||||
function stubNavigation(below: boolean[]) {
|
||||
const entries = [...below].reverse().map((sameDocument) => ({ sameDocument }));
|
||||
entries.push({ sameDocument: true });
|
||||
Object.defineProperty(window, "navigation", {
|
||||
configurable: true,
|
||||
value: { currentEntry: { index: entries.length - 1 }, entries: () => entries },
|
||||
});
|
||||
}
|
||||
afterEach(() => {
|
||||
Reflect.deleteProperty(window, "navigation");
|
||||
});
|
||||
|
||||
describe("useBackToExit back-ladder", () => {
|
||||
it("nested active layers peel off one genuine Back at a time, topmost first (LIFO)", async () => {
|
||||
const outer = vi.fn();
|
||||
@@ -329,3 +343,48 @@ describe("closeTopOverlayThenRun", () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// See useHistorySubview's back: a full tab can have dropped the entry under an overlay's own. The
|
||||
// reconcile then has no entry of ours to step back to, and a back() would leave the document.
|
||||
describe("an overlay whose entry has no entry of this document below it", () => {
|
||||
it("drops its mark from the entry instead of stepping back out of the document", async () => {
|
||||
const back = stubBack();
|
||||
const layer = mountLayer(() => {});
|
||||
await settle();
|
||||
expect(window.history.state?._ov).toBeDefined();
|
||||
stubNavigation([false]);
|
||||
layer.unmount();
|
||||
await settle();
|
||||
expect(back).not.toHaveBeenCalled();
|
||||
expect(window.history.state?._ov).toBeUndefined();
|
||||
});
|
||||
|
||||
it("counts its own back()s still in flight: of two entries closing at once, only the first steps", async () => {
|
||||
// Under the two overlay entries: one entry of this document, then another document's.
|
||||
const landing: (() => void)[] = [];
|
||||
const back = vi.spyOn(window.history, "back").mockImplementation(() => {
|
||||
landing.push(() =>
|
||||
window.dispatchEvent(new PopStateEvent("popstate", { state: window.history.state })),
|
||||
);
|
||||
});
|
||||
const lower = mountLayer(() => {});
|
||||
const upper = mountLayer(() => {});
|
||||
await settle();
|
||||
stubNavigation([true, false]);
|
||||
upper.unmount();
|
||||
lower.unmount();
|
||||
await settle();
|
||||
expect(back).toHaveBeenCalledTimes(1);
|
||||
act(() => landing.forEach((land) => land())); // the one back() lands, the books are square again
|
||||
});
|
||||
|
||||
it("still steps back over its entry when the one below is this document's", async () => {
|
||||
const back = stubBack();
|
||||
const layer = mountLayer(() => {});
|
||||
await settle();
|
||||
stubNavigation([true]);
|
||||
layer.unmount();
|
||||
await settle();
|
||||
expect(back).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user