Skip to content

Commit 5b45135

Browse files
committed
Add debug values
1 parent bcd0b3e commit 5b45135

File tree

6 files changed

+40
-6
lines changed

6 files changed

+40
-6
lines changed

src/__test__/infinite.test.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { paths } from "./fixtures/petstore.js";
77

88
// Mock `useCallback` (return given function as-is)
99
vi.mock("react");
10-
const { useCallback, useMemo } = vi.mocked(React);
10+
const { useCallback, useMemo, useDebugValue } = vi.mocked(React);
1111
useCallback.mockImplementation((fn) => fn);
1212
useMemo.mockImplementation((fn) => fn());
1313

@@ -100,4 +100,16 @@ describe("createInfiniteHook", () => {
100100
undefined,
101101
);
102102
});
103+
104+
it("invokes debug value hook with path", () => {
105+
useInfinite("/pet/findByStatus", () => null);
106+
107+
expect(useDebugValue).toHaveBeenLastCalledWith("/pet/findByStatus");
108+
109+
useInfinite("/pet/findByTags", () => ({
110+
params: { query: { tags: ["tag1"] } },
111+
}));
112+
113+
expect(useDebugValue).toHaveBeenLastCalledWith("/pet/findByTags");
114+
});
103115
});

src/__test__/mutate.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type { paths } from "./fixtures/petstore.js";
99

1010
// Mock `useCallback` (return given function as-is)
1111
vi.mock("react");
12-
const { useCallback } = vi.mocked(React);
12+
const { useCallback, useDebugValue } = vi.mocked(React);
1313
useCallback.mockImplementation((fn) => fn);
1414

1515
// Mock `useSWRConfig`
@@ -78,6 +78,12 @@ describe("createMutateHook", () => {
7878
);
7979
});
8080

81+
it("invokes debug value hook with client prefix", () => {
82+
useMutate();
83+
84+
expect(useDebugValue).toHaveBeenLastCalledWith("<unique-key>");
85+
});
86+
8187
describe("useMutate -> mutate -> key matcher", () => {
8288
it("returns false for non-array keys", async () => {
8389
await mutate(["/pet/findByStatus"]);

src/__test__/query-base.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { paths } from "./fixtures/petstore.js";
77

88
// Mock `useCallback` (return given function as-is)
99
vi.mock("react");
10-
const { useCallback, useMemo } = vi.mocked(React);
10+
const { useCallback, useMemo, useDebugValue } = vi.mocked(React);
1111
useCallback.mockImplementation((fn) => fn);
1212
useMemo.mockImplementation((fn) => fn());
1313

@@ -113,4 +113,14 @@ describe("configureBaseQueryHook", () => {
113113
{ errorRetryCount: 56 },
114114
);
115115
});
116+
117+
it("invokes debug value hook with path", () => {
118+
useQuery("/pet/findByStatus");
119+
120+
expect(useDebugValue).toHaveBeenLastCalledWith("/pet/findByStatus");
121+
122+
useQuery("/pet/{petId}", { params: { path: { petId: 4 } } });
123+
124+
expect(useDebugValue).toHaveBeenLastCalledWith("/pet/{petId}");
125+
});
116126
});

src/infinite.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import useSWRInfinite, {
66
type SWRInfiniteKeyLoader,
77
} from "swr/infinite";
88
import type { TypesForGetRequest } from "./types.js";
9-
import { useCallback } from "react";
9+
import { useCallback, useDebugValue } from "react";
1010

1111
/**
1212
* ```ts
@@ -50,6 +50,8 @@ export function createInfiniteHook<
5050
type Key = [Prefix, Path, Init | undefined] | null;
5151
type KeyLoader = SWRInfiniteKeyLoader<Data, Key>;
5252

53+
useDebugValue(path);
54+
5355
const fetcher: SWRInfiniteFetcher<Data, KeyLoader> = useCallback(
5456
async ([_, path, init]) => {
5557
// @ts-expect-error TODO: Improve internal init types

src/mutate.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Client } from "openapi-fetch";
22
import type { MediaType, PathsWithMethod } from "openapi-typescript-helpers";
3-
import { useCallback } from "react";
3+
import { useCallback, useDebugValue } from "react";
44
import { type MutatorCallback, type MutatorOptions, useSWRConfig } from "swr";
55
import type { PartialDeep } from "type-fest";
66
import type { TypesForGetRequest } from "./types.js";
@@ -39,6 +39,8 @@ export function createMutateHook<
3939
return function useMutate() {
4040
const { mutate: swrMutate } = useSWRConfig();
4141

42+
useDebugValue(prefix);
43+
4244
function mutate<
4345
Path extends PathsWithMethod<Paths, "get">,
4446
R extends TypesForGetRequest<Paths, Path>,

src/query-base.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type {
66
} from "openapi-typescript-helpers";
77
import type { Fetcher, SWRHook } from "swr";
88
import type { TypesForGetRequest } from "./types.js";
9-
import { useCallback, useMemo } from "react";
9+
import { useCallback, useDebugValue, useMemo } from "react";
1010

1111
/**
1212
* @private
@@ -30,6 +30,8 @@ export function configureBaseQueryHook(useHook: SWRHook) {
3030
? [(Init | null)?, Config?]
3131
: [Init | null, Config?]
3232
) {
33+
useDebugValue(path);
34+
3335
const key = useMemo(
3436
() => (init !== null ? ([prefix, path, init] as const) : null),
3537
[prefix, path, init],

0 commit comments

Comments
 (0)