Skip to content

Commit

Permalink
fix(client): fix SWRConfiguration options, add typescript test case f…
Browse files Browse the repository at this point in the history
…or SWRConfig types

fixes: #50
  • Loading branch information
sannajammeh committed Oct 23, 2023
1 parent bb74ec5 commit 9a660c9
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 46 deletions.
2 changes: 1 addition & 1 deletion packages/client/src/createSWRProxyHooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type DecorateProcedure<
? {
useSWR: <
TData = inferProcedureOutput<TProcedure>,
TConfig extends SWRConfiguration<TData> = {}
TConfig extends SWRConfiguration<TData> = SWRConfiguration<TData>
>(
input: inferProcedureInput<TProcedure>,
opts?: TConfig & {
Expand Down
103 changes: 58 additions & 45 deletions test/unit/tests/query.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,74 +4,87 @@ import { expect, expectTypeOf, it } from "vitest";
import { render, screen, trpc, waitFor } from "../utils";

it("makes query without args", async () => {
const Component = () => {
const { data } = trpc.hello.useSWR();
const Component = () => {
const { data } = trpc.hello.useSWR();

return data ? <p>{data}</p> : <div>Loading...</div>;
};
return data ? <p>{data}</p> : <div>Loading...</div>;
};

render(<Component />);
render(<Component />);

expect(screen.getByText("Loading...")).toBeInTheDocument();
expect(screen.getByText("Loading...")).toBeInTheDocument();

await waitFor(() => {
expect(screen.getByText("world")).toBeInTheDocument();
});
await waitFor(() => {
expect(screen.getByText("world")).toBeInTheDocument();
});
});

it("makes query with args", async () => {
const Component = () => {
const { data: user } = trpc.user.get.useSWR({ id: 1 });
const Component = () => {
const { data: user } = trpc.user.get.useSWR({ id: 1 });

return user ? <p>{user.name}</p> : <div>Loading...</div>;
};
return user ? <p>{user.name}</p> : <div>Loading...</div>;
};

render(<Component />);
render(<Component />);

expect(screen.getByText("Loading...")).toBeInTheDocument();
expect(screen.getByText("Loading...")).toBeInTheDocument();

await waitFor(() => {
expect(screen.getByText("bar")).toBeInTheDocument();
});
await waitFor(() => {
expect(screen.getByText("bar")).toBeInTheDocument();
});
});

it("makes conditional query", async () => {
const Component = () => {
const [shouldFetch, setShouldFetch] = useState(true);
const Component = () => {
const [shouldFetch, setShouldFetch] = useState(true);

const { data } = trpc.hello.useSWR(undefined, {
isDisabled: !shouldFetch,
});
const { data } = trpc.hello.useSWR(undefined, {
isDisabled: !shouldFetch,
});

return (
<>
<button onClick={() => setShouldFetch(false)}>toggle</button>
<div>{data ? data : "disabled"}</div>
</>
);
};
return (
<>
<button onClick={() => setShouldFetch(false)}>toggle</button>
<div>{data ? data : "disabled"}</div>
</>
);
};

render(<Component />);
render(<Component />);

await waitFor(() => {
expect(screen.getByText("world")).toBeInTheDocument();
});
await waitFor(() => {
expect(screen.getByText("world")).toBeInTheDocument();
});

const toggleButton = await screen.findByText("toggle");
const toggleButton = await screen.findByText("toggle");

act(() => toggleButton.click());
act(() => toggleButton.click());

await waitFor(() => {
expect(screen.getByText("disabled")).toBeInTheDocument();
});
await waitFor(() => {
expect(screen.getByText("disabled")).toBeInTheDocument();
});
});

it("Allows correct types during suspense", async () => {
() => {
const { data } = trpc.hello.useSWR(void 0, { suspense: true});
() => {
const { data } = trpc.hello.useSWR(void 0, { suspense: true });

expectTypeOf(data).toBeString();
expectTypeOf(data).toBeString();

return <p>{data}</p>;
};
})
return <p>{data}</p>;
};
});

it("Has correct SWR option types", async () => {
() => {
const { data } = trpc.hello.useSWR(void 0, {
suspense: true,
onSuccess: (data) => {
expectTypeOf(data).toBeString();
},
});

return <p>{data}</p>;
};
});

1 comment on commit 9a660c9

@vercel
Copy link

@vercel vercel bot commented on 9a660c9 Oct 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

trpc-swr – ./

trpc-swr-sannajammeh.vercel.app
trpc-swr.vercel.app
trpc-swr-git-main-sannajammeh.vercel.app

Please sign in to comment.