aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/organisms/widgets/links-widget/links-widget.test.tsx
blob: f0edecc57056d709798b5ac8a617f4631c3d2d82 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { describe, expect, it } from '@jest/globals';
import { render, screen as rtlScreen } from '@testing-library/react';
import { Heading } from '../../../atoms';
import { LinksWidget, type LinksWidgetItemData } from './links-widget';

describe('LinksWidget', () => {
  it('renders the widget heading and a list of links', () => {
    const heading = 'modi sit fugiat';
    const headingLvl = 3;
    const items = [
      { id: 'item1', label: 'Link 1', url: '#link1' },
      { id: 'item2', label: 'Link 2', url: '#link2' },
      { id: 'item3', label: 'Link 3', url: '#link3' },
    ] satisfies LinksWidgetItemData[];

    render(
      <LinksWidget
        heading={<Heading level={headingLvl}>{heading}</Heading>}
        items={items}
      />
    );

    expect(
      rtlScreen.getByRole('heading', { level: headingLvl })
    ).toHaveTextContent(heading);
    expect(rtlScreen.getAllByRole('link').length).toBe(items.length);
  });

  it('can render a nested list of links', () => {
    const heading = 'modi sit fugiat';
    const headingLvl = 3;
    const items = [
      { id: 'item1', label: 'Link 1', url: '#link1' },
      {
        id: 'item2',
        label: 'Link 2',
        url: '#link2',
        child: [
          { id: 'subitem1', label: 'Nested link 1', url: '#nested-link1' },
          { id: 'subitem2', label: 'Nested link 2', url: '#nested-link2' },
        ],
      },
      { id: 'item3', label: 'Link 3', url: '#link3' },
    ] satisfies LinksWidgetItemData[];

    render(
      <LinksWidget
        heading={<Heading level={headingLvl}>{heading}</Heading>}
        items={items}
      />
    );

    const totalLinks =
      items.length +
      items.reduce(
        (accumulator, currentValue) =>
          accumulator + (currentValue.child?.length ?? 0),
        0
      );

    expect(rtlScreen.getAllByRole('link').length).toBe(totalLinks);
  });

  it('can render an ordered list of links', () => {
    const heading = 'modi sit fugiat';
    const headingLvl = 3;
    const items = [
      { id: 'item1', label: 'Link 1', url: '#link1' },
      { id: 'item2', label: 'Link 2', url: '#link2' },
      { id: 'item3', label: 'Link 3', url: '#link3' },
    ] satisfies LinksWidgetItemData[];

    render(
      <LinksWidget
        heading={<Heading level={headingLvl}>{heading}</Heading>}
        isOrdered
        items={items}
      />
    );

    expect(rtlScreen.getByRole('list')).toHaveClass(`list--ordered`);
  });
});