aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/organisms/widgets/toc-widget/toc-widget.test.tsx
blob: e4e63aceb69e549613744a185cce65a27b500d85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { describe, expect, it } from '@jest/globals';
import { render, screen as rtlScreen } from '@testing-library/react';
import { Heading } from '../../../atoms';
import { TocWidget } from './toc-widget';

describe('TocWidget', () => {
  it('renders the widget heading and a list of links', () => {
    const heading = 'fugit iusto qui';
    const headingLvl = 3;
    const tree = [
      { children: [], depth: 2, id: 'title1', label: 'Title 1' },
      {
        children: [
          { children: [], depth: 3, id: 'subtitle1', label: 'Subtitle 1' },
          { children: [], depth: 3, id: 'subtitle2', label: 'Subtitle 2' },
        ],
        depth: 2,
        id: 'title2',
        label: 'Title 2',
      },
      { children: [], depth: 2, id: 'title3', label: 'Title 3' },
    ];

    render(
      <TocWidget
        heading={<Heading level={headingLvl}>{heading}</Heading>}
        tree={tree}
      />
    );

    const totalLinks =
      tree.length +
      tree.reduce(
        (accumulator, currentValue) =>
          accumulator + currentValue.children.length,
        0
      );

    expect(
      rtlScreen.getByRole('heading', { level: headingLvl })
    ).toHaveTextContent(heading);
    expect(rtlScreen.getAllByRole('link')).toHaveLength(totalLinks);
  });
});