2019-06-26 09:59:24 -06:00
|
|
|
jest.mock("../toast_internal_support", () => {
|
|
|
|
return {
|
|
|
|
createToast: jest.fn(),
|
|
|
|
createToastOnce: jest.fn()
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
import { createToastOnce, createToast } from "../toast_internal_support";
|
|
|
|
|
|
|
|
const {
|
|
|
|
warning,
|
|
|
|
error,
|
|
|
|
success,
|
|
|
|
info,
|
|
|
|
fun,
|
|
|
|
init,
|
2020-04-20 15:07:40 -06:00
|
|
|
removeToast,
|
2019-07-15 17:33:45 -06:00
|
|
|
busy,
|
2019-06-26 09:59:24 -06:00
|
|
|
}: typeof import("../toast") = jest.requireActual("../toast");
|
|
|
|
|
|
|
|
describe("toasts", () => {
|
|
|
|
it("pops a warning() toast", () => {
|
|
|
|
warning("test suite msg 1");
|
2020-04-20 15:07:40 -06:00
|
|
|
expect(createToastOnce).toHaveBeenCalledWith(
|
|
|
|
"test suite msg 1", "Warning", "orange", "", console.warn);
|
2019-06-26 09:59:24 -06:00
|
|
|
});
|
|
|
|
|
2019-08-23 15:18:28 -06:00
|
|
|
it("pops a warning() toast with different title and color", () => {
|
2020-04-20 15:07:40 -06:00
|
|
|
warning("test suite msg", "new title", "purple", "id-prefix");
|
|
|
|
expect(createToastOnce).toHaveBeenCalledWith(
|
|
|
|
"test suite msg", "new title", "purple", "id-prefix", console.warn);
|
2019-08-23 15:18:28 -06:00
|
|
|
});
|
|
|
|
|
2019-06-26 09:59:24 -06:00
|
|
|
it("pops a error() toast", () => {
|
|
|
|
error("test suite msg 2");
|
2020-04-20 15:07:40 -06:00
|
|
|
expect(createToastOnce).toHaveBeenCalledWith(
|
|
|
|
"test suite msg 2", "Error", "red", "", console.error);
|
2019-06-26 09:59:24 -06:00
|
|
|
});
|
|
|
|
|
2019-08-23 15:18:28 -06:00
|
|
|
it("pops a error() toast with different title and color", () => {
|
2020-04-20 15:07:40 -06:00
|
|
|
error("test suite msg", "new title", "purple", "id-prefix");
|
|
|
|
expect(createToastOnce).toHaveBeenCalledWith(
|
|
|
|
"test suite msg", "new title", "purple", "id-prefix", console.error);
|
2019-08-23 15:18:28 -06:00
|
|
|
});
|
|
|
|
|
2019-06-26 09:59:24 -06:00
|
|
|
it("pops a success() toast", () => {
|
|
|
|
success("test suite msg");
|
2020-04-20 15:07:40 -06:00
|
|
|
expect(createToast).toHaveBeenCalledWith(
|
|
|
|
"test suite msg", "Success", "green", "");
|
2019-06-26 09:59:24 -06:00
|
|
|
});
|
|
|
|
|
2019-08-23 15:18:28 -06:00
|
|
|
it("pops a success() toast with different title and color", () => {
|
2020-04-20 15:07:40 -06:00
|
|
|
success("test suite msg", "new title", "purple", "id-prefix");
|
|
|
|
expect(createToast).toHaveBeenCalledWith(
|
|
|
|
"test suite msg", "new title", "purple", "id-prefix");
|
2019-08-23 15:18:28 -06:00
|
|
|
});
|
|
|
|
|
2019-06-26 09:59:24 -06:00
|
|
|
it("pops a info() toast", () => {
|
|
|
|
info("test suite msg");
|
2020-04-20 15:07:40 -06:00
|
|
|
expect(createToast).toHaveBeenCalledWith(
|
|
|
|
"test suite msg", "FYI", "blue", "");
|
2019-06-26 09:59:24 -06:00
|
|
|
});
|
|
|
|
|
2019-08-23 15:18:28 -06:00
|
|
|
it("pops a info() toast with different title and color", () => {
|
2020-04-20 15:07:40 -06:00
|
|
|
info("test suite msg", "new title", "purple", "id-prefix");
|
|
|
|
expect(createToast).toHaveBeenCalledWith(
|
|
|
|
"test suite msg", "new title", "purple", "id-prefix");
|
2019-08-23 15:18:28 -06:00
|
|
|
});
|
|
|
|
|
2019-07-15 17:33:45 -06:00
|
|
|
it("pops a busy() toast", () => {
|
|
|
|
busy("test suite msg");
|
2020-04-20 15:07:40 -06:00
|
|
|
expect(createToast).toHaveBeenCalledWith(
|
|
|
|
"test suite msg", "Busy", "yellow", "");
|
2019-07-15 17:33:45 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
it("pops a busy() toast with different title and color", () => {
|
2020-04-20 15:07:40 -06:00
|
|
|
busy("test suite msg", "new title", "purple", "id-prefix");
|
|
|
|
expect(createToast).toHaveBeenCalledWith(
|
|
|
|
"test suite msg", "new title", "purple", "id-prefix");
|
2019-07-15 17:33:45 -06:00
|
|
|
});
|
|
|
|
|
2019-06-26 09:59:24 -06:00
|
|
|
it("pops a fun() toast", () => {
|
|
|
|
fun("test suite msg");
|
2020-04-20 15:07:40 -06:00
|
|
|
expect(createToast).toHaveBeenCalledWith(
|
|
|
|
"test suite msg", "Did you know?", "dark-blue", "");
|
2019-06-26 09:59:24 -06:00
|
|
|
});
|
|
|
|
|
2019-08-23 15:18:28 -06:00
|
|
|
it("pops a fun() toast with different title and color", () => {
|
2020-04-20 15:07:40 -06:00
|
|
|
fun("test suite msg", "new title", "purple", "id-prefix");
|
|
|
|
expect(createToast).toHaveBeenCalledWith(
|
|
|
|
"test suite msg", "new title", "purple", "id-prefix");
|
2019-08-23 15:18:28 -06:00
|
|
|
});
|
|
|
|
|
2020-04-20 15:07:40 -06:00
|
|
|
const getToastContainerCount = () =>
|
|
|
|
Object.values(document.querySelectorAll(".toast-container")).length;
|
|
|
|
|
|
|
|
const getToastCount = () =>
|
|
|
|
document.querySelector(".toast-container")?.childElementCount;
|
|
|
|
|
2019-06-26 09:59:24 -06:00
|
|
|
it("adds the appropriate div to the DOM", () => {
|
2020-04-20 15:07:40 -06:00
|
|
|
document.body.innerHTML = "";
|
|
|
|
expect(getToastContainerCount()).toEqual(0);
|
2019-06-26 09:59:24 -06:00
|
|
|
init();
|
2020-04-20 15:07:40 -06:00
|
|
|
expect(getToastContainerCount()).toEqual(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("removes a toast message", () => {
|
|
|
|
document.body.innerHTML = "";
|
|
|
|
init();
|
|
|
|
expect(getToastCount()).toEqual(0);
|
|
|
|
const toast = document.createElement("div");
|
|
|
|
toast.id = "id-prefix-123";
|
|
|
|
document.querySelector(".toast-container")?.appendChild(toast);
|
|
|
|
expect(getToastCount()).toEqual(1);
|
|
|
|
removeToast("id-prefix");
|
|
|
|
expect(getToastCount()).toEqual(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("doesn't remove a toast message: parent missing", () => {
|
|
|
|
document.body.innerHTML = "";
|
|
|
|
expect(getToastContainerCount()).toEqual(0);
|
|
|
|
console.error = jest.fn();
|
|
|
|
removeToast("id-prefix");
|
|
|
|
expect(console.error).toHaveBeenCalledWith("toast-container is null.");
|
2019-06-26 09:59:24 -06:00
|
|
|
});
|
|
|
|
});
|