From 06cac1cb6312534578a149a1535df4685cf3b49d Mon Sep 17 00:00:00 2001 From: Sebastian <5648131+polyrainbow@users.noreply.github.com> Date: Wed, 8 Jan 2025 22:34:07 +0100 Subject: [PATCH] fix: random should create 1 history entry (#86) This fixes a regression introduced with the Navigation API where we did not correctly replace history entries anymore. Co-authored-by: Sebastian --- src/components/NoteAccessProvider.tsx | 3 +++ src/components/NoteView.tsx | 2 +- src/hooks/useGoToNote.tsx | 2 +- tests/integration/editor.spec.ts | 20 ++++++++++++++++++++ 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/components/NoteAccessProvider.tsx b/src/components/NoteAccessProvider.tsx index 73fea8b7..844ed174 100644 --- a/src/components/NoteAccessProvider.tsx +++ b/src/components/NoteAccessProvider.tsx @@ -33,6 +33,9 @@ const NoteAccessProvider = ({ // @ts-ignore navigation.navigate( getAppPath(PathTemplate.START, new Map(), urlParams), + { + history: "replace", + }, ); }); } else { diff --git a/src/components/NoteView.tsx b/src/components/NoteView.tsx index 734f2db6..4ffe3fbb 100644 --- a/src/components/NoteView.tsx +++ b/src/components/NoteView.tsx @@ -268,7 +268,7 @@ const NoteView = ({ slug }: NoteViewProps) => { ]), new URLSearchParams(location.search), ), - { replace: true }, + { history: "replace" }, ); } } diff --git a/src/hooks/useGoToNote.tsx b/src/hooks/useGoToNote.tsx index a7c9b5ab..9393babf 100644 --- a/src/hooks/useGoToNote.tsx +++ b/src/hooks/useGoToNote.tsx @@ -23,7 +23,7 @@ const useGoToNote = () => { // @ts-ignore return navigation.navigate(path, { - replace: params?.replace, + history: params?.replace ? "replace" : "push", state: { contentIfNewNote: params?.contentIfNewNote, }, diff --git a/tests/integration/editor.spec.ts b/tests/integration/editor.spec.ts index d6aaee07..deeb6f20 100644 --- a/tests/integration/editor.spec.ts +++ b/tests/integration/editor.spec.ts @@ -1103,4 +1103,24 @@ test.describe("Editor view", () => { await expect(onlyParagraphChild).not.toHaveAttribute("href"); await expect(onlyParagraphChild).toHaveText("www.example.com"); }); + + test( + "opening a random note should create one history entry", + async ({ page }) => { + await page.keyboard.type("Note 1"); + await page.click("#button_upload"); // save as "note-1" + + await page.click("#button_random-note"); + await page.click("#button_random-note"); + await page.click("#button_random-note"); + + // history length should be 6 now: + // start, new, note-1, random, random, random + + const result = await page.evaluate(() => { + return navigation.entries().map(e => e.url); + }); + expect(result.length).toBe(6); + }, + ); });