@payfit/unity-components
Version:
65 lines (64 loc) • 3.06 kB
JavaScript
import { expect as e, userEvent as t, within as n } from "storybook/test";
//#region src/core/components/toast/test-utils.ts
var r = (e, t) => t ?? e.i18n.t((e) => e.unity.component.common.close.label), i = (i, a) => {
let o = (e) => n(e).getByRole("button", { name: r(i, a) }), s = () => i.canvasElement.querySelector("[data-rht-toaster]"), c = async (e, t, { timeoutMs: n = 2e3, pollMs: r = 50 } = {}) => {
let i = await e(), a = Math.min(200, Math.max(1, Math.ceil(n / Math.max(1, r)))), o = () => new Promise((e) => {
let t = !1, n = () => {
t || (t = !0, e());
};
setTimeout(n, r), typeof window < "u" && typeof window.requestAnimationFrame == "function" && window.requestAnimationFrame(n), typeof setImmediate == "function" && setImmediate(n);
});
for (let n = 0; n < a && !t(i); n++) await o(), i = await e();
return i;
}, l = async (e) => {
let t = s();
if (!t) throw Error("No toast container found on the page");
let { errorToasts: r, statusToasts: i } = await c(() => ({
errorToasts: n(t).queryAllByRole("alert").filter((e) => e.getAttribute("aria-live") === "assertive"),
statusToasts: n(t).queryAllByRole("status").filter((e) => e.getAttribute("aria-live") === "polite")
}), (t) => e ? e === "error" ? t.errorToasts.length > 0 : t.statusToasts.length > 0 : t.errorToasts.length > 0 || t.statusToasts.length > 0);
return e ? e === "error" ? r : i : [...r, ...i];
}, u = async (e) => {
let t = (await l(e)).at(0);
if (!t) throw Error(`Toast with variant: ${e} not found in the page`);
return t;
}, d = async () => {
let e = await l();
await Promise.all(e.map((e) => t.click(o(e))));
}, f = async ({ variant: e, toast: n }) => {
await i.step("Close toast", async () => {
let r = n ?? await u(e);
await t.click(o(r), { delay: 100 });
});
};
return {
findToast: u,
findAllToasts: l,
getToastContainer: s,
closeAllToasts: d,
closeToast: f,
triggerToastAction: async ({ variant: e, action: r, closeAtEnd: a = !1 }) => {
let o = await u(e);
await i.step("Trigger the action of the toast", async () => {
await t.click(n(o).getByRole("button", { name: r }));
}), a && await f({ variant: e });
},
assertTextExistsInToast: async ({ variant: t, content: r, title: a, closeAtEnd: o = !1 }) => {
let s = await u(t);
a && await i.step(`Check if "${a}" is in the toast`, async () => {
await e(n(s).getByRole("heading", { name: a })).toBeInTheDocument();
}), r && await i.step(`Check if "${r}" is in the toast`, async () => {
await e(n(s).getByText(r)).toBeInTheDocument();
}), o && await f({ variant: t });
},
assertToastNotInDocument: async ({ variant: t, title: r, content: i }) => {
let a = (await l(t)).filter((e) => r && n(e).queryByText(r) && i && n(e).queryByText(i));
for (let n of a) {
let a = `Toast with variant: ${t}`;
r && (a += ` and title: ${r}`), i && (a += ` and content: ${i}`), await e(n, `${a} not in the document`).not.toBeInTheDocument();
}
}
};
};
//#endregion
export { i as getTestingUtilsToast };