@payfit/unity-components
Version:
65 lines (64 loc) • 3.1 kB
JavaScript
import { expect as e, userEvent as t, within as n } from "storybook/test";
//#region src/components/toast/test-utils.ts
var r = (r, i) => {
let a = () => r.canvasElement.querySelector("#_rht_toaster"), o = 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;
}, s = async (e) => {
let t = a();
if (!t) throw Error("No toast container found on the page");
let { errorToasts: r, statusToasts: i } = await o(() => ({
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];
}, c = async (e) => {
let t = (await s(e)).at(0);
if (!t) throw Error(`Toast with variant: ${e} not found in the page`);
return t;
}, l = async () => {
let e = await s();
await Promise.all(e.map((e) => t.click(n(e).getByRole("button", { name: i.formatMessage({ id: "unity:component:common:close:label" }) }))));
}, u = async ({ variant: e, toast: a }) => {
await r.step("Close toast", async () => {
let r = a ?? await c(e);
await t.click(n(r).getByRole("button", { name: i.formatMessage({ id: "unity:component:common:close:label" }) }), { delay: 100 });
});
};
return {
findToast: c,
findAllToasts: s,
getToastContainer: a,
closeAllToasts: l,
closeToast: u,
triggerToastAction: async ({ variant: e, action: i, closeAtEnd: a = !1 }) => {
let o = await c(e);
await r.step("Trigger the action of the toast", async () => {
await t.click(n(o).getByRole("button", { name: i }));
}), a && await u({ variant: e });
},
assertTextExistsInToast: async ({ variant: t, content: i, title: a, closeAtEnd: o = !1 }) => {
let s = await c(t);
a && await r.step(`Check if "${a}" is in the toast`, async () => {
await e(n(s).getByRole("heading", { name: a })).toBeInTheDocument();
}), i && await r.step(`Check if "${i}" is in the toast`, async () => {
await e(n(s).getByText(i)).toBeInTheDocument();
}), o && await u({ variant: t });
},
assertToastNotInDocument: async ({ variant: t, title: r, content: i }) => {
let a = (await s(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 { r as getTestingUtilsToast };