Skip to content

Commit

Permalink
test: update accordion tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DavieReid committed Aug 24, 2023
1 parent 68772c9 commit 703fc39
Showing 1 changed file with 28 additions and 28 deletions.
56 changes: 28 additions & 28 deletions packages/components/src/Accordion/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React from 'react';
import { render, fireEvent, waitFor, screen } from '@testing-library/react';
import { render, waitFor, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { within } from '@testing-library/dom';

import { Accordion, AccordionDetails, AccordionSection, AccordionSummary } from '../index';

Expand All @@ -23,11 +25,11 @@ describe('GIVEN an `Accordion` component', () => {
);
// assert
expect(getAllByRole('button').length).toEqual(3);
expect(queryAllByRole('tabpanel').length).toEqual(3);
expect(queryAllByRole('tabpanel').length).toEqual(0);
});

test('THEN should show and hide panels when the tab is clicked', async () => {
const { getByRole, getAllByRole, queryAllByRole } = render(
render(
<Accordion>
<AccordionSection>
<AccordionSummary>SUMMARY 1</AccordionSummary>
Expand All @@ -43,40 +45,38 @@ describe('GIVEN an `Accordion` component', () => {
</AccordionSection>
</Accordion>
);

// assert
expect(queryAllByRole('button').length).toEqual(3);
expect(getAllByRole('button')[0]).toHaveAttribute('aria-expanded', 'false');
expect(getAllByRole('button')[1]).toHaveAttribute('aria-expanded', 'false');
expect(getAllByRole('button')[2]).toHaveAttribute('aria-expanded', 'false');
expect(screen.queryByText('CONTENT 1')).not.toBeInTheDocument();
expect(screen.queryByText('CONTENT 2')).not.toBeInTheDocument();
expect(screen.queryByText('CONTENT 3')).not.toBeInTheDocument();
expect(screen.queryAllByRole('button').length).toEqual(3);
expect(screen.getAllByRole('button')[0]).toHaveAttribute('aria-expanded', 'false');
expect(screen.getAllByRole('button')[1]).toHaveAttribute('aria-expanded', 'false');
expect(screen.getAllByRole('button')[2]).toHaveAttribute('aria-expanded', 'false');
expect(screen.queryAllByRole('tabpanel').length).toEqual(0); // nothing visible

// act - click to open tab 2
let tab2 = getAllByRole('button')[1];
fireEvent.click(tab2);

let tab2 = screen.getAllByRole('button')[1];
userEvent.click(tab2);
// assert
await waitFor(() => expect(getAllByRole('button')[1]).toHaveAttribute('aria-expanded', 'true'));
await waitFor(() => expect(screen.queryByText('CONTENT 2')).toBeInTheDocument());
expect(getAllByRole('button')[0]).toHaveAttribute('aria-expanded', 'false');
expect(getAllByRole('button')[2]).toHaveAttribute('aria-expanded', 'false');
expect(screen.queryByText('CONTENT 1')).not.toBeInTheDocument();
expect(screen.queryByText('CONTENT 3')).not.toBeInTheDocument();
await waitFor(() =>
expect(screen.getAllByRole('button')[1]).toHaveAttribute('aria-expanded', 'true')
);
await waitFor(() => expect(screen.queryAllByRole('tabpanel').length).toEqual(1));
expect(screen.getAllByRole('button')[0]).toHaveAttribute('aria-expanded', 'false');
expect(screen.getAllByRole('button')[2]).toHaveAttribute('aria-expanded', 'false');

const visiblePanelContent = within(screen.queryAllByRole('tabpanel')[0]).getByText('CONTENT 2');
expect(visiblePanelContent).toBeDefined();

// act - click to close tab 2
tab2 = getAllByRole('button')[1];
fireEvent.click(tab2);
tab2 = screen.getAllByRole('button')[1];
userEvent.click(tab2);

// assert
await waitFor(() =>
expect(getAllByRole('button')[1]).toHaveAttribute('aria-expanded', 'false')
expect(screen.getAllByRole('button')[1]).toHaveAttribute('aria-expanded', 'false')
);
await waitFor(() => expect(screen.queryByText('CONTENT 2')).not.toBeInTheDocument());
expect(getAllByRole('button')[0]).toHaveAttribute('aria-expanded', 'false');
expect(getAllByRole('button')[2]).toHaveAttribute('aria-expanded', 'false');
expect(screen.queryByText('CONTENT 1')).not.toBeInTheDocument();
expect(screen.queryByText('CONTENT 3')).not.toBeInTheDocument();
await waitFor(() => expect(screen.queryAllByRole('tabpanel').length).toEqual(0));
expect(screen.getAllByRole('button')[0]).toHaveAttribute('aria-expanded', 'false');
expect(screen.getAllByRole('button')[1]).toHaveAttribute('aria-expanded', 'false');
expect(screen.getAllByRole('button')[2]).toHaveAttribute('aria-expanded', 'false');
});
});

1 comment on commit 703fc39

@vercel
Copy link

@vercel vercel bot commented on 703fc39 Aug 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

mosaic – ./

mosaic-git-main-mosaic-dev-team.vercel.app
mosaic-mosaic-dev-team.vercel.app

Please sign in to comment.