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", () => {
|
2019-02-18 11:51:01 +01:00
|
|
|
let n = 0;
|
2019-01-29 14:15:36 +01:00
|
|
|
const notifications = new NotificationManager();
|
2019-02-18 11:51:01 +01:00
|
|
|
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" });
|
2019-02-18 11:51:01 +01:00
|
|
|
expect(n).toBe(1);
|
2019-01-29 14:15:36 +01:00
|
|
|
expect(id).toBeDefined();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("can close a notification", () => {
|
2019-02-18 11:51:01 +01:00
|
|
|
let n = 0;
|
2019-01-29 20:42:05 +01:00
|
|
|
const notifications = new NotificationManager();
|
2019-02-18 11:51:01 +01:00
|
|
|
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" });
|
2019-02-18 11:51:01 +01:00
|
|
|
expect(n).toBe(1);
|
2019-01-29 14:15:36 +01:00
|
|
|
|
|
|
|
|
notifications.close(id);
|
2019-02-18 11:51:01 +01:00
|
|
|
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();
|
2019-02-18 11:51:01 +01:00
|
|
|
let n = 0;
|
2019-01-29 20:42:05 +01:00
|
|
|
const notifications = new NotificationManager();
|
2019-02-18 11:51:01 +01:00
|
|
|
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);
|
2019-02-18 11:51:01 +01:00
|
|
|
expect(n).toBe(1);
|
2019-01-29 16:13:05 +01:00
|
|
|
jest.runAllTimers();
|
2019-02-18 11:51:01 +01:00
|
|
|
expect(n).toBe(0);
|
2019-01-29 16:13:05 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("sticky notifications do not close themselves after a while", () => {
|
|
|
|
|
jest.useFakeTimers();
|
2019-02-18 11:51:01 +01:00
|
|
|
let n = 0;
|
2019-01-29 20:42:05 +01:00
|
|
|
const notifications = new NotificationManager();
|
2019-02-18 11:51:01 +01:00
|
|
|
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);
|
2019-02-18 11:51:01 +01:00
|
|
|
expect(n).toBe(1);
|
2019-01-29 16:13:05 +01:00
|
|
|
jest.runAllTimers();
|
2019-02-18 11:51:01 +01:00
|
|
|
expect(n).toBe(1);
|
2019-01-29 16:13:05 +01:00
|
|
|
});
|