summaryrefslogtreecommitdiff
path: root/spec/frontend/pipeline_editor/components/file-nav/branch_switcher_spec.js
blob: fa937100982bf3968e1226bd8d6223c7ef1f01c1 (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
import { GlDropdown, GlDropdownItem, GlIcon } from '@gitlab/ui';
import { shallowMount, createLocalVue } from '@vue/test-utils';
import VueApollo from 'vue-apollo';
import createMockApollo from 'helpers/mock_apollo_helper';
import waitForPromises from 'helpers/wait_for_promises';
import BranchSwitcher from '~/pipeline_editor/components/file_nav/branch_switcher.vue';
import { DEFAULT_FAILURE } from '~/pipeline_editor/constants';
import { mockDefaultBranch, mockProjectBranches, mockProjectFullPath } from '../../mock_data';

const localVue = createLocalVue();
localVue.use(VueApollo);

describe('Pipeline editor branch switcher', () => {
  let wrapper;
  let mockApollo;
  let mockAvailableBranchQuery;

  const createComponentWithApollo = () => {
    const resolvers = {
      Query: {
        project: mockAvailableBranchQuery,
      },
    };

    mockApollo = createMockApollo([], resolvers);
    wrapper = shallowMount(BranchSwitcher, {
      localVue,
      apolloProvider: mockApollo,
      provide: {
        projectFullPath: mockProjectFullPath,
      },
      data() {
        return {
          currentBranch: mockDefaultBranch,
        };
      },
    });
  };

  const findDropdown = () => wrapper.findComponent(GlDropdown);
  const findDropdownItems = () => wrapper.findAll(GlDropdownItem);

  beforeEach(() => {
    mockAvailableBranchQuery = jest.fn();
  });

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

  describe('while querying', () => {
    beforeEach(() => {
      createComponentWithApollo();
    });

    it('does not render dropdown', () => {
      expect(findDropdown().exists()).toBe(false);
    });
  });

  describe('after querying', () => {
    beforeEach(async () => {
      mockAvailableBranchQuery.mockResolvedValue(mockProjectBranches);
      createComponentWithApollo();
      await waitForPromises();
    });

    it('query is called with correct variables', async () => {
      expect(mockAvailableBranchQuery).toHaveBeenCalledTimes(1);
      expect(mockAvailableBranchQuery).toHaveBeenCalledWith(
        expect.anything(),
        {
          fullPath: mockProjectFullPath,
        },
        expect.anything(),
        expect.anything(),
      );
    });

    it('renders list of branches', () => {
      expect(findDropdown().exists()).toBe(true);
      expect(findDropdownItems()).toHaveLength(mockProjectBranches.repository.branches.length);
    });

    it('renders current branch at the top of the list with a check mark', () => {
      const firstDropdownItem = findDropdownItems().at(0);
      const icon = firstDropdownItem.findComponent(GlIcon);

      expect(firstDropdownItem.text()).toBe(mockDefaultBranch);
      expect(icon.exists()).toBe(true);
      expect(icon.props('name')).toBe('check');
    });

    it('does not render check mark for other branches', () => {
      const secondDropdownItem = findDropdownItems().at(1);
      const icon = secondDropdownItem.findComponent(GlIcon);

      expect(icon.classes()).toContain('gl-visibility-hidden');
    });
  });

  describe('on fetch error', () => {
    beforeEach(async () => {
      mockAvailableBranchQuery.mockResolvedValue(new Error());
      createComponentWithApollo();
      await waitForPromises();
    });

    it('does not render dropdown', () => {
      expect(findDropdown().exists()).toBe(false);
    });

    it('shows an error message', () => {
      expect(wrapper.emitted('showError')).toBeDefined();
      expect(wrapper.emitted('showError')[0]).toEqual([
        {
          reasons: [wrapper.vm.$options.i18n.fetchError],
          type: DEFAULT_FAILURE,
        },
      ]);
    });
  });
});