Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve our Dialog component #1052

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 0 additions & 178 deletions src/core/components/Dialog.tsx

This file was deleted.

105 changes: 105 additions & 0 deletions src/core/components/Dialog/Dialog.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
import Dialog from "./Dialog";
import { useState } from "react";
import userEvent from "@testing-library/user-event";
import { mockAnimationsApi } from "jsdom-testing-mocks";
mockAnimationsApi();

describe("Dialog", () => {
it("renders", async () => {
render(
<Dialog open={true} onClose={() => {}}>
<div>Test</div>
</Dialog>,
);
await waitFor(() => {
expect(screen.getByText("Test")).toBeInTheDocument();
});
});

it("closes when clicking outside", async () => {
const user = userEvent.setup();
const Test = () => {
const [open, setOpen] = useState(true);
return (
<>
<button data-testid="btn">Button</button>
<Dialog open={open} onClose={() => setOpen(false)}>
<div>Test</div>
</Dialog>
</>
);
};
render(<Test />);
await waitFor(() => {
expect(screen.getByText("Test")).toBeInTheDocument();
});
await user.click(screen.getByTestId("btn"));
await waitFor(() => {
expect(screen.queryByText("Test")).not.toBeInTheDocument();
});
});

it("does not closes when clicking outside if persistent is true", async () => {
const user = userEvent.setup();
const Test = () => {
const [open, setOpen] = useState(true);
return (
<>
<button data-testid="btn">Button</button>
<Dialog open={open} onClose={() => setOpen(false)} persistent>
<div>Test</div>
</Dialog>
</>
);
};
render(<Test />);
await waitFor(() => {
expect(screen.getByText("Test")).toBeInTheDocument();
});
await user.click(screen.getByTestId("btn"));
await waitFor(() => {
expect(screen.getByText("Test")).toBeInTheDocument();
});
});

it("closes when pressing escape", async () => {
const user = userEvent.setup();
const Test = () => {
const [open, setOpen] = useState(true);
return (
<Dialog open={open} onClose={() => setOpen(false)}>
<Dialog.Content>Content</Dialog.Content>
</Dialog>
);
};
render(<Test />);
await waitFor(() => {
expect(screen.getByText("Content")).toBeInTheDocument();
});
await user.keyboard("{Escape}");
await waitFor(() => {
expect(screen.queryByText("Content")).not.toBeInTheDocument();
});
});

it("does not close when pressing escape if closeOnEscape is false", async () => {
const user = userEvent.setup();
const Test = () => {
const [open, setOpen] = useState(true);
return (
<Dialog open={open} onClose={() => setOpen(false)} closeOnEsc={false}>
<Dialog.Content>Content</Dialog.Content>
</Dialog>
);
};
render(<Test />);
await waitFor(() => {
expect(screen.getByText("Content")).toBeInTheDocument();
});
await user.keyboard("{Escape}");
await waitFor(() => {
expect(screen.getByText("Content")).toBeInTheDocument();
});
});
});
Loading
Loading