Skip to content

Commit

Permalink
Extract query provider, write test for useResumeData
Browse files Browse the repository at this point in the history
  • Loading branch information
caglarturali committed Oct 23, 2024
1 parent 1858c5a commit fb475fd
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
16 changes: 3 additions & 13 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,21 @@
import { lazy, Suspense } from 'react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { ErrorBoundary } from 'react-error-boundary';
import { QueryProvider } from 'contexts/query';
import { ResumeProvider } from 'contexts/ResumeContext';
import LoadingView from 'views/LoadingView';
import ErrorView from 'views/ErrorView';

const queryClient = new QueryClient({
defaultOptions: {
queries: {
staleTime: 60 * 60 * 1000,
refetchOnWindowFocus: false,
retry: 2,
},
},
});

const CurriculumVitae = lazy(() => import('views/CurriculumVitae'));

export function App() {
return (
<ErrorBoundary fallback={<ErrorView />}>
<Suspense fallback={<LoadingView />}>
<QueryClientProvider client={queryClient}>
<QueryProvider>
<ResumeProvider>
<CurriculumVitae />
</ResumeProvider>
</QueryClientProvider>
</QueryProvider>
</Suspense>
</ErrorBoundary>
);
Expand Down
26 changes: 26 additions & 0 deletions src/contexts/query.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { PropsWithChildren } from 'react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';

export const queryClient = new QueryClient({
defaultOptions: {
queries: {
staleTime: 60 * 60 * 1000,
refetchOnWindowFocus: false,
retry: 2,
},
},
});

export function QueryProvider({ children }: PropsWithChildren) {
return (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
);
}

export function withClient(client: QueryClient) {
return function QueryProviderWithClient({ children }: PropsWithChildren) {
return (
<QueryClientProvider client={client}>{children}</QueryClientProvider>
);
};
}
17 changes: 17 additions & 0 deletions src/hooks/__tests__/useResumeData.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { QueryClient } from '@tanstack/react-query';
import { renderHook, waitFor } from '@testing-library/react';
import useResumeData from 'hooks/useResumeData';
import { withClient } from 'contexts/query';

describe('useResumeData', async () => {
test('should render the hook correctly', async () => {
const queryClient = new QueryClient();
const { result } = renderHook(useResumeData, {
wrapper: withClient(queryClient),
});

await waitFor(() => {
expect(result.current.data).toBeDefined();
});
});
});

0 comments on commit fb475fd

Please sign in to comment.