Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backport fix for generateStaticParams #907

Merged
merged 2 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 93 additions & 32 deletions packages/expo-router/src/__tests__/loadStaticParamsAsync.test.node.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { RouteNode } from "../Route";
import { getExactRoutes } from "../getRoutes";
import { loadStaticParamsAsync } from "../loadStaticParamsAsync";
import {
loadStaticParamsAsync,
assertStaticParams,
} from "../loadStaticParamsAsync";
import { RequireContext } from "../types";

function createMockContextModule(
Expand All @@ -22,6 +25,53 @@ function dropFunctions({ loadRoute, ...node }: RouteNode) {
};
}

describe(assertStaticParams, () => {
it(`asserts parameters do not match supported dynamic properties`, () => {
expect(() =>
assertStaticParams(
{
contextKey: "./[post].tsx",
dynamic: [{ deep: false, name: "post" }],
},
{
shape: "square",
}
)
).toThrowErrorMatchingInlineSnapshot(`
"[./[post].tsx]: generateStaticParams() must return an array of params that match the dynamic route. Expected non-nullish values for key: "post".
Received:
{
"shape": square,
"post": undefined
}"
`);
});
it(`asserts nullish parameters`, () => {
expect(() =>
assertStaticParams(
{
contextKey: "./[post]/[other].tsx",
dynamic: [
{ deep: false, name: "post" },
{ deep: false, name: "other" },
],
},
{
// @ts-expect-error: expected
post: null,
}
)
).toThrowErrorMatchingInlineSnapshot(`
"[./[post]/[other].tsx]: generateStaticParams() must return an array of params that match the dynamic routes. Expected non-nullish values for keys: "post", "other".
Received:
{
"post": null,
"other": undefined
}"
`);
});
});

describe(loadStaticParamsAsync, () => {
it(`evaluates a single dynamic param`, async () => {
const route = getExactRoutes(
Expand Down Expand Up @@ -77,22 +127,26 @@ describe(loadStaticParamsAsync, () => {
});

it(`evaluates with nested dynamic routes`, async () => {
const generateStaticParamsParent = jest.fn(async () => {
return ["red", "blue"].map((color) => ({
color,
}));
});
const generateStaticParams = jest.fn(async ({ params }) => {
return ["square", "triangle"].map((shape) => ({
...params,
shape,
}));
});
const ctx = createMockContextModule({
"./_layout.tsx": { default() {} },
"./[color]/[shape].tsx": {
default() {},
async generateStaticParams({ params }) {
return ["square", "triangle"].map((shape) => ({
...params,
shape,
}));
},
generateStaticParams,
},
"./[color]/_layout.tsx": {
default() {},
generateStaticParams() {
return ["red", "blue"].map((color) => ({ color }));
},
generateStaticParams: generateStaticParamsParent,
},
});
const route = getExactRoutes(ctx);
Expand Down Expand Up @@ -132,18 +186,6 @@ describe(loadStaticParamsAsync, () => {
dynamic: [{ deep: false, name: "shape" }],
route: "[shape]",
},
{
children: [],
contextKey: "./[color]/square.tsx",
dynamic: null,
route: "square",
},
{
children: [],
contextKey: "./[color]/triangle.tsx",
dynamic: null,
route: "triangle",
},
],
contextKey: "./[color]/_layout.tsx",
dynamic: [{ deep: false, name: "color" }],
Expand Down Expand Up @@ -208,6 +250,19 @@ describe(loadStaticParamsAsync, () => {
initialRouteName: undefined,
route: "",
});

expect(generateStaticParamsParent).toBeCalledTimes(1);
expect(generateStaticParamsParent).toHaveBeenNthCalledWith(1, {
params: {},
});

expect(generateStaticParams).toBeCalledTimes(2);
expect(generateStaticParams).toHaveBeenNthCalledWith(1, {
params: { color: "red" },
});
expect(generateStaticParams).toHaveBeenNthCalledWith(2, {
params: { color: "blue" },
});
});

it(`throws when required parameter is missing`, async () => {
Expand All @@ -221,11 +276,14 @@ describe(loadStaticParamsAsync, () => {
},
})
)!;
await expect(
loadStaticParamsAsync(routes)
).rejects.toThrowErrorMatchingInlineSnapshot(
`"generateStaticParams() must return an array of params that match the dynamic route. Received {}"`
);
await expect(loadStaticParamsAsync(routes)).rejects
.toThrowErrorMatchingInlineSnapshot(`
"[./post/[post].tsx]: generateStaticParams() must return an array of params that match the dynamic route. Expected non-nullish values for key: "post".
Received:
{
"post": undefined
}"
`);
});

it(`evaluates with nested deep dynamic segments`, async () => {
Expand Down Expand Up @@ -510,11 +568,14 @@ describe(loadStaticParamsAsync, () => {
).rejects.toThrowErrorMatchingInlineSnapshot(
`"generateStaticParams() for route "./post/[...post].tsx" expected param "post" not to be empty while parsing "/"."`
);
await expect(
loadWithParam([{ post: null }])
).rejects.toThrowErrorMatchingInlineSnapshot(
`"generateStaticParams() must return an array of params that match the dynamic route. Received {"post":null}"`
);
await expect(loadWithParam([{ post: null }])).rejects
.toThrowErrorMatchingInlineSnapshot(`
"[./post/[...post].tsx]: generateStaticParams() must return an array of params that match the dynamic route. Expected non-nullish values for key: "post".
Received:
{
"post": null
}"
`);
await expect(
loadWithParam([{ post: false }])
).rejects.toThrowErrorMatchingInlineSnapshot(
Expand Down
Loading