summaryrefslogtreecommitdiff
path: root/spec/frontend/vue_shared/issuable/list/components/issuable_tabs_spec.js
blob: 8c22b67bdbec21aeef0bc7fcd0ee7d871134a980 (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
import { GlTab, GlBadge } from '@gitlab/ui';
import { mount } from '@vue/test-utils';

import IssuableTabs from '~/vue_shared/issuable/list/components/issuable_tabs.vue';

import { mockIssuableListProps } from '../mock_data';

const createComponent = ({
  tabs = mockIssuableListProps.tabs,
  tabCounts = mockIssuableListProps.tabCounts,
  currentTab = mockIssuableListProps.currentTab,
} = {}) =>
  mount(IssuableTabs, {
    propsData: {
      tabs,
      tabCounts,
      currentTab,
    },
    slots: {
      'nav-actions': `
      <button class="js-new-issuable">New issuable</button>
    `,
    },
  });

describe('IssuableTabs', () => {
  let wrapper;

  beforeEach(() => {
    wrapper = createComponent();
  });

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

  const findAllGlBadges = () => wrapper.findAllComponents(GlBadge);
  const findAllGlTabs = () => wrapper.findAllComponents(GlTab);

  describe('methods', () => {
    describe('isTabActive', () => {
      it.each`
        tabName     | currentTab  | returnValue
        ${'opened'} | ${'opened'} | ${true}
        ${'opened'} | ${'closed'} | ${false}
      `(
        'returns $returnValue when tab name is "$tabName" is current tab is "$currentTab"',
        async ({ tabName, currentTab, returnValue }) => {
          wrapper.setProps({
            currentTab,
          });

          await wrapper.vm.$nextTick();

          expect(wrapper.vm.isTabActive(tabName)).toBe(returnValue);
        },
      );
    });
  });

  describe('template', () => {
    it('renders gl-tab for each tab within `tabs` array', () => {
      const tabsEl = findAllGlTabs();

      expect(tabsEl.exists()).toBe(true);
      expect(tabsEl).toHaveLength(mockIssuableListProps.tabs.length);
    });

    it('renders gl-badge component within a tab', () => {
      const badges = findAllGlBadges();

      // Does not render `All` badge since it has an undefined count
      expect(badges).toHaveLength(2);
      expect(badges.at(0).text()).toBe(`${mockIssuableListProps.tabCounts.opened}`);
      expect(badges.at(1).text()).toBe(`${mockIssuableListProps.tabCounts.closed}`);
    });

    it('renders contents for slot "nav-actions"', () => {
      const buttonEl = wrapper.find('button.js-new-issuable');

      expect(buttonEl.exists()).toBe(true);
      expect(buttonEl.text()).toBe('New issuable');
    });
  });

  describe('events', () => {
    it('gl-tab component emits `click` event on `click` event', () => {
      const tabEl = findAllGlTabs().at(0);

      tabEl.vm.$emit('click', 'opened');

      expect(wrapper.emitted('click')).toBeTruthy();
      expect(wrapper.emitted('click')[0]).toEqual(['opened']);
    });
  });
});