-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Remove TODO - Refactor duplicate code into dg-common
- Loading branch information
1 parent
bbd11c6
commit 03de9cb
Showing
10 changed files
with
191 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
packages/datagateway-common/src/queryClientSettingsUpdater.component.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import React from 'react'; | ||
import { render, RenderResult } from '@testing-library/react'; | ||
import { QueryClient, QueryClientProvider } from 'react-query'; | ||
import { Provider } from 'react-redux'; | ||
import thunk from 'redux-thunk'; | ||
import { initialState as dGCommonInitialState } from './state/reducers/dgcommon.reducer'; | ||
import { StateType } from './state/app.types'; | ||
import { createLocation } from 'history'; | ||
import configureStore from 'redux-mock-store'; | ||
import { | ||
QueryClientSettingsUpdater, | ||
QueryClientSettingsUpdaterRedux, | ||
} from './queryClientSettingsUpdater.component'; | ||
|
||
describe('QueryClientSettingsUpdater', () => { | ||
const renderComponent = ( | ||
queryRetries: number | undefined = undefined, | ||
queryClient = new QueryClient() | ||
): RenderResult => { | ||
function Wrapper({ | ||
children, | ||
}: React.PropsWithChildren<unknown>): JSX.Element { | ||
return ( | ||
<QueryClientProvider client={queryClient}> | ||
{children} | ||
</QueryClientProvider> | ||
); | ||
} | ||
return render( | ||
<QueryClientSettingsUpdater | ||
queryClient={queryClient} | ||
queryRetries={queryRetries} | ||
/>, | ||
{ | ||
wrapper: Wrapper, | ||
} | ||
); | ||
}; | ||
|
||
beforeEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
it('syncs retry prop to query client when it updates', async () => { | ||
const queryClient = new QueryClient({ | ||
// set random other option to check it doesn't get overridden | ||
defaultOptions: { queries: { staleTime: 300000 } }, | ||
}); | ||
let queryRetries = 1; | ||
const { rerender } = renderComponent(queryRetries, queryClient); | ||
|
||
expect(queryClient.getDefaultOptions()).toEqual({ | ||
queries: { staleTime: 300000, retry: 1 }, | ||
}); | ||
|
||
queryRetries = 0; | ||
|
||
rerender( | ||
<QueryClientSettingsUpdater | ||
queryClient={queryClient} | ||
queryRetries={queryRetries} | ||
/> | ||
); | ||
|
||
expect(queryClient.getDefaultOptions()).toEqual({ | ||
queries: { staleTime: 300000, retry: 0 }, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('QueryClientSettingsUpdaterRedux', () => { | ||
const initialState: StateType = { | ||
dgcommon: dGCommonInitialState, | ||
router: { | ||
action: 'POP', | ||
location: { ...createLocation('/'), query: {} }, | ||
}, | ||
}; | ||
const renderComponent = ( | ||
state: StateType = initialState, | ||
queryClient = new QueryClient() | ||
): RenderResult => { | ||
const mockStore = configureStore([thunk]); | ||
function Wrapper({ | ||
children, | ||
}: React.PropsWithChildren<unknown>): JSX.Element { | ||
return ( | ||
<Provider store={mockStore(state)}> | ||
<QueryClientProvider client={queryClient}> | ||
{children} | ||
</QueryClientProvider> | ||
</Provider> | ||
); | ||
} | ||
return render( | ||
<QueryClientSettingsUpdaterRedux queryClient={queryClient} />, | ||
{ | ||
wrapper: Wrapper, | ||
} | ||
); | ||
}; | ||
|
||
beforeEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
it('syncs retry setting to query client when it updates', async () => { | ||
const queryClient = new QueryClient({ | ||
// set random other option to check it doesn't get overridden | ||
defaultOptions: { queries: { staleTime: 300000 } }, | ||
}); | ||
const { rerender } = renderComponent(initialState, queryClient); | ||
|
||
initialState.dgcommon.queryRetries = 0; | ||
|
||
rerender(<QueryClientSettingsUpdaterRedux queryClient={queryClient} />); | ||
|
||
expect(queryClient.getDefaultOptions()).toEqual({ | ||
queries: { staleTime: 300000, retry: 0 }, | ||
}); | ||
}); | ||
}); |
39 changes: 39 additions & 0 deletions
39
packages/datagateway-common/src/queryClientSettingsUpdater.component.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import React from 'react'; | ||
import { QueryClient } from 'react-query'; | ||
import { StateType } from './state/app.types'; | ||
import { useSelector } from 'react-redux'; | ||
|
||
export const QueryClientSettingsUpdater: React.FC<{ | ||
queryRetries: number | undefined; | ||
queryClient: QueryClient; | ||
}> = (props) => { | ||
const { queryClient, queryRetries } = props; | ||
|
||
React.useEffect(() => { | ||
if (typeof queryRetries !== 'undefined') { | ||
const opts = queryClient.getDefaultOptions(); | ||
queryClient.setDefaultOptions({ | ||
...opts, | ||
queries: { ...opts.queries, retry: queryRetries }, | ||
}); | ||
} | ||
}, [queryClient, queryRetries]); | ||
|
||
return null; | ||
}; | ||
|
||
export const QueryClientSettingsUpdaterRedux: React.FC<{ | ||
queryClient: QueryClient; | ||
}> = (props) => { | ||
const { queryClient } = props; | ||
const queryRetries = useSelector( | ||
(state: StateType) => state.dgcommon.queryRetries | ||
); | ||
|
||
return ( | ||
<QueryClientSettingsUpdater | ||
queryClient={queryClient} | ||
queryRetries={queryRetries} | ||
/> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.