Welcome to mirror list, hosted at ThFree Co, Russian Federation.

table_of_contents_list_spec.js « components « default « frontend « spec - gitlab.com/gitlab-org/gitlab-docs.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 46b1bd7646d5c60cfbe23c3facb4a76e6718c96c (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
84
85
86
87
88
89
90
import { mount } from '@vue/test-utils';
import TableOfContentsList from '../../../../content/frontend/default/components/table_of_contents_list.vue';
import { parseTOC } from '../../../../content/frontend/shared/toc/parse_toc';
import { createExampleToc } from '../../shared/toc_helper';

describe('frontend/default/components/table_of_contents_list', () => {
  let wrapper;

  const createComponent = (props = {}) => {
    wrapper = mount(TableOfContentsList, {
      propsData: {
        ...props,
      },
    });
  };

  const findItemsData = () => parseTOC(wrapper.element);
  const findLinks = () => wrapper.findAll('a');

  afterEach(() => {
    wrapper.destroy();
    wrapper = null;
  });

  describe('with items', () => {
    let items;

    beforeEach(() => {
      items = createExampleToc();
      createComponent({ items });
    });

    it('renders all items', () => {
      expect(findItemsData()).toEqual(items);
    });

    it('starts at level 0', () => {
      expect(findLinks().at(0).classes('toc-level-0')).toBe(true);
    });
  });

  describe('with empty items', () => {
    beforeEach(() => {
      createComponent({ items: [] });
    });

    it('shows empty ul', () => {
      expect(wrapper.element).toMatchSnapshot();
    });
  });

  describe('with level', () => {
    beforeEach(() => {
      createComponent({
        items: [
          {
            text: 'A',
            items: [{ text: 'A_1' }, { text: 'A_2' }],
          },
          {
            text: 'B',
            level: 1,
            items: [{ text: 'B_1', items: [{ text: 'B_1_1' }] }],
          },
        ],
        level: 1,
      });
    });

    it('increments levels for nested lists', () => {
      // Order isn't important. We just want to find the link + level class mapping
      const data = findLinks().wrappers.reduce(
        (acc, link) =>
          Object.assign(acc, {
            [link.text()]: link.classes().find((x) => x.startsWith('toc-level')),
          }),
        {},
      );

      expect(data).toEqual({
        A: 'toc-level-1',
        A_1: 'toc-level-2',
        A_2: 'toc-level-2',
        B: 'toc-level-2',
        B_1: 'toc-level-3',
        B_1_1: 'toc-level-4',
      });
    });
  });
});