summaryrefslogtreecommitdiff
path: root/spec/frontend/vue_mr_widget/components/mr_widget_rebase_spec.js
blob: d3221cc2fc742fd24b8c81510841f651a6328ece (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
import { shallowMount } 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';

let wrapper;

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

describe('Merge request widget rebase component', () => {
  const findRebaseMessageEl = () => wrapper.find('[data-testid="rebase-message"]');
  const findRebaseMessageElText = () => findRebaseMessageEl().text();

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

  [true, false].forEach((mergeRequestWidgetGraphql) => {
    describe(`widget graphql is ${mergeRequestWidgetGraphql ? 'enabled' : 'dislabed'}`, () => {
      describe('While rebasing', () => {
        it('should show progress message', () => {
          factory(
            {
              mr: { rebaseInProgress: true },
              service: {},
            },
            mergeRequestWidgetGraphql,
          );

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

      describe('With permissions', () => {
        it('it should render rebase button and warning message', () => {
          factory(
            {
              mr: {
                rebaseInProgress: false,
                canPushToSourceBranch: true,
              },
              service: {},
            },
            mergeRequestWidgetGraphql,
          );

          const text = findRebaseMessageElText();

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

        it('it should render error message when it fails', async () => {
          factory(
            {
              mr: {
                rebaseInProgress: false,
                canPushToSourceBranch: true,
              },
              service: {},
            },
            mergeRequestWidgetGraphql,
          );

          wrapper.setData({ rebasingError: 'Something went wrong!' });

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

      describe('Without permissions', () => {
        it('should render a message explaining user does not have permissions', () => {
          factory(
            {
              mr: {
                rebaseInProgress: false,
                canPushToSourceBranch: false,
                targetBranch: 'foo',
              },
              service: {},
            },
            mergeRequestWidgetGraphql,
          );

          const text = findRebaseMessageElText();

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

        it('should render the correct target branch name', () => {
          const targetBranch = 'fake-branch-to-test-with';
          factory(
            {
              mr: {
                rebaseInProgress: false,
                canPushToSourceBranch: false,
                targetBranch,
              },
              service: {},
            },
            mergeRequestWidgetGraphql,
          );

          const elem = findRebaseMessageEl();

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

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

          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');
        });
      });
    });
  });
});