2024-08-27 21:45:39 +08:00
|
|
|
/*
|
2024-09-06 16:22:13 +08:00
|
|
|
Copyright 2023, 2024 New Vector Ltd.
|
2024-08-27 21:45:39 +08:00
|
|
|
|
2024-09-06 16:22:13 +08:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
Please see LICENSE in the repository root for full details.
|
2024-08-27 21:45:39 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
import { describe, expect, test, vi } from "vitest";
|
2024-09-06 04:23:44 +08:00
|
|
|
import { render } from "@testing-library/react";
|
2024-08-30 21:40:09 +08:00
|
|
|
import userEvent from "@testing-library/user-event";
|
2024-08-27 21:45:39 +08:00
|
|
|
|
|
|
|
import { Toast } from "../src/Toast";
|
|
|
|
import { withFakeTimers } from "./utils/test";
|
|
|
|
|
|
|
|
describe("Toast", () => {
|
|
|
|
test("renders", () => {
|
|
|
|
const { queryByRole } = render(
|
|
|
|
<Toast open={false} onDismiss={() => {}}>
|
|
|
|
Hello world!
|
|
|
|
</Toast>,
|
|
|
|
);
|
|
|
|
expect(queryByRole("dialog")).toBe(null);
|
|
|
|
const { getByRole } = render(
|
|
|
|
<Toast open={true} onDismiss={() => {}}>
|
|
|
|
Hello world!
|
|
|
|
</Toast>,
|
|
|
|
);
|
|
|
|
expect(getByRole("dialog")).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
2024-08-30 21:40:09 +08:00
|
|
|
test("dismisses when Esc is pressed", async () => {
|
2024-09-06 04:23:44 +08:00
|
|
|
const user = userEvent.setup();
|
2024-08-30 21:40:09 +08:00
|
|
|
const onDismiss = vi.fn();
|
2024-09-03 17:04:59 +08:00
|
|
|
render(
|
2024-08-30 21:40:09 +08:00
|
|
|
<Toast open={true} onDismiss={onDismiss}>
|
|
|
|
Hello world!
|
|
|
|
</Toast>,
|
|
|
|
);
|
|
|
|
await user.keyboard("[Escape]");
|
|
|
|
expect(onDismiss).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
test("dismisses when background is clicked", async () => {
|
|
|
|
const user = userEvent.setup();
|
|
|
|
const onDismiss = vi.fn();
|
2024-09-06 04:23:44 +08:00
|
|
|
const { getByRole } = render(
|
2024-08-30 21:40:09 +08:00
|
|
|
<Toast open={true} onDismiss={onDismiss}>
|
|
|
|
Hello world!
|
|
|
|
</Toast>,
|
|
|
|
);
|
|
|
|
const background = getByRole("dialog").previousSibling! as Element;
|
|
|
|
await user.click(background);
|
|
|
|
expect(onDismiss).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2024-08-27 21:45:39 +08:00
|
|
|
test("dismisses itself after the specified timeout", () => {
|
|
|
|
withFakeTimers(() => {
|
|
|
|
const onDismiss = vi.fn();
|
|
|
|
render(
|
|
|
|
<Toast open={true} onDismiss={onDismiss} autoDismiss={2000}>
|
|
|
|
Hello world!
|
|
|
|
</Toast>,
|
|
|
|
);
|
|
|
|
vi.advanceTimersByTime(2000);
|
|
|
|
expect(onDismiss).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|