TRPC Test Wrapper用于使用jest和react测试库进行单元和集成测试-客户端模拟

u5i3ibmn  于 2022-11-25  发布在  React
关注(0)|答案(1)|浏览(111)

如何使用jest和react测试库模拟TRPC客户端进行单元和集成测试?

rekjcdws

rekjcdws1#

我们需要围绕使用查询方法的组件创建一个TRPCTestingWrapper。

  • 对包含查询的组件进行单元测试(不必测试查询本身)
export const TRPCWrapper = ({ children }: { children: ReactNode }) => {
  const [queryClient] = useState(() => new QueryClient());
  const [trpcClient] = useState(() =>
    trpc.createClient({
      links: [
        httpBatchLink({
          url: platformSpecificLocalHostUrl,
          fetch: async (input, init?) => {
            const fetch = getFetch();
            return fetch(input, {
              ...init,
              // credentials: "include",
            });
          },
        }),
      ],
    })
  );

  return (
    <trpc.Provider client={trpcClient} queryClient={queryClient}>
      <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
    </trpc.Provider>
  );
};

相关问题