Skip to content

Commit

Permalink
Reformat.
Browse files Browse the repository at this point in the history
  • Loading branch information
molefrog committed Jan 28, 2025
1 parent c0d37bf commit 51dc15c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
6 changes: 3 additions & 3 deletions packages/wouter/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,10 @@ export function useSearchParams() {

const setSearchParams = useEvent((nextInit, options) => {
tempSearchParams = new URLSearchParams(
typeof nextInit === 'function' ? nextInit(tempSearchParams) : nextInit,
typeof nextInit === "function" ? nextInit(tempSearchParams) : nextInit
);
navigate(location + '?' + tempSearchParams, options);
})
navigate(location + "?" + tempSearchParams, options);
});

return [searchParams, setSearchParams];
}
Expand Down
24 changes: 13 additions & 11 deletions packages/wouter/test/use-search-params.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,23 @@ it("can return browser search params", () => {
history.replaceState(null, "", "/users?active=true");
const { result } = renderHook(() => useSearchParams());

expect(result.current[0].get('active')).toBe("true");
expect(result.current[0].get("active")).toBe("true");
});

it("can change browser search params", () => {
history.replaceState(null, "", "/users?active=true");
const { result } = renderHook(() => useSearchParams());

expect(result.current[0].get('active')).toBe("true");
expect(result.current[0].get("active")).toBe("true");

act(() => result.current[1](prev => {
prev.set('active', 'false');
return prev;
}));
act(() =>
result.current[1]((prev) => {
prev.set("active", "false");
return prev;
})
);

expect(result.current[0].get('active')).toBe("false");
expect(result.current[0].get("active")).toBe("false");
});

it("can be customized in the Router", () => {
Expand All @@ -44,17 +46,17 @@ it("unescapes search string", () => {
expect(Array.from(searchResult.current[0].keys()).length).toBe(0);

act(() => navigate("/?nonce=not Found&country=საქართველო"));
expect(searchResult.current[0].get('nonce')).toBe("not Found");
expect(searchResult.current[0].get('country')).toBe("საქართველო");
expect(searchResult.current[0].get("nonce")).toBe("not Found");
expect(searchResult.current[0].get("country")).toBe("საქართველო");

// question marks
act(() => navigate("/?вопрос=как дела?"));
expect(searchResult.current[0].get('вопрос')).toBe("как дела?");
expect(searchResult.current[0].get("вопрос")).toBe("как дела?");
});

it("is safe against parameter injection", () => {
history.replaceState(null, "", "/?search=foo%26parameter_injection%3Dbar");
const { result } = renderHook(() => useSearchParams());

expect(result.current[0].get('search')).toBe("foo&parameter_injection=bar");
expect(result.current[0].get("search")).toBe("foo&parameter_injection=bar");
});
10 changes: 7 additions & 3 deletions packages/wouter/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,14 @@ export function useSearch<
H extends BaseSearchHook = BrowserSearchHook
>(): ReturnType<H>;

export type URLSearchParamsInit = ConstructorParameters<typeof URLSearchParams>[0];
export type URLSearchParamsInit = ConstructorParameters<
typeof URLSearchParams
>[0];
export type SetSearchParams = (
nextInit: URLSearchParamsInit | ((prev: URLSearchParams) => URLSearchParamsInit),
options?: { replace?: boolean; state?: any },
nextInit:
| URLSearchParamsInit
| ((prev: URLSearchParams) => URLSearchParamsInit),
options?: { replace?: boolean; state?: any }
) => void;

export function useSearchParams(): [URLSearchParams, SetSearchParams];
Expand Down

0 comments on commit 51dc15c

Please sign in to comment.