diff --git a/e2e/specs/routing.spec.ts b/e2e/specs/routing.spec.ts index 2c2dc576..3e44977b 100644 --- a/e2e/specs/routing.spec.ts +++ b/e2e/specs/routing.spec.ts @@ -1,7 +1,7 @@ import {spec} from "@welshman/lib" import type {TrustedEvent} from "@welshman/util" import {RelayMessageType} from "@welshman/net" -import {expect, getTranscript, roomPath, test, users} from "../harness" +import {expect, getTranscript, roomPath, spacePath, test, users} from "../harness" test("keeps two spaces' contents on their own relays", async ({seed, as}) => { const scenario = await seed(({relay, user}) => { @@ -55,3 +55,60 @@ test("keeps two spaces' contents on their own relays", async ({seed, as}) => { expect(strays).toEqual([]) }) + +test("goes back to the room you left when you switch spaces", async ({seed, as}) => { + const scenario = await seed(({relay, user}) => { + const space = relay("space") + const other = relay("other") + + space.room("lounge", {name: "Space Lounge"}) + other.room("lounge", {name: "Other Lounge"}) + space.join(user.alice, "lounge") + other.join(user.alice, "lounge") + }) + + const space = scenario.space("space") + const other = scenario.space("other") + const page = await as(users.alice, roomPath(space.url, "lounge")) + + await expect(page.getByRole("link", {name: "Space Lounge"})).toBeVisible() + + await page.locator('.primary-nav [data-tip^="other"]').click() + await expect(page).toHaveURL(new RegExp(spacePath(other.url))) + + await page.getByRole("link", {name: "Other Lounge"}).click() + await expect(page).toHaveURL(new RegExp(`${roomPath(other.url, "lounge")}$`)) + + // Two entries back: the other space's landing page, then the room this started on. The switch + // used to replace that room's entry rather than push one, so the second step overshot it. + await page.goBack() + await page.goBack() + + await expect(page).toHaveURL(new RegExp(`${roomPath(space.url, "lounge")}$`)) +}) + +test("does not stack a history entry for the space you are already in", async ({seed, as}) => { + const scenario = await seed(({relay, user}) => { + const space = relay("space") + + space.room("lounge", {name: "Space Lounge"}) + space.room("garden", {name: "Space Garden"}) + space.join(user.alice, "lounge") + space.join(user.alice, "garden") + }) + + const space = scenario.space("space") + const page = await as(users.alice, roomPath(space.url, "lounge")) + + await page.getByRole("link", {name: "Space Garden"}).click() + await expect(page).toHaveURL(new RegExp(`${roomPath(space.url, "garden")}$`)) + + // The space's entry page is the room you are on, so this navigates nowhere and should replace + // rather than push. One step back is the room this started on, not the one it never left. + await page.locator('.primary-nav [data-tip^="space"]').click() + await expect(page).toHaveURL(new RegExp(`${roomPath(space.url, "garden")}$`)) + + await page.goBack() + + await expect(page).toHaveURL(new RegExp(`${roomPath(space.url, "lounge")}$`)) +}) diff --git a/src/app/components/PrimaryNavItemSpace.svelte b/src/app/components/PrimaryNavItemSpace.svelte index 4cd68472..174f7649 100644 --- a/src/app/components/PrimaryNavItemSpace.svelte +++ b/src/app/components/PrimaryNavItemSpace.svelte @@ -1,8 +1,10 @@ diff --git a/src/routes/spaces/[relay]/+page.svelte b/src/routes/spaces/[relay]/+page.svelte index 4cbbe046..eefe4f54 100644 --- a/src/routes/spaces/[relay]/+page.svelte +++ b/src/routes/spaces/[relay]/+page.svelte @@ -13,7 +13,7 @@ $effect(() => { if (width > md) { - goToSpace(url) + goToSpace(url, {replaceState: true}) } })