summaryrefslogtreecommitdiff
path: root/spec/javascripts/vue_mr_widget/components/mr_widget_rebase_spec.js
blob: 7216ad00cc1b95f8818b0b7232d402ba9000079a (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
import Vue from 'vue';
import eventHub from '~/vue_merge_request_widget/event_hub';
import component from '~/vue_merge_request_widget/components/states/mr_widget_rebase.vue';
import mountComponent from 'spec/helpers/vue_mount_component_helper';

describe('Merge request widget rebase component', () => {
  let Component;
  let vm;
  beforeEach(() => {
    Component = Vue.extend(component);
  });

  afterEach(() => {
    vm.$destroy();
  });

  describe('While rebasing', () => {
    it('should show progress message', () => {
      vm = mountComponent(Component, {
        mr: { rebaseInProgress: true },
        service: {},
      });

      expect(
        vm.$el.querySelector('.rebase-state-find-class-convention span').textContent.trim(),
      ).toContain('Rebase in progress');
    });
  });

  describe('With permissions', () => {
    beforeEach(() => {
      vm = mountComponent(Component, {
        mr: {
          rebaseInProgress: false,
          canPushToSourceBranch: true,
        },
        service: {},
      });
    });

    it('it should render rebase button and warning message', () => {
      const text = vm.$el
        .querySelector('.rebase-state-find-class-convention span')
        .textContent.trim();

      expect(text).toContain('Fast-forward merge is not possible.');
      expect(text.replace(/\s\s+/g, ' ')).toContain(
        'Rebase the source branch onto the target branch or merge target',
      );

      expect(text).toContain('branch into source branch to allow this merge request to be merged.');
    });

    it('it should render error message when it fails', done => {
      vm.rebasingError = 'Something went wrong!';

      Vue.nextTick(() => {
        expect(
          vm.$el.querySelector('.rebase-state-find-class-convention span').textContent.trim(),
        ).toContain('Something went wrong!');
        done();
      });
    });
  });

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

      const text = vm.$el
        .querySelector('.rebase-state-find-class-convention span')
        .textContent.trim();

      expect(text).toContain('Fast-forward merge is not possible.');
      expect(text).toContain('Rebase the source branch onto');
      expect(text).toContain('foo');
      expect(text.replace(/\s\s+/g, ' ')).toContain('to allow this merge request to be merged.');
    });

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

      const elem = vm.$el.querySelector('.rebase-state-find-class-convention span');

      expect(elem.innerHTML).toContain(
        `Fast-forward merge is not possible. Rebase the source branch onto <span class="label-branch">${targetBranch}</span> to allow this merge request to be merged.`,
      );
    });
  });

  describe('methods', () => {
    it('checkRebaseStatus', done => {
      spyOn(eventHub, '$emit');
      vm = mountComponent(Component, {
        mr: {},
        service: {
          rebase() {
            return Promise.resolve();
          },
          poll() {
            return Promise.resolve({
              data: {
                rebase_in_progress: false,
                merge_error: null,
              },
            });
          },
        },
      });

      vm.rebase();

      // Wait for the rebase request
      vm.$nextTick()
        // Wait for the polling request
        .then(vm.$nextTick())
        // Wait for the eventHub to be called
        .then(vm.$nextTick())
        .then(() => {
          expect(eventHub.$emit).toHaveBeenCalledWith('MRWidgetRebaseSuccess');
        })
        .then(done)
        .catch(done.fail);
    });
  });
});