Skip to content

Commit 99ff732

Browse files
author
ismay
committed
feat(custom-data-provider): include react-query provider in custom-data-provider
Since the useDataQuery hook now uses react-query internally the CustomDataProvider has been updated as well: * We've configured react-query to not retry on errors when the useDataQuery hook is a descendant of the CustomDataProvider. That way your tests won't time out if you're testing queries that throw an error. * The CustomDataProvider now accepts a `queryClientOptions` prop, which allows you to configure react-query's QueryClient. For the available options see: https://react-query.tanstack.com/reference/QueryClient
1 parent ed2f9af commit 99ff732

File tree

3 files changed

+113
-136
lines changed

3 files changed

+113
-136
lines changed

services/data/src/__tests__/integration.test.tsx

+7-25
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,9 @@
11
import { render, waitFor } from '@testing-library/react'
22
import React, { ReactNode } from 'react'
3-
import { QueryClient, QueryClientProvider, setLogger } from 'react-query'
3+
import { setLogger } from 'react-query'
44
import { CustomDataProvider, DataQuery } from '../react'
55
import { QueryRenderInput } from '../types'
66

7-
// eslint-disable-next-line react/display-name
8-
const createWrapper = (mockData, queryClientOptions = {}) => ({
9-
children,
10-
}: {
11-
children?: ReactNode
12-
}) => {
13-
const queryClient = new QueryClient(queryClientOptions)
14-
15-
return (
16-
<QueryClientProvider client={queryClient}>
17-
<CustomDataProvider data={mockData}>{children}</CustomDataProvider>
18-
</QueryClientProvider>
19-
)
20-
}
21-
227
beforeAll(() => {
238
// Prevent the react-query logger from logging to the console
249
setLogger({
@@ -38,7 +23,9 @@ describe('Testing custom data provider and useQuery hook', () => {
3823
const data = {
3924
answer: 42,
4025
}
41-
const wrapper = createWrapper(data)
26+
const wrapper = ({ children }) => (
27+
<CustomDataProvider data={data}>{children}</CustomDataProvider>
28+
)
4229
const renderFunction = jest.fn(
4330
({ loading, error, data }: QueryRenderInput) => {
4431
if (loading) return 'loading'
@@ -88,14 +75,9 @@ describe('Testing custom data provider and useQuery hook', () => {
8875
throw expectedError
8976
},
9077
}
91-
// Disable automatic retries, see: https://react-query.tanstack.com/reference/useQuery
92-
const wrapper = createWrapper(data, {
93-
defaultOptions: {
94-
queries: {
95-
retry: false,
96-
},
97-
},
98-
})
78+
const wrapper = ({ children }) => (
79+
<CustomDataProvider data={data}>{children}</CustomDataProvider>
80+
)
9981
const renderFunction = jest.fn(
10082
({ loading, error, data }: QueryRenderInput) => {
10183
if (loading) return 'loading'
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react'
2+
import { QueryClient, QueryClientProvider } from 'react-query'
23
import { DataEngine } from '../../engine'
34
import { CustomDataLink, CustomData, CustomLinkOptions } from '../../links'
45
import { DataContext } from '../context/DataContext'
@@ -7,17 +8,37 @@ interface CustomProviderInput {
78
children: React.ReactNode
89
data: CustomData
910
options?: CustomLinkOptions
11+
queryClientOptions?: any
1012
}
13+
14+
/**
15+
* Disable automatic retries, which can cause tests to take unnecessarily long
16+
* see: https://react-query.tanstack.com/reference/useQuery
17+
*/
18+
const defaultQueryClientOptions = {
19+
defaultOptions: {
20+
queries: {
21+
retry: false,
22+
},
23+
},
24+
}
25+
1126
export const CustomDataProvider = ({
1227
children,
1328
data,
1429
options,
30+
queryClientOptions = defaultQueryClientOptions,
1531
}: CustomProviderInput) => {
1632
const link = new CustomDataLink(data, options)
1733
const engine = new DataEngine(link)
18-
34+
const queryClient = new QueryClient(queryClientOptions)
1935
const context = { engine }
36+
2037
return (
21-
<DataContext.Provider value={context}>{children}</DataContext.Provider>
38+
<QueryClientProvider client={queryClient}>
39+
<DataContext.Provider value={context}>
40+
{children}
41+
</DataContext.Provider>
42+
</QueryClientProvider>
2243
)
2344
}

0 commit comments

Comments
 (0)