summaryrefslogtreecommitdiff
path: root/spec/javascripts/vue_mr_widget/components/states/mr_widget_merge_when_pipeline_succeeds_spec.js
blob: 8d8b90cea168de9af431cf1ab313d31d3a1924ba (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
import Vue from 'vue';
import mwpsComponent from '~/vue_merge_request_widget/components/states/mr_widget_merge_when_pipeline_succeeds';
import eventHub from '~/vue_merge_request_widget/event_hub';

const targetBranchPath = '/foo/bar';
const targetBranch = 'foo';
const sha = '1EA2EZ34';

const createComponent = () => {
  const Component = Vue.extend(mwpsComponent);
  const mr = {
    shouldRemoveSourceBranch: false,
    canRemoveSourceBranch: true,
    canCancelAutomaticMerge: true,
    mergeUserId: 1,
    currentUserId: 1,
    setToMWPSBy: {},
    sha,
    targetBranchPath,
    targetBranch,
  };

  const service = {
    cancelAutomaticMerge() {},
    mergeResource: {
      save() {},
    },
  };

  return new Component({
    el: document.createElement('div'),
    propsData: { mr, service },
  });
};

describe('MRWidgetMergeWhenPipelineSucceeds', () => {
  describe('props', () => {
    it('should have props', () => {
      const { mr, service } = mwpsComponent.props;

      expect(mr.type instanceof Object).toBeTruthy();
      expect(mr.required).toBeTruthy();

      expect(service.type instanceof Object).toBeTruthy();
      expect(service.required).toBeTruthy();
    });
  });

  describe('components', () => {
    it('should have components added', () => {
      expect(mwpsComponent.components['mr-widget-author']).toBeDefined();
    });
  });

  describe('data', () => {
    it('should have default data', () => {
      const data = mwpsComponent.data();

      expect(data.isCancellingAutoMerge).toBeFalsy();
      expect(data.isRemovingSourceBranch).toBeFalsy();
    });
  });

  describe('computed', () => {
    describe('canRemoveSourceBranch', () => {
      it('should return true when user is able to remove source branch', () => {
        const vm = createComponent();

        expect(vm.canRemoveSourceBranch).toBeTruthy();
      });

      it('should return false when user id is not the same with who set the MWPS', () => {
        const vm = createComponent();

        vm.mr.mergeUserId = 2;
        expect(vm.canRemoveSourceBranch).toBeFalsy();

        vm.mr.currentUserId = 2;
        expect(vm.canRemoveSourceBranch).toBeTruthy();

        vm.mr.currentUserId = 3;
        expect(vm.canRemoveSourceBranch).toBeFalsy();
      });

      it('should return false when shouldRemoveSourceBranch set to false', () => {
        const vm = createComponent();

        vm.mr.shouldRemoveSourceBranch = true;
        expect(vm.canRemoveSourceBranch).toBeFalsy();
      });

      it('should return false if user is not able to remove the source branch', () => {
        const vm = createComponent();

        vm.mr.canRemoveSourceBranch = false;
        expect(vm.canRemoveSourceBranch).toBeFalsy();
      });
    });
  });

  describe('methods', () => {
    describe('cancelAutomaticMerge', () => {
      it('should set flag and call service then tell main component to update the widget with data', (done) => {
        const vm = createComponent();
        const mrObj = {
          is_new_mr_data: true,
        };
        spyOn(eventHub, '$emit');
        spyOn(vm.service, 'cancelAutomaticMerge').and.returnValue(new Promise((resolve) => {
          resolve({
            json() {
              return mrObj;
            },
          });
        }));

        vm.cancelAutomaticMerge();
        setTimeout(() => {
          expect(vm.isCancellingAutoMerge).toBeTruthy();
          expect(eventHub.$emit).toHaveBeenCalledWith('UpdateWidgetData', mrObj);
          done();
        }, 333);
      });
    });

    describe('removeSourceBranch', () => {
      it('should set flag and call service then request main component to update the widget', (done) => {
        const vm = createComponent();
        spyOn(eventHub, '$emit');
        spyOn(vm.service.mergeResource, 'save').and.returnValue(new Promise((resolve) => {
          resolve({
            json() {
              return {
                status: 'merge_when_pipeline_succeeds',
              };
            },
          });
        }));

        vm.removeSourceBranch();
        setTimeout(() => {
          expect(eventHub.$emit).toHaveBeenCalledWith('MRWidgetUpdateRequested');
          expect(vm.service.mergeResource.save).toHaveBeenCalledWith({
            sha,
            merge_when_pipeline_succeeds: true,
            should_remove_source_branch: true,
          });
          done();
        }, 333);
      });
    });
  });

  describe('template', () => {
    let vm;
    let el;

    beforeEach(() => {
      vm = createComponent();
      el = vm.$el;
    });

    it('should have correct elements', () => {
      expect(el.classList.contains('mr-widget-body')).toBeTruthy();
      expect(el.innerText).toContain('to be merged automatically when the pipeline succeeds.');
      expect(el.innerText).toContain('The changes will be merged into');
      expect(el.innerText).toContain(targetBranch);
      expect(el.innerText).toContain('The source branch will not be removed.');
      expect(el.querySelector('.js-cancel-auto-merge').innerText).toContain('Cancel automatic merge');
      expect(el.querySelector('.js-cancel-auto-merge').getAttribute('disabled')).toBeFalsy();
      expect(el.querySelector('.js-remove-source-branch').innerText).toContain('Remove source branch');
      expect(el.querySelector('.js-remove-source-branch').getAttribute('disabled')).toBeFalsy();
    });

    it('should disable cancel auto merge button when the action is in progress', (done) => {
      vm.isCancellingAutoMerge = true;

      Vue.nextTick(() => {
        expect(el.querySelector('.js-cancel-auto-merge').getAttribute('disabled')).toBeTruthy();
        done();
      });
    });

    it('should show source branch will be removed text when it source branch set to remove', (done) => {
      vm.mr.shouldRemoveSourceBranch = true;

      Vue.nextTick(() => {
        const normalizedText = el.innerText.replace(/\s+/g, ' ');
        expect(normalizedText).toContain('The source branch will be removed.');
        expect(normalizedText).not.toContain('The source branch will not be removed.');
        done();
      });
    });

    it('should not show remove source branch button when user not able to remove source branch', (done) => {
      vm.mr.currentUserId = 4;

      Vue.nextTick(() => {
        expect(el.querySelector('.js-remove-source-branch')).toEqual(null);
        done();
      });
    });

    it('should disable remove source branch button when the action is in progress', (done) => {
      vm.isRemovingSourceBranch = true;

      Vue.nextTick(() => {
        expect(el.querySelector('.js-remove-source-branch').getAttribute('disabled')).toBeTruthy();
        done();
      });
    });
  });
});