Skip to content

Commit

Permalink
add memoryLocation searchHook tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mitulagr2 committed Nov 14, 2024
1 parent 42718dd commit 97f556a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
21 changes: 21 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,17 @@ it("should support initial path", () => {
unmount();
});

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

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

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

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

Expand All @@ -33,6 +44,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();
});

it("should return standalone `navigate` method", () => {
const { hook, navigate } = memoryLocation();

Expand Down
13 changes: 13 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,18 @@ 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");
});

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

Expand Down

0 comments on commit 97f556a

Please sign in to comment.