Skip to content

Commit

Permalink
test(accordion.spec.tsx): add tests for accordion click to toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Apodaca committed Apr 19, 2023
1 parent ac6eab1 commit 955ceff
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/lib/components/Accordion/Accordion.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ describe('Components / Accordion', () => {
expect(content()[1]).toBeVisible();
});

it('should open focused panel, and close others when `Space` is pressed on an `Accordion.Panel`', async () => {
const user = userEvent.setup();
render(<TestAccordion />);

// eslint-disable-next-line @typescript-eslint/no-unused-vars
for (const _ of titles()) {
await user.tab();
}
await user.keyboard('[Space]');
await user.keyboard('[Space]');

expect(content()[0]).not.toBeVisible();
expect(content()[1]).not.toBeVisible();
});
it('should open focused panel without closing others on an `Accordion.Panel` with `alwaysOpen={true}`', async () => {
const user = userEvent.setup();
render(<TestAccordion alwaysOpen />);
Expand Down Expand Up @@ -237,6 +251,22 @@ describe('Components / Accordion', () => {
});
});
});
describe('Click to toggle open', () => {
beforeEach(() => {
render(<TestAccordion />);
});

it('should open and close the accordion when title is clicked', async () => {
const titleElements = titles();

await userEvent.click(titleElements[1]); // open second panel
await userEvent.click(titleElements[1]); // close second panel
expect(content()[0]).not.toBeVisible()// content should not be visible
expect(content()[1]).not.toBeVisible()// content should not be visible


});
});
});

const TestAccordion: FC<Omit<AccordionProps, 'children'>> = (props) => (
Expand Down

0 comments on commit 955ceff

Please sign in to comment.