Files
owl/web/static/tests/services/notifications.test.ts
T

55 lines
1.7 KiB
TypeScript
Raw Normal View History

2019-01-31 13:19:30 +01:00
import {
NotificationManager,
INotification
} from "../../src/ts/services/notifications";
2019-01-29 14:15:36 +01:00
test("can subscribe and add notification", () => {
2019-01-31 13:19:30 +01:00
let notifs: INotification[] = [];
2019-01-29 14:15:36 +01:00
const notifications = new NotificationManager();
2019-01-31 13:19:30 +01:00
notifications.on("notifications_updated", {}, n => (notifs = n));
expect(notifs.length).toBe(0);
2019-01-29 20:42:05 +01:00
const id = notifications.add({ title: "test", message: "message" });
2019-01-31 13:19:30 +01:00
expect(notifs.length).toBe(1);
2019-01-29 14:15:36 +01:00
expect(id).toBeDefined();
});
test("can close a notification", () => {
2019-01-31 13:19:30 +01:00
let notifs: INotification[] = [];
2019-01-29 20:42:05 +01:00
const notifications = new NotificationManager();
2019-01-31 13:19:30 +01:00
notifications.on("notifications_updated", {}, n => (notifs = n));
2019-01-29 14:15:36 +01:00
2019-01-29 20:42:05 +01:00
const id = notifications.add({ title: "test", message: "message" });
2019-01-31 13:19:30 +01:00
expect(notifs.length).toBe(1);
2019-01-29 14:15:36 +01:00
notifications.close(id);
2019-01-31 13:19:30 +01:00
expect(notifs.length).toBe(0);
2019-01-29 14:15:36 +01:00
});
2019-01-29 16:13:05 +01:00
test("notifications closes themselves after a while", () => {
jest.useFakeTimers();
2019-01-31 13:19:30 +01:00
let notifs: INotification[] = [];
2019-01-29 20:42:05 +01:00
const notifications = new NotificationManager();
2019-01-31 13:19:30 +01:00
notifications.on("notifications_updated", {}, n => (notifs = n));
2019-01-29 16:13:05 +01:00
2019-01-29 20:42:05 +01:00
notifications.add({ title: "test", message: "message" });
2019-01-29 16:13:05 +01:00
expect(setTimeout).toHaveBeenCalledTimes(1);
2019-01-31 13:19:30 +01:00
expect(notifs.length).toBe(1);
2019-01-29 16:13:05 +01:00
jest.runAllTimers();
2019-01-31 13:19:30 +01:00
expect(notifs.length).toBe(0);
2019-01-29 16:13:05 +01:00
});
test("sticky notifications do not close themselves after a while", () => {
jest.useFakeTimers();
2019-01-31 13:19:30 +01:00
let notifs: INotification[] = [];
2019-01-29 20:42:05 +01:00
const notifications = new NotificationManager();
2019-01-31 13:19:30 +01:00
notifications.on("notifications_updated", {}, n => (notifs = n));
2019-01-29 16:13:05 +01:00
2019-01-29 20:42:05 +01:00
notifications.add({ title: "test", message: "message", sticky: true });
2019-01-29 16:13:05 +01:00
expect(setTimeout).toHaveBeenCalledTimes(0);
2019-01-31 13:19:30 +01:00
expect(notifs.length).toBe(1);
2019-01-29 16:13:05 +01:00
jest.runAllTimers();
2019-01-31 13:19:30 +01:00
expect(notifs.length).toBe(1);
2019-01-29 16:13:05 +01:00
});