test-utils.tsx 1.3 KB

12345678910111213141516171819202122232425262728293031
  1. import React, { FC, ReactElement } from 'react';
  2. import { render, RenderOptions, renderHook } from '@testing-library/react';
  3. import { ApolloClient, ApolloProvider, HttpLink, InMemoryCache } from '@apollo/client';
  4. import fetch from 'isomorphic-fetch';
  5. import { TRPCTestClientProvider } from './TRPCTestClientProvider';
  6. const link = new HttpLink({
  7. uri: 'http://localhost:3000/graphql',
  8. // Use explicit `window.fetch` so tha outgoing requests
  9. // are captured and deferred until the Service Worker is ready.
  10. fetch: (...args) => fetch(...args),
  11. });
  12. // create a mock of Apollo Client
  13. export const mockApolloClient = new ApolloClient({
  14. cache: new InMemoryCache({}),
  15. link,
  16. });
  17. const AllTheProviders: FC<{ children: React.ReactNode }> = ({ children }) => (
  18. <TRPCTestClientProvider>
  19. <ApolloProvider client={mockApolloClient}>{children}</ApolloProvider>
  20. </TRPCTestClientProvider>
  21. );
  22. const customRender = (ui: ReactElement, options?: Omit<RenderOptions, 'wrapper'>) => render(ui, { wrapper: AllTheProviders, ...options });
  23. const customRenderHook = (callback: () => any, options?: Omit<RenderOptions, 'wrapper'>) => renderHook(callback, { wrapper: AllTheProviders, ...options });
  24. export * from '@testing-library/react';
  25. export { customRender as render };
  26. export { customRenderHook as renderHook };