Skip to content

Commit dab1dfa

Browse files
authored
Merge pull request #8941 from marmelab/fix-useGetList-onSuccess-delete
Fix `useGetList` default `onSuccess` throws when the query is disabled
2 parents 40e853a + 3be844f commit dab1dfa

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

packages/ra-core/src/dataProvider/useGetList.spec.tsx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { QueryClient } from 'react-query';
55

66
import { CoreAdminContext } from '../core';
77
import { useGetList } from './useGetList';
8+
import { DataProvider } from '../types';
89

910
const UseGetList = ({
1011
resource = 'posts',
@@ -343,4 +344,35 @@ describe('useGetList', () => {
343344
queryClient.getQueryData(['posts', 'getOne', { id: '1' }])
344345
).toEqual({ id: 1, title: 'live' });
345346
});
347+
348+
it('should not fail when the query is disabled and the cache gets updated by another query', async () => {
349+
const callback: any = jest.fn();
350+
const onSuccess = jest.fn();
351+
const queryClient = new QueryClient();
352+
const dataProvider = ({
353+
getList: jest.fn(() =>
354+
Promise.resolve({ data: [{ id: 1, title: 'live' }], total: 1 })
355+
),
356+
} as unknown) as DataProvider;
357+
render(
358+
<CoreAdminContext
359+
queryClient={queryClient}
360+
dataProvider={dataProvider}
361+
>
362+
<UseGetList
363+
options={{ enabled: false, onSuccess }}
364+
callback={callback}
365+
/>
366+
</CoreAdminContext>
367+
);
368+
await waitFor(() => {
369+
expect(callback).toHaveBeenCalled();
370+
});
371+
// Simulate the side-effect of e.g. a call to delete
372+
queryClient.setQueriesData(['posts', 'getList'], res => res);
373+
// If we get this far without an error being thrown, the test passes
374+
await waitFor(() => {
375+
expect(onSuccess).toHaveBeenCalled();
376+
});
377+
});
346378
});

packages/ra-core/src/dataProvider/useGetList.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,8 @@ export const useGetList = <RecordType extends RaRecord = any>(
8686
{
8787
...options,
8888
onSuccess: value => {
89-
const { data } = value;
9089
// optimistically populate the getOne cache
91-
data.forEach(record => {
90+
value?.data?.forEach(record => {
9291
queryClient.setQueryData(
9392
[resource, 'getOne', { id: String(record.id), meta }],
9493
oldRecord => oldRecord ?? record

0 commit comments

Comments
 (0)