Skip to content

Commit 0145963

Browse files
committed
Improve test
1 parent a4e5b76 commit 0145963

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,22 @@ describe('<Notification />', () => {
1212
);
1313
const dataProvider = { delete: deleteOne } as any;
1414
render(<ConsecutiveUndoable dataProvider={dataProvider} />);
15-
await screen.findByText('Post deleted');
15+
(await screen.findByText('Delete post 1')).click();
16+
17+
// the notification shows up
18+
await screen.findByText('Post 1 deleted');
19+
// but the delete hasn't been called yet
1620
expect(deleteOne).toHaveBeenCalledTimes(0);
17-
await waitFor(() => {
18-
expect(deleteOne).toHaveBeenCalledTimes(1);
19-
});
21+
22+
screen.getByText('Delete post 2').click();
23+
24+
// the second notification shows up
25+
await screen.findByText('Post 2 deleted');
26+
// the first delete has been called
27+
expect(deleteOne).toHaveBeenCalledTimes(1);
28+
2029
screen.getByText('ra.action.undo').click();
30+
// the second delete hasn't been called
2131
expect(deleteOne).toHaveBeenCalledTimes(1);
2232
});
2333
});

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
useDelete,
66
CoreAdminContext,
77
} from 'ra-core';
8-
import { Alert } from '@mui/material';
8+
import { Alert, Button, Stack } from '@mui/material';
99

1010
import { Notification } from './Notification';
1111

@@ -155,24 +155,26 @@ export const CustomNode = () => (
155155
</Wrapper>
156156
);
157157

158-
const DeletePosts = () => {
158+
const DeletePost = ({ id }) => {
159159
const [deleteOne] = useDelete();
160160
const notify = useNotify();
161-
const deletePost = id => {
161+
const deletePost = () => {
162162
deleteOne(
163163
'posts',
164164
{ id },
165165
{
166166
mutationMode: 'undoable',
167-
onSuccess: () => notify('Post deleted', { undoable: true }),
167+
onSuccess: () =>
168+
notify(`Post ${id} deleted`, { undoable: true }),
168169
}
169170
);
170171
};
171-
React.useEffect(() => {
172-
deletePost(1);
173-
setTimeout(deletePost, 100, 2);
174-
}, []); // eslint-disable-line react-hooks/exhaustive-deps
175-
return null;
172+
173+
return (
174+
<Button variant="outlined" onClick={deletePost}>
175+
Delete post {id}
176+
</Button>
177+
);
176178
};
177179

178180
export const ConsecutiveUndoable = ({
@@ -184,7 +186,10 @@ export const ConsecutiveUndoable = ({
184186
} as any,
185187
}) => (
186188
<CoreAdminContext dataProvider={dataProvider}>
187-
<DeletePosts />
189+
<Stack spacing={2} direction="row" m={2}>
190+
<DeletePost id={1} />
191+
<DeletePost id={2} />
192+
</Stack>
188193
<Notification />
189194
</CoreAdminContext>
190195
);

0 commit comments

Comments
 (0)