summaryrefslogtreecommitdiff
path: root/spec/frontend/projects/settings/branch_rules/branch_dropdown_spec.js
blob: 79bce5a4b3fa83f2c997bad43e6dfc0f6c24c3ad (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
import Vue, { nextTick } from 'vue';
import VueApollo from 'vue-apollo';
import { GlDropdown, GlSearchBoxByType, GlDropdownItem, GlSprintf } from '@gitlab/ui';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import BranchDropdown, {
  i18n,
} from '~/projects/settings/branch_rules/components/branch_dropdown.vue';
import createMockApollo from 'helpers/mock_apollo_helper';
import branchesQuery from '~/projects/settings/branch_rules/queries/branches.query.graphql';
import waitForPromises from 'helpers/wait_for_promises';
import { createAlert } from '~/flash';

Vue.use(VueApollo);
jest.mock('~/flash');

describe('Branch dropdown', () => {
  let wrapper;

  const projectPath = 'test/project';
  const value = 'main';
  const mockBranchNames = ['test 1', 'test 2'];

  const createComponent = async ({ branchNames = mockBranchNames, resolver } = {}) => {
    const mockResolver =
      resolver ||
      jest.fn().mockResolvedValue({
        data: { project: { id: '1', repository: { branchNames } } },
      });
    const apolloProvider = createMockApollo([[branchesQuery, mockResolver]]);

    wrapper = shallowMountExtended(BranchDropdown, {
      apolloProvider,
      propsData: { projectPath, value },
    });

    await waitForPromises();
  };

  const findGlDropdown = () => wrapper.findComponent(GlDropdown);
  const findAllBranches = () => wrapper.findAllComponents(GlDropdownItem);
  const findNoDataMsg = () => wrapper.findByTestId('no-data');
  const findGlSearchBoxByType = () => wrapper.findComponent(GlSearchBoxByType);
  const findWildcardButton = () => wrapper.findByTestId('create-wildcard-button');
  const findHelpText = () => wrapper.findComponent(GlSprintf);
  const setSearchTerm = (searchTerm) => findGlSearchBoxByType().vm.$emit('input', searchTerm);

  beforeEach(() => createComponent());

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

  it('renders a GlDropdown component with the correct props', () => {
    expect(findGlDropdown().props()).toMatchObject({ text: value });
  });

  it('renders GlDropdownItem components for each branch', () => {
    expect(findAllBranches().length).toBe(mockBranchNames.length);

    mockBranchNames.forEach((branchName, index) =>
      expect(findAllBranches().at(index).text()).toBe(branchName),
    );
  });

  it('emits `select` with the branch name when a branch is clicked', () => {
    findAllBranches().at(0).vm.$emit('click');
    expect(wrapper.emitted('input')).toEqual([[mockBranchNames[0]]]);
  });

  describe('branch searching', () => {
    it('displays a message if no branches can be found', async () => {
      await createComponent({ branchNames: [] });

      expect(findNoDataMsg().text()).toBe(i18n.noMatch);
    });

    it('displays a loading state while search request is in flight', async () => {
      setSearchTerm('test');
      await nextTick();

      expect(findGlSearchBoxByType().props()).toMatchObject({ isLoading: true });
    });

    it('renders a wildcard button', async () => {
      const searchTerm = 'test-*';
      setSearchTerm(searchTerm);
      await nextTick();

      expect(findWildcardButton().exists()).toBe(true);
      findWildcardButton().vm.$emit('click');
      expect(wrapper.emitted('createWildcard')).toEqual([[searchTerm]]);
    });

    it('renders help text', () => {
      expect(findHelpText().attributes('message')).toBe(i18n.branchHelpText);
    });
  });

  it('displays an error message if fetch failed', async () => {
    const error = new Error('an error occurred');
    const resolver = jest.fn().mockRejectedValueOnce(error);
    await createComponent({ resolver });

    expect(createAlert).toHaveBeenCalledWith({
      message: i18n.fetchBranchesError,
      captureError: true,
      error,
    });
  });
});