mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
display notifications instantly (imperative way)
This commit is contained in:
@@ -1,54 +1,55 @@
|
||||
import {
|
||||
NotificationManager,
|
||||
INotification
|
||||
} from "../../src/ts/core/notifications";
|
||||
import { NotificationManager } from "../../src/ts/core/notifications";
|
||||
|
||||
test("can subscribe and add notification", () => {
|
||||
let notifs: INotification[] = [];
|
||||
let n = 0;
|
||||
const notifications = new NotificationManager();
|
||||
notifications.on("notifications_updated", {}, n => (notifs = n));
|
||||
expect(notifs.length).toBe(0);
|
||||
notifications.on("notification_added", null, () => n++);
|
||||
notifications.on("notification_closed", null, () => n--);
|
||||
expect(n).toBe(0);
|
||||
const id = notifications.add({ title: "test", message: "message" });
|
||||
expect(notifs.length).toBe(1);
|
||||
expect(n).toBe(1);
|
||||
expect(id).toBeDefined();
|
||||
});
|
||||
|
||||
test("can close a notification", () => {
|
||||
let notifs: INotification[] = [];
|
||||
let n = 0;
|
||||
const notifications = new NotificationManager();
|
||||
notifications.on("notifications_updated", {}, n => (notifs = n));
|
||||
notifications.on("notification_added", null, () => n++);
|
||||
notifications.on("notification_closed", null, () => n--);
|
||||
|
||||
const id = notifications.add({ title: "test", message: "message" });
|
||||
expect(notifs.length).toBe(1);
|
||||
expect(n).toBe(1);
|
||||
|
||||
notifications.close(id);
|
||||
expect(notifs.length).toBe(0);
|
||||
expect(n).toBe(0);
|
||||
});
|
||||
|
||||
test("notifications closes themselves after a while", () => {
|
||||
jest.useFakeTimers();
|
||||
let notifs: INotification[] = [];
|
||||
let n = 0;
|
||||
const notifications = new NotificationManager();
|
||||
notifications.on("notifications_updated", {}, n => (notifs = n));
|
||||
notifications.on("notification_added", null, () => n++);
|
||||
notifications.on("notification_closed", null, () => n--);
|
||||
|
||||
notifications.add({ title: "test", message: "message" });
|
||||
|
||||
expect(setTimeout).toHaveBeenCalledTimes(1);
|
||||
expect(notifs.length).toBe(1);
|
||||
expect(n).toBe(1);
|
||||
jest.runAllTimers();
|
||||
expect(notifs.length).toBe(0);
|
||||
expect(n).toBe(0);
|
||||
});
|
||||
|
||||
test("sticky notifications do not close themselves after a while", () => {
|
||||
jest.useFakeTimers();
|
||||
let notifs: INotification[] = [];
|
||||
let n = 0;
|
||||
const notifications = new NotificationManager();
|
||||
notifications.on("notifications_updated", {}, n => (notifs = n));
|
||||
notifications.on("notification_added", null, () => n++);
|
||||
notifications.on("notification_closed", null, () => n--);
|
||||
|
||||
notifications.add({ title: "test", message: "message", sticky: true });
|
||||
|
||||
expect(setTimeout).toHaveBeenCalledTimes(0);
|
||||
expect(notifs.length).toBe(1);
|
||||
expect(n).toBe(1);
|
||||
jest.runAllTimers();
|
||||
expect(notifs.length).toBe(1);
|
||||
expect(n).toBe(1);
|
||||
});
|
||||
|
||||
@@ -28,9 +28,7 @@ exports[`can be rendered (in home menu) 1`] = `
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class=\\"o_notification_container\\">
|
||||
|
||||
</div>
|
||||
<div class=\\"o_notification_container\\"></div>
|
||||
<div class=\\"o_loading d-none\\">Loading</div>
|
||||
</div>"
|
||||
`;
|
||||
@@ -104,9 +102,7 @@ exports[`if url has action_id, will render action and navigate to proper menu_id
|
||||
|
||||
<div class=\\"o_content\\"></div>
|
||||
|
||||
<div class=\\"o_notification_container\\">
|
||||
|
||||
</div>
|
||||
<div class=\\"o_notification_container\\"></div>
|
||||
<div class=\\"o_loading\\">Loading</div>
|
||||
</div>"
|
||||
`;
|
||||
@@ -171,9 +167,7 @@ exports[`start with no action => clicks on client action => discuss is rendered
|
||||
<button>Add sticky notif</button>
|
||||
</div></div>
|
||||
|
||||
<div class=\\"o_notification_container\\">
|
||||
|
||||
</div>
|
||||
<div class=\\"o_notification_container\\"></div>
|
||||
<div class=\\"o_loading d-none\\">Loading</div>
|
||||
</div>"
|
||||
`;
|
||||
|
||||
@@ -47,21 +47,23 @@ test("can be rendered", async () => {
|
||||
});
|
||||
|
||||
test("can be closed by clicking on it (if sticky)", async () => {
|
||||
let notifs: INotification[] = [];
|
||||
env.notifications.on(
|
||||
"notifications_updated",
|
||||
null,
|
||||
_notifs => (notifs = _notifs)
|
||||
);
|
||||
let n = 0;
|
||||
let notif;
|
||||
env.notifications.on("notification_added", null, _notif => {
|
||||
n++;
|
||||
notif = _notif;
|
||||
});
|
||||
env.notifications.on("notification_closed", null, () => n--);
|
||||
|
||||
env.notifications.add({
|
||||
title: "title",
|
||||
message: "message",
|
||||
sticky: true
|
||||
});
|
||||
|
||||
const navbar = new Notification(env, notifs[0]);
|
||||
const navbar = new Notification(env, notif);
|
||||
await navbar.mount(fixture);
|
||||
expect(notifs.length).toBe(1);
|
||||
expect(n).toBe(1);
|
||||
(<any>fixture.getElementsByClassName("o_close")[0]).click();
|
||||
expect(notifs.length).toBe(0);
|
||||
expect(n).toBe(0);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user