Skip to content

Commit a4e5b76

Browse files
committed
Add unit test
1 parent 4dfa2b2 commit a4e5b76

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import * as React from 'react';
2+
import { render, screen, waitFor } from '@testing-library/react';
3+
4+
import { ConsecutiveUndoable } from './Notification.stories';
5+
6+
describe('<Notification />', () => {
7+
it('should confirm the first undoable notification when a second one starts', async () => {
8+
const deleteOne = jest
9+
.fn()
10+
.mockImplementation((_resource, { id }) =>
11+
Promise.resolve({ data: { id } })
12+
);
13+
const dataProvider = { delete: deleteOne } as any;
14+
render(<ConsecutiveUndoable dataProvider={dataProvider} />);
15+
await screen.findByText('Post deleted');
16+
expect(deleteOne).toHaveBeenCalledTimes(0);
17+
await waitFor(() => {
18+
expect(deleteOne).toHaveBeenCalledTimes(1);
19+
});
20+
screen.getByText('ra.action.undo').click();
21+
expect(deleteOne).toHaveBeenCalledTimes(1);
22+
});
23+
});

packages/ra-ui-materialui/src/layout/Notification.stories.tsx

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import * as React from 'react';
2-
import { useNotify, NotificationContextProvider } from 'ra-core';
2+
import {
3+
useNotify,
4+
NotificationContextProvider,
5+
useDelete,
6+
CoreAdminContext,
7+
} from 'ra-core';
38
import { Alert } from '@mui/material';
49

510
import { Notification } from './Notification';
@@ -149,3 +154,37 @@ export const CustomNode = () => (
149154
<CustomNodeNotification />
150155
</Wrapper>
151156
);
157+
158+
const DeletePosts = () => {
159+
const [deleteOne] = useDelete();
160+
const notify = useNotify();
161+
const deletePost = id => {
162+
deleteOne(
163+
'posts',
164+
{ id },
165+
{
166+
mutationMode: 'undoable',
167+
onSuccess: () => notify('Post deleted', { undoable: true }),
168+
}
169+
);
170+
};
171+
React.useEffect(() => {
172+
deletePost(1);
173+
setTimeout(deletePost, 100, 2);
174+
}, []); // eslint-disable-line react-hooks/exhaustive-deps
175+
return null;
176+
};
177+
178+
export const ConsecutiveUndoable = ({
179+
dataProvider = {
180+
delete: async (_resource, { id }) => {
181+
console.log('delete post', id);
182+
return { data: { id } };
183+
},
184+
} as any,
185+
}) => (
186+
<CoreAdminContext dataProvider={dataProvider}>
187+
<DeletePosts />
188+
<Notification />
189+
</CoreAdminContext>
190+
);

0 commit comments

Comments
 (0)