Welcome to mirror list, hosted at ThFree Co, Russian Federation.

source_branch_dropdown_spec.js « components « branches « jira_connect « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 56e425fa4ebc709241fd86f5ab6cfd6afa28a724 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { GlCollapsibleListbox } from '@gitlab/ui';
import { mount, shallowMount } from '@vue/test-utils';
import Vue from 'vue';
import VueApollo from 'vue-apollo';
import createMockApollo from 'helpers/mock_apollo_helper';
import waitForPromises from 'helpers/wait_for_promises';
import SourceBranchDropdown from '~/jira_connect/branches/components/source_branch_dropdown.vue';
import { BRANCHES_PER_PAGE } from '~/jira_connect/branches/constants';
import getProjectQuery from '~/jira_connect/branches/graphql/queries/get_project.query.graphql';
import { mockProjects } from '../mock_data';

const mockProject = {
  id: 'test',
  repository: {
    branchNames: ['main', 'f-test', 'release'],
    rootRef: 'main',
  },
};
const mockSelectedProject = mockProjects[0];

const mockProjectQueryResponse = {
  data: {
    project: mockProject,
  },
};
const mockGetProjectQuery = jest.fn().mockResolvedValue(mockProjectQueryResponse);
const mockQueryLoading = jest.fn().mockReturnValue(new Promise(() => {}));

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

  const findListbox = () => wrapper.findComponent(GlCollapsibleListbox);

  const assertListboxItems = () => {
    const listboxItems = findListbox().props('items');
    expect(listboxItems).toHaveLength(mockProject.repository.branchNames.length);
    expect(listboxItems.map((item) => item.text)).toEqual(mockProject.repository.branchNames);
  };

  function createMockApolloProvider({ getProjectQueryLoading = false } = {}) {
    Vue.use(VueApollo);

    const mockApollo = createMockApollo([
      [getProjectQuery, getProjectQueryLoading ? mockQueryLoading : mockGetProjectQuery],
    ]);

    return mockApollo;
  }

  function createComponent({ mockApollo, props, mountFn = shallowMount } = {}) {
    wrapper = mountFn(SourceBranchDropdown, {
      apolloProvider: mockApollo || createMockApolloProvider(),
      propsData: props,
    });
  }

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

  describe('when `selectedProject` prop is not specified', () => {
    beforeEach(() => {
      createComponent();
    });

    it('sets listbox `disabled` prop to `true`', () => {
      expect(findListbox().props('disabled')).toBe(true);
    });

    describe('when `selectedProject` becomes specified', () => {
      beforeEach(async () => {
        wrapper.setProps({
          selectedProject: mockSelectedProject,
        });

        await waitForPromises();
      });

      it('sets listbox props correctly', () => {
        expect(findListbox().props()).toMatchObject({
          disabled: false,
          loading: false,
          searchable: true,
          searching: false,
          toggleText: 'Select a branch',
        });
      });

      it('renders available source branches as listbox items', () => {
        assertListboxItems();
      });
    });
  });

  describe('when `selectedProject` prop is specified', () => {
    describe('when branches are loading', () => {
      it('sets loading prop to true', () => {
        createComponent({
          mockApollo: createMockApolloProvider({ getProjectQueryLoading: true }),
          props: { selectedProject: mockSelectedProject },
        });
        expect(findListbox().props('loading')).toEqual(true);
      });
    });

    describe('when branches have loaded', () => {
      describe('when searching branches', () => {
        it('triggers a refetch', async () => {
          createComponent({ mountFn: mount, props: { selectedProject: mockSelectedProject } });
          await waitForPromises();
          jest.clearAllMocks();

          const mockSearchTerm = 'mai';
          await findListbox().vm.$emit('search', mockSearchTerm);

          expect(mockGetProjectQuery).toHaveBeenCalledWith({
            branchNamesLimit: BRANCHES_PER_PAGE,
            branchNamesOffset: 0,
            branchNamesSearchPattern: `*${mockSearchTerm}*`,
            projectPath: 'test-path',
          });
        });
      });

      describe('template', () => {
        beforeEach(async () => {
          createComponent({ props: { selectedProject: mockSelectedProject } });
          await waitForPromises();
        });

        it('sets listbox props correctly', () => {
          expect(findListbox().props()).toMatchObject({
            disabled: false,
            loading: false,
            searchable: true,
            searching: false,
            toggleText: 'Select a branch',
          });
        });

        it('omits monospace styling from listbox', () => {
          expect(findListbox().classes()).not.toContain('gl-font-monospace');
        });

        it('renders available source branches as listbox items', () => {
          assertListboxItems();
        });

        it("emits `change` event with the repository's `rootRef` by default", () => {
          expect(wrapper.emitted('change')[0]).toEqual([mockProject.repository.rootRef]);
        });

        describe('when selecting a listbox item', () => {
          it('emits `change` event with the selected branch name', async () => {
            const mockBranchName = mockProject.repository.branchNames[1];
            findListbox().vm.$emit('select', mockBranchName);
            expect(wrapper.emitted('change')[1]).toEqual([mockBranchName]);
          });
        });

        describe('when `selectedBranchName` prop is specified', () => {
          const mockBranchName = mockProject.repository.branchNames[2];

          beforeEach(async () => {
            wrapper.setProps({
              selectedBranchName: mockBranchName,
            });
          });

          it('sets listbox text to `selectedBranchName` value', () => {
            expect(findListbox().props('toggleText')).toBe(mockBranchName);
          });

          it('adds monospace styling to listbox', () => {
            expect(findListbox().classes()).toContain('gl-font-monospace');
          });
        });
      });
    });
  });
});