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 useDataProvider should only log errors in development mode #10515

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
67 changes: 66 additions & 1 deletion packages/ra-core/src/dataProvider/useDataProvider.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ const UseGetCustom = () => {
};

describe('useDataProvider', () => {
const originalEnv = process.env;

it('should return a way to call the dataProvider', async () => {
const getOne = jest.fn(() =>
Promise.resolve({ data: { id: 1, title: 'foo' } })
Expand Down Expand Up @@ -229,7 +231,13 @@ describe('useDataProvider', () => {
);
});

it('should call getList and show error', async () => {
it('should call getList and show error in development environment', async () => {
jest.resetModules();
process.env = {
...originalEnv,
NODE_ENV: 'development',
};

jest.spyOn(console, 'error').mockImplementation(() => {});

const getList = jest.fn(() =>
Expand All @@ -251,6 +259,63 @@ describe('useDataProvider', () => {
expect(queryByTestId('error')?.textContent).toBe(
'ra.notification.data_provider_error'
);

process.env = originalEnv;
});

it('should call getList and not show error in test environment', async () => {
jest.resetModules();
process.env = {
...originalEnv,
};

const getList = jest.fn(() =>
Promise.resolve({ data: [{ id: 1, title: 'foo' }] })
);
const dataProvider = { getList };
const { queryByTestId } = render(
<CoreAdminContext dataProvider={dataProvider}>
<UseGetList />
</CoreAdminContext>
);

expect(queryByTestId('loading')).not.toBeNull();
await act(async () => {
await new Promise(resolve => setTimeout(resolve));
});
expect(getList).toBeCalledTimes(1);
expect(queryByTestId('loading')).toBeNull();
expect(queryByTestId('error')).toBeNull();

process.env = originalEnv;
});

it('should call getList and not show error in production environment', async () => {
jest.resetModules();
process.env = {
...originalEnv,
NODE_ENV: 'production',
};

const getList = jest.fn(() =>
Promise.resolve({ data: [{ id: 1, title: 'foo' }] })
);
const dataProvider = { getList };
const { queryByTestId } = render(
<CoreAdminContext dataProvider={dataProvider}>
<UseGetList />
</CoreAdminContext>
);

expect(queryByTestId('loading')).not.toBeNull();
await act(async () => {
await new Promise(resolve => setTimeout(resolve));
});
expect(getList).toBeCalledTimes(1);
expect(queryByTestId('loading')).toBeNull();
expect(queryByTestId('error')).toBeNull();

process.env = originalEnv;
});

it('should call custom and not show error', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/ra-core/src/dataProvider/useDataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export const useDataProvider = <
.apply(dataProvider, args)
.then(response => {
if (
process.env.NODE_ENV !== 'production' &&
process.env.NODE_ENV === 'development' &&
reactAdminFetchActions.includes(type)
) {
validateResponseFormat(response, type);
Expand Down
Loading