riot-web/test/stores/ToastStore-test.ts

147 lines
5.4 KiB
TypeScript

/*
Copyright 2022 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
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([]);
});
});
});