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

52 lines
1.6 KiB
TypeScript
Raw Normal View History

2019-01-29 20:42:05 +01:00
import { NotificationManager } from "../../src/ts/services/notifications";
2019-01-29 14:15:36 +01:00
test("can subscribe and add notification", () => {
let notified = false;
const notifications = new NotificationManager();
notifications.on("notification_added", {}, () => (notified = true));
expect(notified).toBe(false);
2019-01-29 20:42:05 +01:00
const id = notifications.add({ title: "test", message: "message" });
2019-01-29 14:15:36 +01:00
expect(notified).toBe(true);
expect(id).toBeDefined();
});
test("can close a notification", () => {
2019-01-29 20:42:05 +01:00
const notifications = new NotificationManager();
2019-01-29 14:15:36 +01:00
let notified = false;
2019-01-29 16:13:05 +01:00
notifications.on("notification_removed", {}, () => (notified = true));
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-29 14:15:36 +01:00
expect(notified).toBe(false);
notifications.close(id);
expect(notified).toBe(true);
});
2019-01-29 16:13:05 +01:00
test("notifications closes themselves after a while", () => {
jest.useFakeTimers();
2019-01-29 20:42:05 +01:00
const notifications = new NotificationManager();
2019-01-29 16:13:05 +01:00
let removed = false;
notifications.on("notification_removed", {}, () => (removed = true));
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(removed).toBe(false);
jest.runAllTimers();
expect(removed).toBe(true);
});
test("sticky notifications do not close themselves after a while", () => {
jest.useFakeTimers();
2019-01-29 20:42:05 +01:00
const notifications = new NotificationManager();
2019-01-29 16:13:05 +01:00
let removed = false;
notifications.on("notification_removed", {}, () => (removed = true));
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(removed).toBe(false);
jest.runAllTimers();
expect(removed).toBe(false);
});