From 9a660c94ef759b55295a6745b62994f07d6a596a Mon Sep 17 00:00:00 2001 From: Sanna Jammeh <50969683+sannajammeh@users.noreply.github.com> Date: Mon, 23 Oct 2023 21:17:20 +0200 Subject: [PATCH] fix(client): fix SWRConfiguration options, add typescript test case for SWRConfig types fixes: #50 --- packages/client/src/createSWRProxyHooks.tsx | 2 +- test/unit/tests/query.spec.tsx | 103 +++++++++++--------- 2 files changed, 59 insertions(+), 46 deletions(-) diff --git a/packages/client/src/createSWRProxyHooks.tsx b/packages/client/src/createSWRProxyHooks.tsx index 9dbb3e6..28dec9d 100644 --- a/packages/client/src/createSWRProxyHooks.tsx +++ b/packages/client/src/createSWRProxyHooks.tsx @@ -26,7 +26,7 @@ type DecorateProcedure< ? { useSWR: < TData = inferProcedureOutput, - TConfig extends SWRConfiguration = {} + TConfig extends SWRConfiguration = SWRConfiguration >( input: inferProcedureInput, opts?: TConfig & { diff --git a/test/unit/tests/query.spec.tsx b/test/unit/tests/query.spec.tsx index 5c0af2b..99125b2 100644 --- a/test/unit/tests/query.spec.tsx +++ b/test/unit/tests/query.spec.tsx @@ -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 ?

{data}

:
Loading...
; - }; + return data ?

{data}

:
Loading...
; + }; - render(); + render(); - 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 ?

{user.name}

:
Loading...
; - }; + return user ?

{user.name}

:
Loading...
; + }; - render(); + render(); - 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 ( - <> - -
{data ? data : "disabled"}
- - ); - }; + return ( + <> + +
{data ? data : "disabled"}
+ + ); + }; - render(); + render(); - 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

{data}

; - }; -}) \ No newline at end of file + return

{data}

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

{data}

; + }; +});