Skip to content

Commit 9e85b50

Browse files
committed
Fix useEditController tests and improve warning test.
1 parent 98bf0bf commit 9e85b50

File tree

1 file changed

+13
-25
lines changed

1 file changed

+13
-25
lines changed

packages/ra-core/src/controller/edit/useEditController.spec.tsx

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ import {
2828
CanAccess,
2929
DisableAuthentication,
3030
} from './useEditController.security.stories';
31-
import { EncodedId } from './useEditController.stories';
31+
import {
32+
EncodedId,
33+
WarningLogWithDifferentMeta,
34+
} from './useEditController.stories';
3235

3336
const Confirm = () => {
3437
const takeMutation = useTakeUndoableMutation();
@@ -50,6 +53,10 @@ describe('useEditController', () => {
5053
resource: 'posts',
5154
};
5255

56+
afterEach(() => {
57+
jest.restoreAllMocks();
58+
});
59+
5360
it('should call the dataProvider.getOne() function on mount', async () => {
5461
const getOne = jest
5562
.fn()
@@ -220,26 +227,7 @@ describe('useEditController', () => {
220227
it('should emit a warning when providing a different meta in query options and mutation options without redirecting', async () => {
221228
const warnFn = jest.spyOn(console, 'warn').mockImplementation(() => {});
222229

223-
const getOne = jest
224-
.fn()
225-
.mockImplementationOnce(() =>
226-
Promise.resolve({ data: { id: 0, title: 'hello' } })
227-
);
228-
const dataProvider = { getOne } as unknown as DataProvider;
229-
230-
render(
231-
<CoreAdminContext dataProvider={dataProvider}>
232-
<EditController
233-
{...defaultProps}
234-
resource="posts"
235-
queryOptions={{ meta: { foo: 'bar' } }}
236-
mutationOptions={{ meta: { foo: 'baz' } }}
237-
redirect={false}
238-
>
239-
{() => <div />}
240-
</EditController>
241-
</CoreAdminContext>
242-
);
230+
render(<WarningLogWithDifferentMeta />);
243231

244232
expect(warnFn).toHaveBeenCalledWith(
245233
'When not redirecting after editing, query meta and mutation meta should be the same, or you will have data update issues.'
@@ -290,6 +278,7 @@ describe('useEditController', () => {
290278
});
291279

292280
it('should return an undoable save callback by default', async () => {
281+
window.confirm = jest.fn().mockReturnValue(true);
293282
let post = { id: 12, test: 'previous' };
294283
const update = jest
295284
.fn()
@@ -322,7 +311,7 @@ describe('useEditController', () => {
322311
await waitFor(() => {
323312
screen.getByText('previous');
324313
});
325-
screen.getByLabelText('save').click();
314+
fireEvent.click(screen.getByLabelText('save'));
326315
await waitFor(() => {
327316
screen.getByText('updated');
328317
});
@@ -331,7 +320,7 @@ describe('useEditController', () => {
331320
data: { test: 'updated' },
332321
previousData: { id: 12, test: 'previous' },
333322
});
334-
screen.getByLabelText('confirm').click();
323+
fireEvent.click(screen.getByLabelText('confirm'));
335324
await waitFor(() => {
336325
screen.getByText('updated');
337326
});
@@ -365,8 +354,7 @@ describe('useEditController', () => {
365354
</EditController>
366355
</CoreAdminContext>
367356
);
368-
await new Promise(resolve => setTimeout(resolve, 10));
369-
screen.getByText('{"id":12}');
357+
await screen.findByText('{"id":12}');
370358
await act(async () => saveCallback({ foo: 'bar' }));
371359
await screen.findByText('{"id":12,"foo":"bar"}');
372360
expect(update).toHaveBeenCalledWith('posts', {

0 commit comments

Comments
 (0)