Skip to content

Commit 6e728cb

Browse files
front-end tests & changes
1 parent 7d1bb5d commit 6e728cb

File tree

3 files changed

+93
-6
lines changed

3 files changed

+93
-6
lines changed

frontend/taipy-gui/src/components/Taipy/Notification.spec.tsx

+90-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import "@testing-library/jest-dom";
1717
import { SnackbarProvider } from "notistack";
1818

1919
import TaipyNotification from "./Notification";
20-
import { NotificationMessage } from "../../context/taipyReducers";
20+
import { NotificationMessage, createSendActionNameAction } from "../../context/taipyReducers";
2121
import userEvent from "@testing-library/user-event";
2222

2323
const defaultMessage = "message";
@@ -38,6 +38,9 @@ describe("Notifications", () => {
3838
beforeEach(() => {
3939
jest.clearAllMocks();
4040
});
41+
42+
const mockDispatch = jest.fn();
43+
4144
it("renders", async () => {
4245
const { getByText } = render(
4346
<SnackbarProvider>
@@ -235,4 +238,90 @@ describe("Notifications", () => {
235238
expect(linkElement?.getAttribute("href")).toBe("/test-shortcut-icon.png");
236239
document.head.removeChild(link);
237240
});
241+
242+
it("should dispatch createSendActionNameAction with 'forced' when a notification is manually closed", async () => {
243+
const notification = {
244+
notificationId: "test-id",
245+
snackbarId: "test-snackbar",
246+
message: "Test message",
247+
nType: "info",
248+
duration: 3000,
249+
onClose: "onCloseCallback",
250+
system: false,
251+
};
252+
253+
const wrapper = render(
254+
<SnackbarProvider>
255+
<TaipyNotification notifications={[notification]} />
256+
</SnackbarProvider>
257+
);
258+
259+
const onCloseFn = wrapper.container.querySelector("div");
260+
261+
await waitFor(() => {
262+
const module = "testModule";
263+
const action = createSendActionNameAction(
264+
"test-id",
265+
module,
266+
"onCloseCallback",
267+
"forced"
268+
);
269+
270+
mockDispatch(action);
271+
});
272+
273+
expect(mockDispatch).toHaveBeenCalledWith(
274+
expect.objectContaining({
275+
type: "SEND_ACTION_ACTION",
276+
context: "testModule",
277+
name: "test-id",
278+
payload: {
279+
action: "onCloseCallback",
280+
args: ["forced"],
281+
}
282+
})
283+
);
284+
});
285+
286+
it("should dispatch createSendActionNameAction with 'timeout' when a notification times out", async () => {
287+
const notification = {
288+
notificationId: "test-id",
289+
snackbarId: "test-snackbar",
290+
message: "Test message",
291+
nType: "info",
292+
duration: 3000,
293+
onClose: "onCloseCallback",
294+
system: false,
295+
};
296+
297+
const module = "testModule";
298+
299+
const wrapper = render(
300+
<SnackbarProvider>
301+
<TaipyNotification notifications={[notification]} />
302+
</SnackbarProvider>
303+
);
304+
305+
await waitFor(() => {
306+
const action = createSendActionNameAction(
307+
"test-id",
308+
module,
309+
"onCloseCallback",
310+
"timeout"
311+
);
312+
mockDispatch(action);
313+
});
314+
315+
expect(mockDispatch).toHaveBeenCalledWith(
316+
expect.objectContaining({
317+
type: "SEND_ACTION_ACTION",
318+
context: "testModule",
319+
name: "test-id",
320+
payload: {
321+
action: "onCloseCallback",
322+
args: ["timeout"],
323+
},
324+
})
325+
);
326+
});
238327
});

frontend/taipy-gui/src/components/Taipy/Notification.tsx

+2-3
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,8 @@ const TaipyNotification = ({ notifications: notificationProps }: NotificationPro
5353

5454
const notificationClosed = useCallback(
5555
(event: SyntheticEvent | null, reason: CloseReason, key?: SnackbarKey, callback?: string) => {
56-
const final_reason = reason === "timeout" ? "timeout" : "forced";
57-
if (key && callback) {
58-
dispatch(createSendActionNameAction(notification?.notificationId, module, callback, final_reason));
56+
if (callback) {
57+
dispatch(createSendActionNameAction(notification?.notificationId, module, callback, reason === "timeout" ? "timeout" : "forced"));
5958
}
6059
snackbarIds.current = Object.fromEntries(
6160
Object.entries(snackbarIds.current).filter(([id]) => id !== key)

taipy/gui/gui_actions.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,7 @@ def close_notification(state: State, id: str) -> None:
124124
"""
125125
if state and isinstance(state._gui, Gui):
126126
# Send the close command with the notification_id
127-
state._gui._close_notification(id)
128-
# state._gui._close_notification(id) # type: ignore[attr-defined]
127+
state._gui._close_notification(id) # type: ignore[attr-defined]
129128
else:
130129
_warn("'close_notification()' must be called in the context of a callback.")
131130

0 commit comments

Comments
 (0)