Skip to content

Commit

Permalink
update useDataProvider tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vytautassvirskas committed Feb 24, 2025
1 parent 1e5a332 commit a9322d3
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion packages/ra-core/src/dataProvider/useDataProvider.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ describe('useDataProvider', () => {
);
});

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

const getList = jest.fn(() =>
Expand All @@ -253,6 +254,50 @@ describe('useDataProvider', () => {
);
});

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

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();
});

it('should call getList and not show error in production environment', async () => {
process.env.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();
});

it('should call custom and not show error', async () => {
const getCustom = jest.fn(() =>
Promise.resolve({ result: [{ id: 1, title: 'foo' }] })
Expand Down

0 comments on commit a9322d3

Please sign in to comment.