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

56 lines
1.7 KiB
TypeScript
Raw Normal View History

2019-02-18 15:34:23 +01:00
import { NotificationManager } from "../../src/ts/store/notifications";
2019-01-29 14:15:36 +01:00
test("can subscribe and add notification", () => {
let n = 0;
2019-01-29 14:15:36 +01:00
const notifications = new NotificationManager();
notifications.on("notification_added", null, () => n++);
notifications.on("notification_closed", null, () => n--);
expect(n).toBe(0);
2019-01-29 20:42:05 +01:00
const id = notifications.add({ title: "test", message: "message" });
expect(n).toBe(1);
2019-01-29 14:15:36 +01:00
expect(id).toBeDefined();
});
test("can close a notification", () => {
let n = 0;
2019-01-29 20:42:05 +01:00
const notifications = new NotificationManager();
notifications.on("notification_added", null, () => n++);
notifications.on("notification_closed", null, () => 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" });
expect(n).toBe(1);
2019-01-29 14:15:36 +01:00
notifications.close(id);
expect(n).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();
let n = 0;
2019-01-29 20:42:05 +01:00
const notifications = new NotificationManager();
notifications.on("notification_added", null, () => n++);
notifications.on("notification_closed", null, () => 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);
expect(n).toBe(1);
2019-01-29 16:13:05 +01:00
jest.runAllTimers();
expect(n).toBe(0);
2019-01-29 16:13:05 +01:00
});
test("sticky notifications do not close themselves after a while", () => {
jest.useFakeTimers();
let n = 0;
2019-01-29 20:42:05 +01:00
const notifications = new NotificationManager();
notifications.on("notification_added", null, () => n++);
notifications.on("notification_closed", null, () => 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);
expect(n).toBe(1);
2019-01-29 16:13:05 +01:00
jest.runAllTimers();
expect(n).toBe(1);
2019-01-29 16:13:05 +01:00
});