summaryrefslogtreecommitdiff
path: root/spec/frontend/nav/components/top_nav_dropdown_menu_spec.js
blob: 70df05a27817f4bc9fd1f46b7dac2b051127479b (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
143
144
145
146
147
148
import { shallowMount, mount } from '@vue/test-utils';
import { nextTick } from 'vue';
import TopNavDropdownMenu from '~/nav/components/top_nav_dropdown_menu.vue';
import TopNavMenuItem from '~/nav/components/top_nav_menu_item.vue';
import TopNavMenuSections from '~/nav/components/top_nav_menu_sections.vue';
import KeepAliveSlots from '~/vue_shared/components/keep_alive_slots.vue';
import { TEST_NAV_DATA } from '../mock_data';

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

  const createComponent = (props = {}, mountFn = shallowMount) => {
    wrapper = mountFn(TopNavDropdownMenu, {
      propsData: {
        primary: TEST_NAV_DATA.primary,
        secondary: TEST_NAV_DATA.secondary,
        views: TEST_NAV_DATA.views,
        ...props,
      },
      stubs: {
        // Stub the keep-alive-slots so we don't render frequent items which uses a store
        KeepAliveSlots: true,
      },
    });
  };

  const findMenuItems = () => wrapper.findAllComponents(TopNavMenuItem);
  const findMenuSections = () => wrapper.find(TopNavMenuSections);
  const findMenuSidebar = () => wrapper.find('[data-testid="menu-sidebar"]');
  const findMenuSubview = () => wrapper.findComponent(KeepAliveSlots);
  const hasFullWidthMenuSidebar = () => findMenuSidebar().classes('gl-w-full');

  const withActiveIndex = (menuItems, activeIndex) =>
    menuItems.map((x, idx) => ({
      ...x,
      active: idx === activeIndex,
    }));

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

  beforeEach(() => {
    jest.spyOn(console, 'error').mockImplementation();
  });

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

    it('renders menu sections', () => {
      expect(findMenuSections().props()).toEqual({
        sections: [
          { id: 'primary', menuItems: TEST_NAV_DATA.primary },
          { id: 'secondary', menuItems: TEST_NAV_DATA.secondary },
        ],
        withTopBorder: false,
      });
    });

    it('has full width menu sidebar', () => {
      expect(hasFullWidthMenuSidebar()).toBe(true);
    });

    it('renders hidden subview with no slot key', () => {
      const subview = findMenuSubview();

      expect(subview.isVisible()).toBe(false);
      expect(subview.props()).toEqual({ slotKey: '' });
    });
  });

  describe('with pre-initialized active view', () => {
    beforeEach(() => {
      // We opt for a small integration test, to make sure the event is handled correctly
      // as it would in prod.
      createComponent(
        {
          primary: withActiveIndex(TEST_NAV_DATA.primary, 1),
        },
        mount,
      );
    });

    it('renders menu sections', () => {
      expect(findMenuSections().props('sections')).toStrictEqual([
        { id: 'primary', menuItems: withActiveIndex(TEST_NAV_DATA.primary, 1) },
        { id: 'secondary', menuItems: TEST_NAV_DATA.secondary },
      ]);
    });

    it('does not have full width menu sidebar', () => {
      expect(hasFullWidthMenuSidebar()).toBe(false);
    });

    it('renders visible subview with slot key', () => {
      const subview = findMenuSubview();

      expect(subview.isVisible()).toBe(true);
      expect(subview.props('slotKey')).toBe(TEST_NAV_DATA.primary[1].view);
    });

    it('does not change view if non-view menu item is clicked', async () => {
      const secondaryLink = findMenuItems().at(TEST_NAV_DATA.primary.length);

      // Ensure this doesn't have a view
      expect(secondaryLink.props('menuItem').view).toBeUndefined();

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

      await nextTick();

      expect(findMenuSubview().props('slotKey')).toBe(TEST_NAV_DATA.primary[1].view);
    });

    describe('when menu item is clicked', () => {
      let primaryLink;

      beforeEach(async () => {
        primaryLink = findMenuItems().at(0);
        primaryLink.vm.$emit('click');
        await nextTick();
      });

      it('clicked on link with view', () => {
        expect(primaryLink.props('menuItem').view).toBeTruthy();
      });

      it('changes active view', () => {
        expect(findMenuSubview().props('slotKey')).toBe(TEST_NAV_DATA.primary[0].view);
      });

      it('changes active status on menu item', () => {
        expect(findMenuSections().props('sections')).toStrictEqual([
          {
            id: 'primary',
            menuItems: withActiveIndex(TEST_NAV_DATA.primary, 0),
          },
          {
            id: 'secondary',
            menuItems: withActiveIndex(TEST_NAV_DATA.secondary, -1),
          },
        ]);
      });
    });
  });
});