summaryrefslogtreecommitdiff
path: root/spec/frontend/nav/components/top_nav_menu_sections_spec.js
blob: 7a5a8475ab746939942597f1b0752a2c77267bc9 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { shallowMount } from '@vue/test-utils';
import TopNavMenuSections from '~/nav/components/top_nav_menu_sections.vue';

const TEST_SECTIONS = [
  {
    id: 'primary',
    menuItems: [
      { type: 'header', title: 'Heading' },
      { type: 'item', id: 'test', href: '/test/href' },
      { type: 'header', title: 'Another Heading' },
      { type: 'item', id: 'foo' },
      { type: 'item', id: 'bar' },
    ],
  },
  {
    id: 'secondary',
    menuItems: [
      { type: 'item', id: 'lorem' },
      { type: 'item', id: 'ipsum' },
    ],
  },
];

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

  const createComponent = (props = {}) => {
    wrapper = shallowMount(TopNavMenuSections, {
      propsData: {
        sections: TEST_SECTIONS,
        ...props,
      },
    });
  };

  const findMenuItemModels = (parent) =>
    parent.findAll('[data-testid="menu-header"],[data-testid="menu-item"]').wrappers.map((x) => {
      return {
        menuItem: x.vm
          ? {
              type: 'item',
              ...x.props('menuItem'),
            }
          : {
              type: 'header',
              title: x.text(),
            },
        classes: x.classes(),
      };
    });
  const findSectionModels = () =>
    wrapper.findAll('[data-testid="menu-section"]').wrappers.map((x) => ({
      classes: x.classes(),
      menuItems: findMenuItemModels(x),
    }));

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

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

    it('renders sections with menu items', () => {
      const headerClasses = ['gl-px-4', 'gl-py-2', 'gl-text-gray-900', 'gl-display-block'];
      const itemClasses = ['gl-w-full'];

      expect(findSectionModels()).toEqual([
        {
          classes: [],
          menuItems: TEST_SECTIONS[0].menuItems.map((menuItem, index) => {
            const classes = menuItem.type === 'header' ? [...headerClasses] : [...itemClasses];
            if (index > 0) classes.push(menuItem.type === 'header' ? 'gl-pt-3!' : 'gl-mt-1');
            return {
              menuItem,
              classes,
            };
          }),
        },
        {
          classes: [
            ...TopNavMenuSections.BORDER_CLASSES.split(' '),
            'gl-border-gray-50',
            'gl-mt-3',
          ],
          menuItems: TEST_SECTIONS[1].menuItems.map((menuItem, index) => {
            const classes = menuItem.type === 'header' ? [...headerClasses] : [...itemClasses];
            if (index > 0) classes.push(menuItem.type === 'header' ? 'gl-pt-3!' : 'gl-mt-1');
            return {
              menuItem,
              classes,
            };
          }),
        },
      ]);
    });

    it('when clicked menu item with href, does nothing', () => {
      const menuItem = wrapper.findAll('[data-testid="menu-item"]').at(0);

      menuItem.vm.$emit('click');

      expect(wrapper.emitted()).toEqual({});
    });

    it('when clicked menu item without href, emits "menu-item-click"', () => {
      const menuItem = wrapper.findAll('[data-testid="menu-item"]').at(1);

      menuItem.vm.$emit('click');

      expect(wrapper.emitted('menu-item-click')).toEqual([[TEST_SECTIONS[0].menuItems[3]]]);
    });
  });

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

    it('renders border classes for top section', () => {
      expect(findSectionModels().map((x) => x.classes)).toEqual([
        [...TopNavMenuSections.BORDER_CLASSES.split(' '), 'gl-border-gray-50'],
        [...TopNavMenuSections.BORDER_CLASSES.split(' '), 'gl-border-gray-50', 'gl-mt-3'],
      ]);
    });
  });

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

    it('renders border classes for top section', () => {
      expect(findSectionModels().map((x) => x.classes)).toEqual([
        [],
        [...TopNavMenuSections.BORDER_CLASSES.split(' '), 'gl-border-gray-100', 'gl-mt-3'],
      ]);
    });
  });
});