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

top_nav_new_dropdown_spec.js « components « nav « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 18210658b89f5251e3d4077864373d7aee746b37 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
import { GlDropdown } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import TopNavNewDropdown from '~/nav/components/top_nav_new_dropdown.vue';

const TEST_VIEW_MODEL = {
  title: 'Dropdown',
  menu_sections: [
    {
      title: 'Section 1',
      menu_items: [
        { id: 'foo-1', title: 'Foo 1', href: '/foo/1' },
        { id: 'foo-2', title: 'Foo 2', href: '/foo/2' },
        { id: 'foo-3', title: 'Foo 3', href: '/foo/3' },
      ],
    },
    {
      title: 'Section 2',
      menu_items: [
        { id: 'bar-1', title: 'Bar 1', href: '/bar/1' },
        { id: 'bar-2', title: 'Bar 2', href: '/bar/2' },
      ],
    },
  ],
};

describe('~/nav/components/top_nav_menu_sections.vue', () => {
  let wrapper;

  const createComponent = (props = {}) => {
    wrapper = shallowMount(TopNavNewDropdown, {
      propsData: {
        viewModel: TEST_VIEW_MODEL,
        ...props,
      },
    });
  };

  const findDropdown = () => wrapper.findComponent(GlDropdown);
  const findDropdownContents = () =>
    findDropdown()
      .findAll('[data-testid]')
      .wrappers.map((child) => {
        const type = child.attributes('data-testid');

        if (type === 'divider') {
          return { type };
        } else if (type === 'header') {
          return { type, text: child.text() };
        }

        return {
          type,
          text: child.text(),
          href: child.attributes('href'),
        };
      });

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

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

    it('renders dropdown parent', () => {
      expect(findDropdown().props()).toMatchObject({
        text: TEST_VIEW_MODEL.title,
        textSrOnly: true,
        icon: 'plus',
      });
    });

    it('renders dropdown content', () => {
      expect(findDropdownContents()).toEqual([
        {
          type: 'header',
          text: TEST_VIEW_MODEL.menu_sections[0].title,
        },
        ...TEST_VIEW_MODEL.menu_sections[0].menu_items.map(({ title, href }) => ({
          type: 'item',
          href,
          text: title,
        })),
        {
          type: 'divider',
        },
        {
          type: 'header',
          text: TEST_VIEW_MODEL.menu_sections[1].title,
        },
        ...TEST_VIEW_MODEL.menu_sections[1].menu_items.map(({ title, href }) => ({
          type: 'item',
          href,
          text: title,
        })),
      ]);
    });
  });

  describe('with only 1 section', () => {
    beforeEach(() => {
      createComponent({
        viewModel: {
          ...TEST_VIEW_MODEL,
          menu_sections: TEST_VIEW_MODEL.menu_sections.slice(0, 1),
        },
      });
    });

    it('renders dropdown content without headers and dividers', () => {
      expect(findDropdownContents()).toEqual(
        TEST_VIEW_MODEL.menu_sections[0].menu_items.map(({ title, href }) => ({
          type: 'item',
          href,
          text: title,
        })),
      );
    });
  });
});