Skip to content

Commit

Permalink
chore: make the Accordion tests more comprehensive
Browse files Browse the repository at this point in the history
  • Loading branch information
ahuth committed Nov 29, 2023
1 parent ea2ceed commit 637b85f
Showing 1 changed file with 27 additions and 15 deletions.
42 changes: 27 additions & 15 deletions packages/components/src/core/Accordion/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,41 @@
import { generateSnapshots } from "@chanzuckerberg/story-utils";
import { composeStory } from "@storybook/react";
import { fireEvent, render, screen } from "@testing-library/react";
import * as snapshotTestStoryFile from "./index.stories";
import Meta, { Test as TestStory } from "./index.stories";
import { composeStories } from "@storybook/react";
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
import * as stories from "./index.stories";

// Returns a component that already contain all decorators from story level, meta level and global level.
const Test = composeStory(TestStory, Meta);
const { Test } = composeStories(stories);

describe("<Accordion />", () => {
generateSnapshots(snapshotTestStoryFile);
generateSnapshots(stories);

it("renders accordion component", () => {
render(<Test {...Test.args} />);
render(<Test />);
const accordionElement = screen.getByTestId("accordion");
expect(accordionElement).not.toBeNull();
});

it("opens accordion when clicked", () => {
it("opens and closes the accordion when clicked", async () => {
const ariaExpanded = "aria-expanded";

render(<Test />);
const accordionElement = screen.getByTestId("accordion");
fireEvent.click(accordionElement);
expect(
screen.getAllByText(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse malesuada lacus ex, sit amet blandit leo lobortis eget."
)
).not.toBeNull();

// Starts closed
const accordionHeader = screen.getByRole("button");
expect(accordionHeader).toHaveAttribute(ariaExpanded, "false");
expect(screen.getByText(/Lorem ipsum/)).not.toBeVisible();

// It opens when clicked.
fireEvent.click(accordionHeader);
expect(accordionHeader).toHaveAttribute(ariaExpanded, "true");
expect(screen.getByText(/Lorem ipsum/)).toBeVisible();

// And closes when clicked again.
fireEvent.click(accordionHeader);
expect(accordionHeader).toHaveAttribute(ariaExpanded, "false");

await waitFor(() => {
expect(screen.getByText(/Lorem ipsum/)).not.toBeVisible();
});
});
});

0 comments on commit 637b85f

Please sign in to comment.