summaryrefslogtreecommitdiff
path: root/spec/frontend/nav/components/top_nav_menu_sections_spec.js
blob: d56542fe572288fb2f2ebf13325f0c32fd7ff65b (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
import { shallowMount } from '@vue/test-utils';
import TopNavMenuSections from '~/nav/components/top_nav_menu_sections.vue';

const TEST_SECTIONS = [
  {
    id: 'primary',
    menuItems: [{ id: 'test', href: '/test/href' }, { id: 'foo' }, { id: 'bar' }],
  },
  {
    id: 'secondary',
    menuItems: [{ id: 'lorem' }, { 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-item"]').wrappers.map((x) => ({
      menuItem: x.props('menuItem'),
      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', () => {
      expect(findSectionModels()).toEqual([
        {
          classes: [],
          menuItems: [
            {
              menuItem: TEST_SECTIONS[0].menuItems[0],
              classes: ['gl-w-full'],
            },
            ...TEST_SECTIONS[0].menuItems.slice(1).map((menuItem) => ({
              menuItem,
              classes: ['gl-w-full', 'gl-mt-1'],
            })),
          ],
        },
        {
          classes: [...TopNavMenuSections.BORDER_CLASSES.split(' '), 'gl-mt-3'],
          menuItems: [
            {
              menuItem: TEST_SECTIONS[1].menuItems[0],
              classes: ['gl-w-full'],
            },
            ...TEST_SECTIONS[1].menuItems.slice(1).map((menuItem) => ({
              menuItem,
              classes: ['gl-w-full', 'gl-mt-1'],
            })),
          ],
        },
      ]);
    });

    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[1]]]);
    });
  });

  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(' ')],
        [...TopNavMenuSections.BORDER_CLASSES.split(' '), 'gl-mt-3'],
      ]);
    });
  });
});