How can I test that the Lightbox component is opening and closing correctly? #306
-
I have a conditional button that will open the light box upon click.
Test:
|
Beta Was this translation helpful? Give feedback.
Answered by
igordanchenko
Sep 20, 2024
Replies: 1 comment 3 replies
-
Here is a working example: import { expect, test, vi } from "vitest";
import { act, render } from "@testing-library/react";
import App from "./App.tsx";
test("App test", () => {
vi.useFakeTimers();
try {
const { queryByLabelText, getByLabelText, getByText } = render(<App />);
act(() => {
getByText("Open").click();
});
expect(queryByLabelText("Previous")).toBeInTheDocument();
expect(queryByLabelText("Next")).toBeInTheDocument();
expect(queryByLabelText("Close")).toBeInTheDocument();
act(() => {
getByLabelText("Close").click();
vi.runAllTimers();
});
expect(queryByLabelText("Previous")).not.toBeInTheDocument();
expect(queryByLabelText("Next")).not.toBeInTheDocument();
expect(queryByLabelText("Close")).not.toBeInTheDocument();
} finally {
vi.useRealTimers();
}
}); |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
igordanchenko
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is a working example: