mirror of
https://github.com/vector-im/element-web.git
synced 2024-11-28 11:28:12 +08:00
c05c429803
Co-authored-by: github-merge-queue <118344674+github-merge-queue@users.noreply.github.com> Co-authored-by: github-merge-queue <github-merge-queue@users.noreply.github.com> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Florian Duros <florian.duros@ormaz.fr> Co-authored-by: Kim Brose <kim.brose@nordeck.net> Co-authored-by: Florian Duros <florianduros@element.io> Co-authored-by: R Midhun Suresh <hi@midhun.dev> Co-authored-by: dbkr <986903+dbkr@users.noreply.github.com> Co-authored-by: ElementRobot <releases@riot.im> Co-authored-by: dbkr <dbkr@users.noreply.github.com> Co-authored-by: David Baker <dbkr@users.noreply.github.com> Co-authored-by: Michael Telatynski <7t3chguy@gmail.com> Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> Co-authored-by: David Langley <davidl@element.io> Co-authored-by: Michael Weimann <michaelw@matrix.org> Co-authored-by: Timshel <Timshel@users.noreply.github.com> Co-authored-by: Sahil Silare <32628578+sahil9001@users.noreply.github.com> Co-authored-by: Will Hunt <will@half-shot.uk> Co-authored-by: Hubert Chathi <hubert@uhoreg.ca> Co-authored-by: Andrew Ferrazzutti <andrewf@element.io> Co-authored-by: Robin <robin@robin.town> Co-authored-by: Tulir Asokan <tulir@maunium.net>
139 lines
5.0 KiB
TypeScript
139 lines
5.0 KiB
TypeScript
/*
|
|
Copyright 2024 New Vector Ltd.
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
Please see LICENSE files in the repository root for full details.
|
|
*/
|
|
|
|
import GenericToast from "../../../src/components/views/toasts/GenericToast";
|
|
import ToastStore, { IToast } from "../../../src/stores/ToastStore";
|
|
|
|
describe("ToastStore", () => {
|
|
const makeToast = (priority: number, key?: string): IToast<typeof GenericToast> => ({
|
|
key: key ?? `toast-${priority}`,
|
|
title: "Test toast",
|
|
component: GenericToast,
|
|
priority,
|
|
});
|
|
|
|
it("sets instance on window when doesnt exist", () => {
|
|
const sharedInstance = ToastStore.sharedInstance();
|
|
expect(sharedInstance).toBeTruthy();
|
|
// using same instance
|
|
expect(ToastStore.sharedInstance()).toBe(sharedInstance);
|
|
});
|
|
|
|
describe("addOrReplaceToast()", () => {
|
|
it("adds a toast to an empty store", () => {
|
|
const toast = makeToast(1);
|
|
const store = new ToastStore();
|
|
const emitSpy = jest.spyOn(store, "emit");
|
|
store.addOrReplaceToast(toast);
|
|
expect(emitSpy).toHaveBeenCalledWith("update");
|
|
expect(store.getToasts()).toEqual([toast]);
|
|
});
|
|
|
|
it("inserts toast according to priority", () => {
|
|
const toast1 = makeToast(1);
|
|
const toast3a = makeToast(3, "a");
|
|
const toast3b = makeToast(3, "b");
|
|
const toast99 = makeToast(99);
|
|
const store = new ToastStore();
|
|
store.addOrReplaceToast(toast1);
|
|
store.addOrReplaceToast(toast99);
|
|
store.addOrReplaceToast(toast3a);
|
|
store.addOrReplaceToast(toast3b);
|
|
// ascending order, newest toast of given priority first
|
|
expect(store.getToasts()).toEqual([toast99, toast3a, toast3b, toast1]);
|
|
});
|
|
|
|
it("replaces toasts by key without changing order", () => {
|
|
const toastA = makeToast(1, "a");
|
|
const toastB = makeToast(3, "b");
|
|
const toastC = makeToast(99, "c");
|
|
const store = new ToastStore();
|
|
store.addOrReplaceToast(toastA);
|
|
store.addOrReplaceToast(toastC);
|
|
store.addOrReplaceToast(toastB);
|
|
expect(store.getToasts()).toEqual([toastC, toastB, toastA]);
|
|
|
|
const toastBNew = makeToast(5, "b");
|
|
store.addOrReplaceToast(toastBNew);
|
|
expect(store.getToasts()).toEqual([toastC, toastBNew, toastA]);
|
|
});
|
|
});
|
|
|
|
describe("dismissToast()", () => {
|
|
it("does nothing when there are no toasts", () => {
|
|
const store = new ToastStore();
|
|
const emitSpy = jest.spyOn(store, "emit");
|
|
|
|
store.dismissToast("whatever");
|
|
|
|
expect(store.getCountSeen()).toEqual(0);
|
|
expect(emitSpy).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("removes toast and emits", () => {
|
|
const store = new ToastStore();
|
|
const toastA = makeToast(1, "a");
|
|
const toastB = makeToast(3, "b");
|
|
store.addOrReplaceToast(toastA);
|
|
store.addOrReplaceToast(toastB);
|
|
const emitSpy = jest.spyOn(store, "emit");
|
|
|
|
store.dismissToast(toastA.key);
|
|
|
|
expect(store.getCountSeen()).toEqual(0);
|
|
expect(emitSpy).toHaveBeenCalledWith("update");
|
|
expect(store.getToasts()).toEqual([toastB]);
|
|
});
|
|
|
|
it("increments countSeen when toast has bottom priority", () => {
|
|
const store = new ToastStore();
|
|
const toastA = makeToast(1, "a");
|
|
const toastB = makeToast(3, "b");
|
|
const toastC = makeToast(99, "c");
|
|
store.addOrReplaceToast(toastA);
|
|
store.addOrReplaceToast(toastC);
|
|
store.addOrReplaceToast(toastB);
|
|
const emitSpy = jest.spyOn(store, "emit");
|
|
|
|
store.dismissToast(toastC.key);
|
|
|
|
expect(store.getCountSeen()).toEqual(1);
|
|
expect(emitSpy).toHaveBeenCalledWith("update");
|
|
});
|
|
|
|
it("resets countSeen when no toasts remain", () => {
|
|
const store = new ToastStore();
|
|
const toastA = makeToast(1, "a");
|
|
const toastB = makeToast(3, "b");
|
|
store.addOrReplaceToast(toastA);
|
|
store.addOrReplaceToast(toastB);
|
|
|
|
store.dismissToast(toastB.key);
|
|
expect(store.getCountSeen()).toEqual(1);
|
|
store.dismissToast(toastA.key);
|
|
expect(store.getCountSeen()).toEqual(0);
|
|
});
|
|
});
|
|
|
|
describe("reset()", () => {
|
|
it("clears countseen and toasts", () => {
|
|
const store = new ToastStore();
|
|
const toastA = makeToast(1, "a");
|
|
const toastB = makeToast(3, "b");
|
|
store.addOrReplaceToast(toastA);
|
|
store.addOrReplaceToast(toastB);
|
|
// increment count seen
|
|
store.dismissToast(toastB.key);
|
|
|
|
store.reset();
|
|
expect(store.getCountSeen()).toEqual(0);
|
|
expect(store.getToasts()).toEqual([]);
|
|
});
|
|
});
|
|
});
|