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

table_of_contents_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: a4c4863cdcbbab2124e15fe7d3d9a67109ec747b (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { shallowMount, mount } from '@vue/test-utils';
import TableOfContents from '../../../../content/frontend/default/components/table_of_contents.vue';
import * as dom from '../../../../content/frontend/shared/dom';
import { flattenItems } from '../../../../content/frontend/shared/toc/flatten_items';
import { createExampleToc } from '../../shared/toc_helper';

const TEST_ITEMS = createExampleToc();
const TEST_HELP_AND_FEEDBACK_ID = 'test-help-and-feedback';

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

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

  beforeEach(() => {
    // jquery is not available in Jest yet so we need to mock this method
    jest.spyOn(dom, 'getOuterHeight').mockReturnValue(100);
  });

  const createComponent = (props = {}, mountFn = shallowMount) => {
    wrapper = mountFn(TableOfContents, {
      propsData: {
        items: TEST_ITEMS,
        helpAndFeedbackId: TEST_HELP_AND_FEEDBACK_ID,
        ...props,
      },
    });
  };

  const findCollapseButton = () => wrapper.find('[data-testid="collapse"]');
  const findCollapseIcon = () => findCollapseButton().find('i');
  const findCollapsibleContainer = () => wrapper.find('[data-testid="container"]');
  const findMainList = () => wrapper.find('[data-testid="main-list"]');
  const findMainListItems = () => findMainList().props('items');
  const clickCollapseButton = () => findCollapseButton().trigger('click');

  const expectCollapsed = (isCollapsed = true) => {
    expect(findCollapseButton().attributes('aria-expanded')).toBe(isCollapsed ? undefined : 'true');
    expect(findCollapsibleContainer().props('isCollapsed')).toBe(isCollapsed);
    expect(findCollapseIcon().classes(isCollapsed ? 'fa-angle-right' : 'fa-angle-down')).toBe(true);
  };

  it('matches snapshot', () => {
    createComponent({ hasHelpAndFeedback: true }, mount);
    expect(wrapper.element).toMatchSnapshot();
  });

  describe('with hasHelpAndFeedback', () => {
    beforeEach(() => {
      createComponent({ hasHelpAndFeedback: true });
    });

    it('appends help and feedback item', () => {
      expect(findMainListItems()).toEqual(
        flattenItems(
          TEST_ITEMS.concat([
            {
              href: `#${TEST_HELP_AND_FEEDBACK_ID}`,
              id: null,
              items: [],
              text: 'Help and feedback',
              withSeparator: true,
            },
          ]),
        ),
      );
    });
  });

  describe('default', () => {
    beforeEach(() => {
      createComponent({}, mount);
    });

    it('renders toc list', () => {
      expect(findMainListItems()).toEqual(flattenItems(TEST_ITEMS));
    });

    it('is initially uncollapsed', () => {
      expectCollapsed(false);
    });

    describe('when collapse button is pressed', () => {
      beforeEach(() => {
        clickCollapseButton();
      });

      it('starts expanding', () => {
        expect(findCollapsibleContainer().classes('sm-collapsing')).toBe(true);
      });

      it('immediately updates collapse status', () => {
        expectCollapsed(true);
      });

      it('when button pressed again, nothing happens because in the middle of collapsing', () => {
        clickCollapseButton();

        return wrapper.vm.$nextTick(() => {
          expect(findCollapsibleContainer().classes('sm-collapsing')).toBe(true);
          expectCollapsed(true);
        });
      });
    });
  });
});