summaryrefslogtreecommitdiff
path: root/spec/frontend/vue_merge_request_widget/components/mr_widget_rebase_spec.js
blob: ec047fe0714ea0651978e773f666237cda27bf91 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import { mount } from '@vue/test-utils';
import { nextTick } from 'vue';
import WidgetRebase from '~/vue_merge_request_widget/components/states/mr_widget_rebase.vue';
import eventHub from '~/vue_merge_request_widget/event_hub';
import toast from '~/vue_shared/plugins/global_toast';

jest.mock('~/vue_shared/plugins/global_toast');

let wrapper;

function createWrapper(propsData) {
  wrapper = mount(WidgetRebase, {
    propsData,
    data() {
      return {
        state: {
          rebaseInProgress: propsData.mr.rebaseInProgress,
          targetBranch: propsData.mr.targetBranch,
          userPermissions: {
            pushToSourceBranch: propsData.mr.canPushToSourceBranch,
          },
        },
      };
    },
    mocks: {
      $apollo: {
        queries: {
          state: { loading: false },
        },
      },
    },
  });
}

describe('Merge request widget rebase component', () => {
  const findRebaseMessage = () => wrapper.find('[data-testid="rebase-message"]');
  const findRebaseMessageText = () => findRebaseMessage().text();
  const findStandardRebaseButton = () => wrapper.find('[data-testid="standard-rebase-button"]');
  const findRebaseWithoutCiButton = () => wrapper.find('[data-testid="rebase-without-ci-button"]');

  afterEach(() => {
    wrapper.destroy();
    wrapper = null;
  });
  describe('while rebasing', () => {
    it('should show progress message', () => {
      createWrapper({
        mr: { rebaseInProgress: true },
        service: {},
      });

      expect(findRebaseMessageText()).toContain('Rebase in progress');
    });
  });

  describe('with permissions', () => {
    const rebaseMock = jest.fn().mockResolvedValue();
    const pollMock = jest.fn().mockResolvedValue({});

    it('renders the warning message', () => {
      createWrapper({
        mr: {
          rebaseInProgress: false,
          canPushToSourceBranch: true,
        },
        service: {
          rebase: rebaseMock,
          poll: pollMock,
        },
      });

      const text = findRebaseMessageText();

      expect(text).toContain('Merge blocked');
      expect(text.replace(/\s\s+/g, ' ')).toContain(
        'the source branch must be rebased onto the target branch',
      );
    });

    it('renders an error message when rebasing has failed', async () => {
      createWrapper({
        mr: {
          rebaseInProgress: false,
          canPushToSourceBranch: true,
        },
        service: {
          rebase: rebaseMock,
          poll: pollMock,
        },
      });

      // setData usage is discouraged. See https://gitlab.com/groups/gitlab-org/-/epics/7330 for details
      // eslint-disable-next-line no-restricted-syntax
      wrapper.setData({ rebasingError: 'Something went wrong!' });

      await nextTick();
      expect(findRebaseMessageText()).toContain('Something went wrong!');
    });

    describe('Rebase buttons', () => {
      beforeEach(() => {
        createWrapper({
          mr: {
            rebaseInProgress: false,
            canPushToSourceBranch: true,
          },
          service: {
            rebase: rebaseMock,
            poll: pollMock,
          },
        });
      });

      it('renders both buttons', () => {
        expect(findRebaseWithoutCiButton().exists()).toBe(true);
        expect(findStandardRebaseButton().exists()).toBe(true);
      });

      it('starts the rebase when clicking', async () => {
        findStandardRebaseButton().vm.$emit('click');

        await nextTick();

        expect(rebaseMock).toHaveBeenCalledWith({ skipCi: false });
      });

      it('starts the CI-skipping rebase when clicking on "Rebase without CI"', async () => {
        findRebaseWithoutCiButton().vm.$emit('click');

        await nextTick();

        expect(rebaseMock).toHaveBeenCalledWith({ skipCi: true });
      });
    });

    describe('Rebase when pipelines must succeed is enabled', () => {
      beforeEach(() => {
        createWrapper({
          mr: {
            rebaseInProgress: false,
            canPushToSourceBranch: true,
            onlyAllowMergeIfPipelineSucceeds: true,
          },
          service: {
            rebase: rebaseMock,
            poll: pollMock,
          },
        });
      });

      it('renders only the rebase button', () => {
        expect(findRebaseWithoutCiButton().exists()).toBe(false);
        expect(findStandardRebaseButton().exists()).toBe(true);
      });

      it('starts the rebase when clicking', async () => {
        findStandardRebaseButton().vm.$emit('click');

        await nextTick();

        expect(rebaseMock).toHaveBeenCalledWith({ skipCi: false });
      });
    });

    describe('Rebase when pipelines must succeed and skipped pipelines are considered successful are enabled', () => {
      beforeEach(() => {
        createWrapper({
          mr: {
            rebaseInProgress: false,
            canPushToSourceBranch: true,
            onlyAllowMergeIfPipelineSucceeds: true,
            allowMergeOnSkippedPipeline: true,
          },
          service: {
            rebase: rebaseMock,
            poll: pollMock,
          },
        });
      });

      it('renders both rebase buttons', () => {
        expect(findRebaseWithoutCiButton().exists()).toBe(true);
        expect(findStandardRebaseButton().exists()).toBe(true);
      });

      it('starts the rebase when clicking', async () => {
        findStandardRebaseButton().vm.$emit('click');

        await nextTick();

        expect(rebaseMock).toHaveBeenCalledWith({ skipCi: false });
      });

      it('starts the CI-skipping rebase when clicking on "Rebase without CI"', async () => {
        findRebaseWithoutCiButton().vm.$emit('click');

        await nextTick();

        expect(rebaseMock).toHaveBeenCalledWith({ skipCi: true });
      });
    });
  });

  describe('without permissions', () => {
    const exampleTargetBranch = 'fake-branch-to-test-with';

    describe('UI text', () => {
      beforeEach(() => {
        createWrapper({
          mr: {
            rebaseInProgress: false,
            canPushToSourceBranch: false,
            targetBranch: exampleTargetBranch,
          },
          service: {},
        });
      });

      it('renders a message explaining user does not have permissions', () => {
        const text = findRebaseMessageText();

        expect(text).toContain('Merge blocked:');
        expect(text).toContain('the source branch must be rebased');
      });

      it('renders the correct target branch name', () => {
        const text = findRebaseMessageText();

        expect(text).toContain('Merge blocked:');
        expect(text).toContain('the source branch must be rebased onto the target branch.');
      });
    });

    it('does render the "Rebase without pipeline" button', () => {
      createWrapper({
        mr: {
          rebaseInProgress: false,
          canPushToSourceBranch: false,
          targetBranch: exampleTargetBranch,
        },
        service: {},
      });

      expect(findRebaseWithoutCiButton().exists()).toBe(true);
    });
  });

  describe('methods', () => {
    it('checkRebaseStatus', async () => {
      jest.spyOn(eventHub, '$emit').mockImplementation(() => {});
      createWrapper({
        mr: {},
        service: {
          rebase() {
            return Promise.resolve();
          },
          poll() {
            return Promise.resolve({
              data: {
                rebase_in_progress: false,
                should_be_rebased: false,
                merge_error: null,
              },
            });
          },
        },
      });

      wrapper.vm.rebase();

      // Wait for the rebase request
      await nextTick();
      // Wait for the polling request
      await nextTick();
      // Wait for the eventHub to be called
      await nextTick();

      expect(eventHub.$emit).toHaveBeenCalledWith('MRWidgetRebaseSuccess');
      expect(toast).toHaveBeenCalledWith('Rebase completed');
    });
  });
});