display notifications instantly (imperative way)

This commit is contained in:
Géry Debongnie
2019-02-18 11:51:01 +01:00
parent efc5e3a1d4
commit 5b68b396ad
6 changed files with 60 additions and 60 deletions
+21 -20
View File
@@ -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);
});