Skip to content
Merged
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
12 changes: 11 additions & 1 deletion packages/ra-ui-materialui/src/layout/Notification.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as React from 'react';
import { render, screen, waitFor } from '@testing-library/react';
import { fireEvent, render, screen, waitFor } from '@testing-library/react';

import {
ConsecutiveNotifications,
ConsecutiveUndoable,
CustomNotificationWithAction,
} from './Notification.stories';
Expand Down Expand Up @@ -43,4 +44,13 @@ describe('<Notification />', () => {
});
expect(consoleLog).toHaveBeenCalledWith('Custom action');
});
it('should display consecutive notifications', async () => {
const { container } = render(<ConsecutiveNotifications />);
await screen.findByText('hello, world');
// This line ensures the test fails without the fix
await new Promise(resolve => setTimeout(resolve, 200));
expect(screen.queryByText('goodbye, world')).toBeNull();
fireEvent.click(container);
await screen.findByText('goodbye, world');
});
});
17 changes: 14 additions & 3 deletions packages/ra-ui-materialui/src/layout/Notification.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@ const Wrapper = ({ children }) => (
</NotificationContextProvider>
);

const BasicNotification = () => {
const BasicNotification = ({
message = 'hello, world',
}: {
message?: string;
}) => {
const notify = useNotify();
React.useEffect(() => {
notify('hello, world');
}, [notify]);
notify(message);
}, [message, notify]);
return null;
};

Expand Down Expand Up @@ -243,3 +247,10 @@ export const CustomNotificationWithAction = () => (
<CustomNotificationElementWithAction />
</Wrapper>
);

export const ConsecutiveNotifications = () => (
<Wrapper>
<BasicNotification />
<BasicNotification message="goodbye, world" />
</Wrapper>
);
27 changes: 13 additions & 14 deletions packages/ra-ui-materialui/src/layout/Notification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,22 @@ export const Notification = (inProps: NotificationProps) => {
setCurrentNotification(notification);
setOpen(true);
}
} else if (notifications.length && currentNotification && open) {
// Close an active snack when a new one is added
setOpen(false);
}

const beforeunload = (e: BeforeUnloadEvent) => {
e.preventDefault();
const confirmationMessage = '';
e.returnValue = confirmationMessage;
return confirmationMessage;
};

if (currentNotification?.notificationOptions?.undoable) {
window.addEventListener('beforeunload', beforeunload);
return () => {
window.removeEventListener('beforeunload', beforeunload);
if (currentNotification) {
const beforeunload = (e: BeforeUnloadEvent) => {
e.preventDefault();
const confirmationMessage = '';
e.returnValue = confirmationMessage;
return confirmationMessage;
};

if (currentNotification?.notificationOptions?.undoable) {
window.addEventListener('beforeunload', beforeunload);
return () => {
window.removeEventListener('beforeunload', beforeunload);
};
}
}
}, [notifications, currentNotification, open, takeNotification]);

Expand Down
Loading