|
@@ -21,15 +21,17 @@ import { NotificationMessage } from "../../context/taipyReducers";
|
|
import userEvent from "@testing-library/user-event";
|
|
import userEvent from "@testing-library/user-event";
|
|
|
|
|
|
const defaultMessage = "message";
|
|
const defaultMessage = "message";
|
|
-const defaultNotifications: NotificationMessage[] = [{ atype: "success", message: defaultMessage, system: true, duration: 3000 }];
|
|
|
|
-const getNotificationsWithType = (aType: string) => [{ ...defaultNotifications[0], atype: aType }];
|
|
|
|
|
|
+const defaultNotifications: NotificationMessage[] = [
|
|
|
|
+ { nType: "success", message: defaultMessage, system: true, duration: 3000, snackbarId: "nId" },
|
|
|
|
+];
|
|
|
|
+const getNotificationsWithType = (nType: string) => [{ ...defaultNotifications[0], nType }];
|
|
|
|
|
|
class myNotification {
|
|
class myNotification {
|
|
static requestPermission = jest.fn(() => Promise.resolve("granted"));
|
|
static requestPermission = jest.fn(() => Promise.resolve("granted"));
|
|
static permission = "granted";
|
|
static permission = "granted";
|
|
}
|
|
}
|
|
|
|
|
|
-describe("Alert Component", () => {
|
|
|
|
|
|
+describe("Notifications", () => {
|
|
beforeAll(() => {
|
|
beforeAll(() => {
|
|
globalThis.Notification = myNotification as unknown as jest.Mocked<typeof Notification>;
|
|
globalThis.Notification = myNotification as unknown as jest.Mocked<typeof Notification>;
|
|
});
|
|
});
|
|
@@ -40,43 +42,43 @@ describe("Alert Component", () => {
|
|
const { getByText } = render(
|
|
const { getByText } = render(
|
|
<SnackbarProvider>
|
|
<SnackbarProvider>
|
|
<TaipyNotification notifications={defaultNotifications} />
|
|
<TaipyNotification notifications={defaultNotifications} />
|
|
- </SnackbarProvider>,
|
|
|
|
|
|
+ </SnackbarProvider>
|
|
);
|
|
);
|
|
const elt = getByText(defaultMessage);
|
|
const elt = getByText(defaultMessage);
|
|
expect(elt.tagName).toBe("DIV");
|
|
expect(elt.tagName).toBe("DIV");
|
|
});
|
|
});
|
|
- it("displays a success alert", async () => {
|
|
|
|
|
|
+ it("displays a success notification", async () => {
|
|
const { getByText } = render(
|
|
const { getByText } = render(
|
|
<SnackbarProvider>
|
|
<SnackbarProvider>
|
|
<TaipyNotification notifications={defaultNotifications} />
|
|
<TaipyNotification notifications={defaultNotifications} />
|
|
- </SnackbarProvider>,
|
|
|
|
|
|
+ </SnackbarProvider>
|
|
);
|
|
);
|
|
const elt = getByText(defaultMessage);
|
|
const elt = getByText(defaultMessage);
|
|
expect(elt.closest(".notistack-MuiContent-success")).toBeInTheDocument();
|
|
expect(elt.closest(".notistack-MuiContent-success")).toBeInTheDocument();
|
|
});
|
|
});
|
|
- it("displays an error alert", async () => {
|
|
|
|
|
|
+ it("displays an error notification", async () => {
|
|
const { getByText } = render(
|
|
const { getByText } = render(
|
|
<SnackbarProvider>
|
|
<SnackbarProvider>
|
|
<TaipyNotification notifications={getNotificationsWithType("error")} />
|
|
<TaipyNotification notifications={getNotificationsWithType("error")} />
|
|
- </SnackbarProvider>,
|
|
|
|
|
|
+ </SnackbarProvider>
|
|
);
|
|
);
|
|
const elt = getByText(defaultMessage);
|
|
const elt = getByText(defaultMessage);
|
|
expect(elt.closest(".notistack-MuiContent-error")).toBeInTheDocument();
|
|
expect(elt.closest(".notistack-MuiContent-error")).toBeInTheDocument();
|
|
});
|
|
});
|
|
- it("displays a warning alert", async () => {
|
|
|
|
|
|
+ it("displays a warning notification", async () => {
|
|
const { getByText } = render(
|
|
const { getByText } = render(
|
|
<SnackbarProvider>
|
|
<SnackbarProvider>
|
|
<TaipyNotification notifications={getNotificationsWithType("warning")} />
|
|
<TaipyNotification notifications={getNotificationsWithType("warning")} />
|
|
- </SnackbarProvider>,
|
|
|
|
|
|
+ </SnackbarProvider>
|
|
);
|
|
);
|
|
const elt = getByText(defaultMessage);
|
|
const elt = getByText(defaultMessage);
|
|
expect(elt.closest(".notistack-MuiContent-warning")).toBeInTheDocument();
|
|
expect(elt.closest(".notistack-MuiContent-warning")).toBeInTheDocument();
|
|
});
|
|
});
|
|
- it("displays an info alert", async () => {
|
|
|
|
|
|
+ it("displays an info notification", async () => {
|
|
const { getByText } = render(
|
|
const { getByText } = render(
|
|
<SnackbarProvider>
|
|
<SnackbarProvider>
|
|
<TaipyNotification notifications={getNotificationsWithType("info")} />
|
|
<TaipyNotification notifications={getNotificationsWithType("info")} />
|
|
- </SnackbarProvider>,
|
|
|
|
|
|
+ </SnackbarProvider>
|
|
);
|
|
);
|
|
const elt = getByText(defaultMessage);
|
|
const elt = getByText(defaultMessage);
|
|
expect(elt.closest(".notistack-MuiContent-info")).toBeInTheDocument();
|
|
expect(elt.closest(".notistack-MuiContent-info")).toBeInTheDocument();
|
|
@@ -86,13 +88,19 @@ describe("Alert Component", () => {
|
|
link.rel = "icon";
|
|
link.rel = "icon";
|
|
link.href = "/test-icon.png";
|
|
link.href = "/test-icon.png";
|
|
document.head.appendChild(link);
|
|
document.head.appendChild(link);
|
|
- const alerts: NotificationMessage[] = [
|
|
|
|
- { atype: "success", message: "This is a system alert", system: true, duration: 3000 },
|
|
|
|
|
|
+ const notifications: NotificationMessage[] = [
|
|
|
|
+ {
|
|
|
|
+ nType: "success",
|
|
|
|
+ message: "This is a system notification",
|
|
|
|
+ system: true,
|
|
|
|
+ duration: 3000,
|
|
|
|
+ snackbarId: "nId",
|
|
|
|
+ },
|
|
];
|
|
];
|
|
render(
|
|
render(
|
|
<SnackbarProvider>
|
|
<SnackbarProvider>
|
|
- <TaipyNotification notifications={alerts} />
|
|
|
|
- </SnackbarProvider>,
|
|
|
|
|
|
+ <TaipyNotification notifications={notifications} />
|
|
|
|
+ </SnackbarProvider>
|
|
);
|
|
);
|
|
const linkElement = document.querySelector("link[rel='icon']");
|
|
const linkElement = document.querySelector("link[rel='icon']");
|
|
if (linkElement) {
|
|
if (linkElement) {
|
|
@@ -103,46 +111,56 @@ describe("Alert Component", () => {
|
|
document.head.removeChild(link);
|
|
document.head.removeChild(link);
|
|
});
|
|
});
|
|
|
|
|
|
- it("closes alert on close button click", async () => {
|
|
|
|
- const alerts = [{ atype: "success", message: "Test Alert", duration: 3000, system: false }];
|
|
|
|
|
|
+ it("closes notification on close button click", async () => {
|
|
|
|
+ const notifications = [
|
|
|
|
+ { nType: "success", message: "Test Notification", duration: 3000, system: false, snackbarId: "nId" },
|
|
|
|
+ ];
|
|
render(
|
|
render(
|
|
<SnackbarProvider>
|
|
<SnackbarProvider>
|
|
- <TaipyNotification notifications={alerts} />
|
|
|
|
- </SnackbarProvider>,
|
|
|
|
|
|
+ <TaipyNotification notifications={notifications} />
|
|
|
|
+ </SnackbarProvider>
|
|
);
|
|
);
|
|
const closeButton = await screen.findByRole("button", { name: /close/i });
|
|
const closeButton = await screen.findByRole("button", { name: /close/i });
|
|
await userEvent.click(closeButton);
|
|
await userEvent.click(closeButton);
|
|
await waitFor(() => {
|
|
await waitFor(() => {
|
|
- const alertMessage = screen.queryByText("Test Alert");
|
|
|
|
- expect(alertMessage).not.toBeInTheDocument();
|
|
|
|
|
|
+ const notificationMessage = screen.queryByText("Test Notification");
|
|
|
|
+ expect(notificationMessage).not.toBeInTheDocument();
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
|
|
- it("Alert disappears when alert type is empty", async () => {
|
|
|
|
- const alerts = [{ atype: "success", message: "Test Alert", duration: 3000, system: false, notificationId: "aNotificationId" }];
|
|
|
|
|
|
+ it("Notification disappears when notification type is empty", async () => {
|
|
|
|
+ const baseNotification = {
|
|
|
|
+ nType: "success",
|
|
|
|
+ message: "Test Notification",
|
|
|
|
+ duration: 3000,
|
|
|
|
+ system: false,
|
|
|
|
+ notificationId: "nId",
|
|
|
|
+ snackbarId: "nId",
|
|
|
|
+ };
|
|
|
|
+ const notifications = [ baseNotification ];
|
|
const { rerender } = render(
|
|
const { rerender } = render(
|
|
<SnackbarProvider>
|
|
<SnackbarProvider>
|
|
- <TaipyNotification notifications={alerts} />
|
|
|
|
- </SnackbarProvider>,
|
|
|
|
|
|
+ <TaipyNotification notifications={notifications} />
|
|
|
|
+ </SnackbarProvider>
|
|
);
|
|
);
|
|
await screen.findByRole("button", { name: /close/i });
|
|
await screen.findByRole("button", { name: /close/i });
|
|
- const newAlerts = [{ atype: "", message: "Test Alert", duration: 3000, system: false, notificationId: "aNotificationId" }];
|
|
|
|
|
|
+ const newNotifications = [ { ...baseNotification, nType: "" }];
|
|
rerender(
|
|
rerender(
|
|
<SnackbarProvider>
|
|
<SnackbarProvider>
|
|
- <TaipyNotification notifications={newAlerts} />
|
|
|
|
- </SnackbarProvider>,
|
|
|
|
|
|
+ <TaipyNotification notifications={newNotifications} />
|
|
|
|
+ </SnackbarProvider>
|
|
);
|
|
);
|
|
await waitFor(() => {
|
|
await waitFor(() => {
|
|
- const alertMessage = screen.queryByText("Test Alert");
|
|
|
|
- expect(alertMessage).not.toBeInTheDocument();
|
|
|
|
|
|
+ const notificationMessage = screen.queryByText("Test Notification");
|
|
|
|
+ expect(notificationMessage).not.toBeInTheDocument();
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
|
|
- it("does nothing when alert is undefined", async () => {
|
|
|
|
|
|
+ it("does nothing when notification is undefined", async () => {
|
|
render(
|
|
render(
|
|
<SnackbarProvider>
|
|
<SnackbarProvider>
|
|
<TaipyNotification notifications={[]} />
|
|
<TaipyNotification notifications={[]} />
|
|
- </SnackbarProvider>,
|
|
|
|
|
|
+ </SnackbarProvider>
|
|
);
|
|
);
|
|
expect(Notification.requestPermission).not.toHaveBeenCalled();
|
|
expect(Notification.requestPermission).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
@@ -152,13 +170,19 @@ describe("Alert Component", () => {
|
|
link.rel = "icon";
|
|
link.rel = "icon";
|
|
link.href = "/test-icon.png";
|
|
link.href = "/test-icon.png";
|
|
document.head.appendChild(link);
|
|
document.head.appendChild(link);
|
|
- const alerts: NotificationMessage[] = [
|
|
|
|
- { atype: "success", message: "This is a system alert", system: true, duration: 3000 },
|
|
|
|
|
|
+ const notifications: NotificationMessage[] = [
|
|
|
|
+ {
|
|
|
|
+ nType: "success",
|
|
|
|
+ message: "This is a system notification",
|
|
|
|
+ system: true,
|
|
|
|
+ duration: 3000,
|
|
|
|
+ snackbarId: "nId",
|
|
|
|
+ },
|
|
];
|
|
];
|
|
render(
|
|
render(
|
|
<SnackbarProvider>
|
|
<SnackbarProvider>
|
|
- <TaipyNotification notifications={alerts} />
|
|
|
|
- </SnackbarProvider>,
|
|
|
|
|
|
+ <TaipyNotification notifications={notifications} />
|
|
|
|
+ </SnackbarProvider>
|
|
);
|
|
);
|
|
const linkElement = document.querySelector("link[rel='icon']");
|
|
const linkElement = document.querySelector("link[rel='icon']");
|
|
expect(linkElement?.getAttribute("href")).toBe("/test-icon.png");
|
|
expect(linkElement?.getAttribute("href")).toBe("/test-icon.png");
|
|
@@ -169,13 +193,19 @@ describe("Alert Component", () => {
|
|
const link = document.createElement("link");
|
|
const link = document.createElement("link");
|
|
link.rel = "icon";
|
|
link.rel = "icon";
|
|
document.head.appendChild(link);
|
|
document.head.appendChild(link);
|
|
- const alerts: NotificationMessage[] = [
|
|
|
|
- { atype: "success", message: "This is a system alert", system: true, duration: 3000 },
|
|
|
|
|
|
+ const notifications: NotificationMessage[] = [
|
|
|
|
+ {
|
|
|
|
+ nType: "success",
|
|
|
|
+ message: "This is a system notification",
|
|
|
|
+ system: true,
|
|
|
|
+ duration: 3000,
|
|
|
|
+ snackbarId: "nId",
|
|
|
|
+ },
|
|
];
|
|
];
|
|
render(
|
|
render(
|
|
<SnackbarProvider>
|
|
<SnackbarProvider>
|
|
- <TaipyNotification notifications={alerts} />
|
|
|
|
- </SnackbarProvider>,
|
|
|
|
|
|
+ <TaipyNotification notifications={notifications} />
|
|
|
|
+ </SnackbarProvider>
|
|
);
|
|
);
|
|
const linkElement = document.querySelector("link[rel='icon']");
|
|
const linkElement = document.querySelector("link[rel='icon']");
|
|
expect(linkElement?.getAttribute("href") || "/favicon.png").toBe("/favicon.png");
|
|
expect(linkElement?.getAttribute("href") || "/favicon.png").toBe("/favicon.png");
|
|
@@ -187,13 +217,19 @@ describe("Alert Component", () => {
|
|
link.rel = "shortcut icon";
|
|
link.rel = "shortcut icon";
|
|
link.href = "/test-shortcut-icon.png";
|
|
link.href = "/test-shortcut-icon.png";
|
|
document.head.appendChild(link);
|
|
document.head.appendChild(link);
|
|
- const alerts: NotificationMessage[] = [
|
|
|
|
- { atype: "success", message: "This is a system alert", system: true, duration: 3000 },
|
|
|
|
|
|
+ const notifications: NotificationMessage[] = [
|
|
|
|
+ {
|
|
|
|
+ nType: "success",
|
|
|
|
+ message: "This is a system notification",
|
|
|
|
+ system: true,
|
|
|
|
+ duration: 3000,
|
|
|
|
+ snackbarId: "nId",
|
|
|
|
+ },
|
|
];
|
|
];
|
|
render(
|
|
render(
|
|
<SnackbarProvider>
|
|
<SnackbarProvider>
|
|
- <TaipyNotification notifications={alerts} />
|
|
|
|
- </SnackbarProvider>,
|
|
|
|
|
|
+ <TaipyNotification notifications={notifications} />
|
|
|
|
+ </SnackbarProvider>
|
|
);
|
|
);
|
|
const linkElement = document.querySelector("link[rel='shortcut icon']");
|
|
const linkElement = document.querySelector("link[rel='shortcut icon']");
|
|
expect(linkElement?.getAttribute("href")).toBe("/test-shortcut-icon.png");
|
|
expect(linkElement?.getAttribute("href")).toBe("/test-shortcut-icon.png");
|