aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/molecules/collapsible/collapsible.test.tsx
blob: 52fbdd06fb9b366538c06a89a99a3ba6dc2b7f8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-w
import { describe, expect, it } from '@jest/globals';
import { render, screen as rtlScreen } from '@testing-library/react';
import { userEvent } from '@testing-library/user-event';
import { Collapsible } from './collapsible';
import { Heading } from 'src/components/atoms';

const body =
  'Error autem numquam vero quo cum qui voluptatem est qui. Quasi id rem molestiae. Velit voluptatum dolores et officia. Ut voluptatem quae ut quaerat vel nulla.';
const heading = 'sed minima sed';

describe('Collapsible', () => {
  it('renders the collapsible heading and body', () => {
    const headingLevel = 2;

    render(
      <Collapsible heading={<Heading level={headingLevel}>{heading}</Heading>}>
        {body}
      </Collapsible>
    );

    expect(
      rtlScreen.getByRole('heading', { level: headingLevel })
    ).toHaveTextContent(heading);
    expect(rtlScreen.getByText(body)).toBeInTheDocument();
  });

  it('can be collapsed by default', () => {
    render(
      <Collapsible heading={<Heading level={3}>{heading}</Heading>} isCollapsed>
        {body}
      </Collapsible>
    );

    expect(rtlScreen.getByRole('button').parentElement).toHaveClass(
      'wrapper--collapsed'
    );
    // Neither toBeVisible or toHaveStyle are working.
    // expect(rtlScreen.getByText(body)).toHaveStyle({ visibility: 'hidden' });
    // expect(rtlScreen.getByText(body)).not.toBeVisible();
  });

  it('can be expanded by default', () => {
    render(
      <Collapsible
        heading={<Heading level={3}>{heading}</Heading>}
        isCollapsed={false}
      >
        {body}
      </Collapsible>
    );

    expect(rtlScreen.getByRole('button').parentElement).toHaveClass(
      'wrapper--expanded'
    );
    expect(rtlScreen.getByText(body)).toBeVisible();
  });

  it('can be collapsed and/or expanded by the user', async () => {
    const user = userEvent.setup();

    render(
      <Collapsible heading={<Heading level={3}>{heading}</Heading>}>
        {body}
      </Collapsible>
    );

    expect(rtlScreen.getByRole('button').parentElement).toHaveClass(
      'wrapper--expanded'
    );

    await user.click(rtlScreen.getByRole('button', { name: heading }));

    expect(rtlScreen.getByRole('button').parentElement).toHaveClass(
      'wrapper--collapsed'
    );

    await user.click(rtlScreen.getByRole('button', { name: heading }));

    expect(rtlScreen.getByRole('button').parentElement).toHaveClass(
      'wrapper--expanded'
    );
  });
});