Skip to content

Commit

Permalink
Merge pull request #62 from vordgi/get-61
Browse files Browse the repository at this point in the history
get-56 improve intercepted routes parsing in get-params
  • Loading branch information
vordgi authored May 1, 2024
2 parents 840c6cb + 21289d0 commit 508e83e
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 26 deletions.
7 changes: 5 additions & 2 deletions package/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ export const normalizePathname = (pathname: string) => {
export const normalizePagePath = (pagePath: string) => {
const cleanPagePath = pagePath && new URL(pagePath, "http://n").pathname;
const pagePathWithoutFileType = cleanPagePath?.replace(/(\/page|\/_not-found)$/, "");
const pagePathWithoutGroups = pagePathWithoutFileType.replace(/\/(\([^)]+\)|\@.+)/g, "");
return pagePathWithoutGroups || "/";
const pagePathWithoutGroups = pagePathWithoutFileType
.split("/")
.filter((segment) => !segment.match(/^(\([^)]+\)$|^\@.+$)/g));

return pagePathWithoutGroups.join("/") || "/";
};

export const parseSegments = (pagePathParts: string[], pathnameParts: string[]) => {
Expand Down
14 changes: 0 additions & 14 deletions tests/nextjs-app/app/(frontend)/(corporate)/groups/groups.spec.ts

This file was deleted.

18 changes: 18 additions & 0 deletions tests/nextjs-app/app/(group1)/(.)intercepted/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { getPathname } from "@nimpl/getters/get-pathname";
import { getParams } from "@nimpl/getters/get-params";
import Nav from "../../../components/Nav";

export default function Page() {
const pathname = getPathname();
const params = getParams();

return (
<div id="intercepted-page">
<Nav />
<div>
<p id="intercepted-get-pathname">{pathname}</p>
<p id="intercepted-get-params">{JSON.stringify(params)}</p>
</div>
</div>
);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { getPathname } from "@nimpl/getters/get-pathname";
import { getParams } from "@nimpl/getters/get-params";
import { NamespaceServerContext } from "../../../../components/NamespaceServerContext";
import BlockWithContext from "../../../../components/BlockWithContext";
import Nav from "../../../../components/Nav";

export default function Page() {
Expand All @@ -14,9 +12,6 @@ export default function Page() {
<div>
<p id="get-pathname">{pathname}</p>
<p id="get-params">{JSON.stringify(params)}</p>
<NamespaceServerContext.Provider value={{ namespace: "dynamic-auto" }}>
<BlockWithContext />
</NamespaceServerContext.Provider>
</div>
</div>
);
Expand Down
57 changes: 57 additions & 0 deletions tests/nextjs-app/app/(group1)/groups.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { test, expect } from "@playwright/test";

test("should return correct page params and pathname for nested groups", async ({ page }) => {
await page.goto("/groups");
page.waitForSelector("#groups-page");

const pathname = await page.$("#get-pathname");
const pathnameRow = await pathname?.textContent();
expect(pathnameRow).toEqual("/groups");

const params = await page.$("#get-params");
const paramsRow = await params?.textContent();
expect(paramsRow && JSON.parse(paramsRow)).toEqual({});
});

test("should return correct page params and pathname for intercepting route", async ({ page }) => {
await page.goto("/intercepted");
page.waitForSelector("#intercepted-page");

const pathname = await page.$("#get-pathname");
const pathnameRow = await pathname?.textContent();
expect(pathnameRow).toEqual("/intercepted");

const params = await page.$("#get-params");
const paramsRow = await params?.textContent();
expect(paramsRow && JSON.parse(paramsRow)).toEqual({});
});

test("should return correct page params and pathname for intercepted route", async ({ page }) => {
await page.goto("/");
await page.waitForSelector("#home-page");
page.click("#to-intercepted");
await page.waitForSelector("#intercepted-page");

const pathname = await page.$("#intercepted-get-pathname");
const pathnameRow = await pathname?.textContent();
expect(pathnameRow).toEqual("/intercepted");

const params = await page.$("#intercepted-get-params");
const paramsRow = await params?.textContent();
expect(paramsRow && JSON.parse(paramsRow)).toEqual({});
});

test("should return correct page params and pathname for nested intercepted route", async ({ page }) => {
await page.goto("/nested");
await page.waitForSelector("#nested-page");
page.click("#to-nested-intercepted");
await page.waitForSelector("#nested-intercepted-page");

const pathname = await page.$("#intercepted-get-pathname");
const pathnameRow = await pathname?.textContent();
expect(pathnameRow).toEqual("/nested-intercepted");

const params = await page.$("#intercepted-get-params");
const paramsRow = await params?.textContent();
expect(paramsRow && JSON.parse(paramsRow)).toEqual({});
});
16 changes: 16 additions & 0 deletions tests/nextjs-app/app/(group1)/intercepted/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { getPathname } from "@nimpl/getters/get-pathname";
import { getParams } from "@nimpl/getters/get-params";

export default function Page() {
const pathname = getPathname();
const params = getParams();

return (
<div id="intercepted-page">
<div>
<p id="get-pathname">{pathname}</p>
<p id="get-params">{JSON.stringify(params)}</p>
</div>
</div>
);
}
16 changes: 16 additions & 0 deletions tests/nextjs-app/app/(group1)/nested-intercepted/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { getPathname } from "@nimpl/getters/get-pathname";
import { getParams } from "@nimpl/getters/get-params";

export default function Page() {
const pathname = getPathname();
const params = getParams();

return (
<div id="nested-intercepted-page">
<div>
<p id="get-pathname">{pathname}</p>
<p id="get-params">{JSON.stringify(params)}</p>
</div>
</div>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { getPathname } from "@nimpl/getters/get-pathname";
import { getParams } from "@nimpl/getters/get-params";
import Nav from "../../../../components/Nav";

export default function Page() {
const pathname = getPathname();
const params = getParams();

return (
<div id="nested-intercepted-page">
<Nav />
<div>
<p id="intercepted-get-pathname">{pathname}</p>
<p id="intercepted-get-params">{JSON.stringify(params)}</p>
</div>
</div>
);
}
9 changes: 9 additions & 0 deletions tests/nextjs-app/app/(group1)/nested/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Nav from "../../../components/Nav";

export default function Page() {
return (
<div id="nested-page">
<Nav />
</div>
);
}
21 changes: 16 additions & 5 deletions tests/nextjs-app/components/Nav/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import Link from "next/link";

export default function Nav() {
return (
<nav>
<Link href="/dynamic/auto">dynamic/auto</Link>
</nav>
);
return (
<nav>
<Link href="/dynamic/auto" id="to-dynamic-auto">
dynamic/auto
</Link>
<Link href="/intercepted" id="to-intercepted">
intercepted
</Link>
<Link href="/nested" id="to-nested">
nested
</Link>
<Link href="/nested-intercepted" id="to-nested-intercepted">
nested-intercepted
</Link>
</nav>
);
}

0 comments on commit 508e83e

Please sign in to comment.