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: useQueryWithStore Should Reset Loading State With Enabled Option #6249

Merged
merged 1 commit into from
May 6, 2021
Merged
Show file tree
Hide file tree
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
54 changes: 54 additions & 0 deletions packages/ra-core/src/dataProvider/useQueryWithStore.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,60 @@ const UseQueryWithStore = ({
};

describe('useQueryWithStore', () => {
it('should not call the dataProvider if options.enabled is set to false and run when it changes to true', async () => {
const dataProvider = {
getOne: jest.fn(() =>
Promise.resolve({
data: { id: 1, title: 'titleFromDataProvider' },
})
),
};
const callback = jest.fn();
const { rerender } = renderWithRedux(
<DataProviderContext.Provider value={dataProvider}>
<UseQueryWithStore
callback={callback}
options={{ enabled: false }}
/>
</DataProviderContext.Provider>,
{ admin: { resources: { posts: { data: {} } } } }
);
let callArgs = callback.mock.calls[0][0];
expect(callArgs.data).toBeUndefined();
expect(callArgs.loading).toEqual(false);
expect(callArgs.loaded).toEqual(false);
expect(callArgs.error).toBeNull();
expect(callArgs.total).toBeNull();

rerender(
<DataProviderContext.Provider value={dataProvider}>
<UseQueryWithStore
callback={callback}
options={{ enabled: true }}
/>
</DataProviderContext.Provider>,
{ admin: { resources: { posts: { data: {} } } } }
);
await new Promise(resolve => setImmediate(resolve)); // dataProvider Promise returns result on next tick
callArgs = callback.mock.calls[2][0];
expect(callArgs.data).toBeUndefined();
expect(callArgs.loading).toEqual(true);
expect(callArgs.loaded).toEqual(false);
expect(callArgs.error).toBeNull();
expect(callArgs.total).toBeNull();

await new Promise(resolve => setImmediate(resolve)); // dataProvider Promise returns result on next tick
callArgs = callback.mock.calls[4][0];
expect(callArgs.data).toEqual({
id: 1,
title: 'titleFromDataProvider',
});
expect(callArgs.loading).toEqual(false);
expect(callArgs.loaded).toEqual(true);
expect(callArgs.error).toBeNull();
expect(callArgs.total).toBeNull();
});

it('should return data from dataProvider', async () => {
const dataProvider = {
getOne: jest.fn(() =>
Expand Down
5 changes: 3 additions & 2 deletions packages/ra-core/src/dataProvider/useQueryWithStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export const useQueryWithStore = <State extends ReduxState = ReduxState>(
data,
total,
error: null,
loading: true,
loading: options?.enabled === false ? false : true,
loaded: isDataLoaded(data),
refetch,
});
Expand All @@ -157,7 +157,7 @@ export const useQueryWithStore = <State extends ReduxState = ReduxState>(
data,
total,
error: null,
loading: true,
loading: options?.enabled === false ? false : true,
loaded: isDataLoaded(data),
refetch,
});
Expand Down Expand Up @@ -186,6 +186,7 @@ export const useQueryWithStore = <State extends ReduxState = ReduxState>(
total,
isDataLoaded,
refetch,
options.enabled,
]);

const dataProvider = useDataProvider();
Expand Down