Skip to content

Commit

Permalink
add support for search prop (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
scottrippey committed Mar 27, 2024
2 parents 12af5b9 + 5afd47b commit b0eed5a
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/sour-buttons-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"next-router-mock": patch
---

Add support for parsing `search` parameter for URLs
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions src/MemoryRouter.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,55 @@ describe("MemoryRouter", () => {
});
});

describe("search parameter", () => {
it("happy path", async () => {
await memoryRouter.push({
pathname: "/path",
search: "foo=FOO&bar=BAR",
});
expectMatch(memoryRouter, {
asPath: "/path?foo=FOO&bar=BAR",
pathname: "/path",
query: {
foo: "FOO",
bar: "BAR",
},
});
});

it("multiple values can be specified for a query parameter", async () => {
await memoryRouter.push({
pathname: "/path",
search: "foo=FOO&foo=BAR",
});
expectMatch(memoryRouter, {
asPath: "/path?foo=FOO&foo=BAR",
pathname: "/path",
query: {
foo: ["FOO", "BAR"],
},
});
});

it("if search and query are both provided preference is given to search", async () => {
await memoryRouter.push({
pathname: "/path",
search: "foo=FOO&bar=BAR",
query: {
baz: "BAZ",
},
});
expectMatch(memoryRouter, {
asPath: "/path?foo=FOO&bar=BAR",
pathname: "/path",
query: {
foo: "FOO",
bar: "BAR",
},
});
});
});

describe("with different paths", () => {
it("the real path and query are used", async () => {
await memoryRouter.push("/real-path", "/as-path");
Expand Down
9 changes: 7 additions & 2 deletions src/MemoryRouter.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import type { NextRouter, RouterEvent } from "next/router";
import mitt, { MittEmitter } from "./lib/mitt";
import { parseUrl, stringifyQueryString } from "./urls";
import { parseUrl, parseQueryString, stringifyQueryString } from "./urls";

export type Url = string | UrlObject;
export type UrlObject = {
pathname?: string | null | undefined;
query?: NextRouter["query"];
hash?: string;
search?: string;
};
export type UrlObjectComplete = {
pathname: string;
Expand Down Expand Up @@ -210,9 +211,13 @@ export class MemoryRouter extends BaseRouter {
*/
function parseUrlToCompleteUrl(url: Url, currentPathname: string): UrlObjectComplete {
const parsedUrl = typeof url === "object" ? url : parseUrl(url);

const queryFromSearch = parsedUrl.search ? parseQueryString(parsedUrl.search) : undefined;
const query = queryFromSearch ?? parsedUrl.query ?? {};

return {
pathname: normalizeTrailingSlash(parsedUrl.pathname ?? currentPathname),
query: parsedUrl.query || {},
query,
hash: parsedUrl.hash || "",
routeParams: {},
};
Expand Down
5 changes: 5 additions & 0 deletions src/urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,8 @@ export function stringifyQueryString(query: NextRouter["query"]): string {
});
return params.toString();
}
export function parseQueryString(query: string): NextRouter["query"] | undefined {
const parsedUrl = parseUrl(`?${query}`);

return parsedUrl.query;
}

0 comments on commit b0eed5a

Please sign in to comment.