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

Feat: memory location search hook #497

Merged
merged 9 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from 8 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
18 changes: 15 additions & 3 deletions packages/wouter/src/memory-location.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,18 @@ import { useSyncExternalStore } from "./react-deps.js";

export const memoryLocation = ({
path = "/",
searchPath = "",
static: staticLocation,
record,
} = {}) => {
let currentPath = path;
let initialPath = path;
if (searchPath) {
// join with & if path contains search query, and ? otherwise
initialPath += path.split("?")[1] ? "&" : "?";
initialPath += searchPath;
}

let [currentPath, currentSearch = ""] = initialPath.split("?");
const history = [currentPath];
mitulagr2 marked this conversation as resolved.
Show resolved Hide resolved
const emitter = mitt();

Expand All @@ -23,7 +31,7 @@ export const memoryLocation = ({
}
}

currentPath = path;
[currentPath, currentSearch = ""] = path.split("?");
emitter.emit("navigate", path);
};

Expand All @@ -39,15 +47,19 @@ export const memoryLocation = ({
navigate,
];

const useMemoryQuery = () =>
useSyncExternalStore(subscribe, () => currentSearch);

function reset() {
// clean history array with mutation to preserve link
history.splice(0, history.length);

navigateImplementation(path);
navigateImplementation(initialPath);
}

return {
hook: useMemoryLocation,
searchHook: useMemoryQuery,
navigate,
history: record ? history : undefined,
reset: record ? reset : undefined,
Expand Down
33 changes: 33 additions & 0 deletions packages/wouter/test/memory-location.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,29 @@ it("should support initial path", () => {
unmount();
});

it("should support initial path with query", () => {
const { searchHook } = memoryLocation({ path: "/test-case?foo=bar" });

const { result, unmount } = renderHook(() => searchHook());
const value = result.current;

expect(value).toBe("foo=bar");
unmount();
});

it("should support search path as parameter", () => {
const { searchHook } = memoryLocation({
path: "/test-case?foo=bar",
searchPath: "key=value",
});

const { result, unmount } = renderHook(() => searchHook());
const value = result.current;

expect(value).toBe("foo=bar&key=value");
unmount();
});

it('should return location hook that has initial path "/" by default', () => {
const { hook } = memoryLocation();

Expand All @@ -33,6 +56,16 @@ it('should return location hook that has initial path "/" by default', () => {
unmount();
});

it('should return search hook that has initial query "" by default', () => {
const { searchHook } = memoryLocation();

const { result, unmount } = renderHook(() => searchHook());
const value = result.current;

expect(value).toBe("");
unmount();
});

mitulagr2 marked this conversation as resolved.
Show resolved Hide resolved
it("should return standalone `navigate` method", () => {
const { hook, navigate } = memoryLocation();

Expand Down
28 changes: 28 additions & 0 deletions packages/wouter/test/use-search.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { renderHook, act } from "@testing-library/react";
import { useSearch, Router } from "wouter";
import { navigate } from "wouter/use-browser-location";
import { memoryLocation } from "wouter/memory-location";
import { it, expect, beforeEach } from "vitest";

beforeEach(() => history.replaceState(null, "", "/"));
Expand All @@ -24,6 +25,33 @@ it("can be customized in the Router", () => {
expect(result.current).toEqual("none");
});

it("can be customized with memoryLocation", () => {
const { searchHook } = memoryLocation({ path: "/foo?key=value" });

const { result } = renderHook(() => useSearch(), {
wrapper: (props) => {
return <Router searchHook={searchHook}>{props.children}</Router>;
},
});

expect(result.current).toEqual("key=value");
});

mitulagr2 marked this conversation as resolved.
Show resolved Hide resolved
it("can be customized with memoryLocation using search path parameter", () => {
const { searchHook } = memoryLocation({
path: "/foo?key=value",
searchPath: "foo=bar",
});

const { result } = renderHook(() => useSearch(), {
wrapper: (props) => {
return <Router searchHook={searchHook}>{props.children}</Router>;
},
});

expect(result.current).toEqual("key=value&foo=bar");
});

it("unescapes search string", () => {
const { result: searchResult } = renderHook(() => useSearch());

Expand Down
15 changes: 13 additions & 2 deletions packages/wouter/types/memory-location.d.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
import { BaseLocationHook, Path } from "./location-hook.js";
import {
BaseLocationHook,
BaseSearchHook,
Path,
SearchString,
} from "./location-hook.js";

type Navigate<S = any> = (
to: Path,
options?: { replace?: boolean; state?: S }
) => void;

type HookReturnValue = { hook: BaseLocationHook; navigate: Navigate };
type HookReturnValue = {
hook: BaseLocationHook;
searchHook: BaseSearchHook;
navigate: Navigate;
};
type StubHistory = { history: Path[]; reset: () => void };

export function memoryLocation(options?: {
path?: Path;
searchPath?: SearchString;
static?: boolean;
record?: false;
}): HookReturnValue;
export function memoryLocation(options?: {
path?: Path;
searchPath?: SearchString;
static?: boolean;
record: true;
}): HookReturnValue & StubHistory;