Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix useGetList default onSuccess throws when the query is disabled #8941

Merged
merged 2 commits into from
May 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions packages/ra-core/src/dataProvider/useGetList.spec.tsx
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ import { QueryClient } from 'react-query';

import { CoreAdminContext } from '../core';
import { useGetList } from './useGetList';
import { DataProvider } from '../types';

const UseGetList = ({
resource = 'posts',
@@ -343,4 +344,35 @@ describe('useGetList', () => {
queryClient.getQueryData(['posts', 'getOne', { id: '1' }])
).toEqual({ id: 1, title: 'live' });
});

it('should not fail when the query is disabled and the cache gets updated by another query', async () => {
const callback: any = jest.fn();
const onSuccess = jest.fn();
const queryClient = new QueryClient();
const dataProvider = ({
getList: jest.fn(() =>
Promise.resolve({ data: [{ id: 1, title: 'live' }], total: 1 })
),
} as unknown) as DataProvider;
render(
<CoreAdminContext
queryClient={queryClient}
dataProvider={dataProvider}
>
<UseGetList
options={{ enabled: false, onSuccess }}
callback={callback}
/>
</CoreAdminContext>
);
await waitFor(() => {
expect(callback).toHaveBeenCalled();
});
// Simulate the side-effect of e.g. a call to delete
queryClient.setQueriesData(['posts', 'getList'], res => res);
// If we get this far without an error being thrown, the test passes
await waitFor(() => {
expect(onSuccess).toHaveBeenCalled();
});
});
});
3 changes: 1 addition & 2 deletions packages/ra-core/src/dataProvider/useGetList.ts
Original file line number Diff line number Diff line change
@@ -86,9 +86,8 @@ export const useGetList = <RecordType extends RaRecord = any>(
{
...options,
onSuccess: value => {
const { data } = value;
// optimistically populate the getOne cache
data.forEach(record => {
value?.data?.forEach(record => {
queryClient.setQueryData(
[resource, 'getOne', { id: String(record.id), meta }],
oldRecord => oldRecord ?? record