summaryrefslogtreecommitdiff
path: root/spec/javascripts/vue_mr_widget/components/states/mr_widget_merged_spec.js
blob: 033cb6942493b46afda1b7a3f7c7bc67e5ef703d (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
import Vue from 'vue';
import mergedComponent from '~/vue_merge_request_widget/components/states/mr_widget_merged.vue';
import eventHub from '~/vue_merge_request_widget/event_hub';
import mountComponent from 'spec/helpers/vue_mount_component_helper';

describe('MRWidgetMerged', () => {
  let vm;
  const targetBranch = 'foo';
  const selectors = {
    get copyMergeShaButton() {
      return vm.$el.querySelector('button.js-mr-merged-copy-sha');
    },
    get mergeCommitShaLink() {
      return vm.$el.querySelector('a.js-mr-merged-commit-sha');
    },
  };

  beforeEach(() => {
    const Component = Vue.extend(mergedComponent);
    const mr = {
      isRemovingSourceBranch: false,
      cherryPickInForkPath: false,
      canCherryPickInCurrentMR: true,
      revertInForkPath: false,
      canRevertInCurrentMR: true,
      canRemoveSourceBranch: true,
      sourceBranchRemoved: true,
      metrics: {
        mergedBy: {
          name: 'Administrator',
          username: 'root',
          webUrl: 'http://localhost:3000/root',
          avatarUrl: 'http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon',
        },
        mergedAt: 'Jan 24, 2018 1:02pm GMT+0000',
        readableMergedAt: '',
        closedBy: {},
        closedAt: 'Jan 24, 2018 1:02pm GMT+0000',
        readableClosedAt: '',
      },
      updatedAt: 'mergedUpdatedAt',
      shortMergeCommitSha: '958c0475',
      mergeCommitSha: '958c047516e182dfc52317f721f696e8a1ee85ed',
      mergeCommitPath: 'http://localhost:3000/root/nautilus/commit/f7ce827c314c9340b075657fd61c789fb01cf74d',
      sourceBranch: 'bar',
      targetBranch,
    };

    const service = {
      removeSourceBranch() {},
    };

    spyOn(eventHub, '$emit');

    vm = mountComponent(Component, { mr, service });
  });

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

  describe('computed', () => {
    describe('shouldShowRemoveSourceBranch', () => {
      it('returns true when sourceBranchRemoved is false', () => {
        vm.mr.sourceBranchRemoved = false;
        expect(vm.shouldShowRemoveSourceBranch).toEqual(true);
      });

      it('returns false wehn sourceBranchRemoved is true', () => {
        vm.mr.sourceBranchRemoved = true;
        expect(vm.shouldShowRemoveSourceBranch).toEqual(false);
      });

      it('returns false when canRemoveSourceBranch is false', () => {
        vm.mr.sourceBranchRemoved = false;
        vm.mr.canRemoveSourceBranch = false;
        expect(vm.shouldShowRemoveSourceBranch).toEqual(false);
      });

      it('returns false when is making request', () => {
        vm.mr.canRemoveSourceBranch = true;
        vm.isMakingRequest = true;
        expect(vm.shouldShowRemoveSourceBranch).toEqual(false);
      });

      it('returns true when all are true', () => {
        vm.mr.isRemovingSourceBranch = true;
        vm.mr.canRemoveSourceBranch = true;
        vm.isMakingRequest = true;
        expect(vm.shouldShowRemoveSourceBranch).toEqual(false);
      });
    });

    describe('shouldShowSourceBranchRemoving', () => {
      it('should correct value when fields changed', () => {
        vm.mr.sourceBranchRemoved = false;
        expect(vm.shouldShowSourceBranchRemoving).toEqual(false);

        vm.mr.sourceBranchRemoved = true;
        expect(vm.shouldShowRemoveSourceBranch).toEqual(false);

        vm.mr.sourceBranchRemoved = false;
        vm.isMakingRequest = true;
        expect(vm.shouldShowSourceBranchRemoving).toEqual(true);

        vm.isMakingRequest = false;
        vm.mr.isRemovingSourceBranch = true;
        expect(vm.shouldShowSourceBranchRemoving).toEqual(true);
      });
    });
  });

  describe('methods', () => {
    describe('removeSourceBranch', () => {
      it('should set flag and call service then request main component to update the widget', (done) => {
        spyOn(vm.service, 'removeSourceBranch').and.returnValue(new Promise((resolve) => {
          resolve({
            data: {
              message: 'Branch was removed',
            },
          });
        }));

        vm.removeSourceBranch();
        setTimeout(() => {
          const args = eventHub.$emit.calls.argsFor(0);
          expect(vm.isMakingRequest).toEqual(true);
          expect(args[0]).toEqual('MRWidgetUpdateRequested');
          expect(args[1]).not.toThrow();
          done();
        }, 333);
      });
    });
  });

  it('has merged by information', () => {
    expect(vm.$el.textContent).toContain('Merged by');
    expect(vm.$el.textContent).toContain('Administrator');
  });

  it('renders branch information', () => {
    expect(vm.$el.textContent).toContain('The changes were merged into');
    expect(vm.$el.textContent).toContain(targetBranch);
  });

  it('renders information about branch being removed', () => {
    expect(vm.$el.textContent).toContain('The source branch has been removed');
  });

  it('shows revert and cherry-pick buttons', () => {
    expect(vm.$el.textContent).toContain('Revert');
    expect(vm.$el.textContent).toContain('Cherry-pick');
  });

  it('shows button to copy commit SHA to clipboard', () => {
    expect(selectors.copyMergeShaButton).toExist();
    expect(selectors.copyMergeShaButton.getAttribute('data-clipboard-text')).toBe(vm.mr.mergeCommitSha);
  });

  it('hides button to copy commit SHA if SHA does not exist', (done) => {
    vm.mr.mergeCommitSha = null;

    Vue.nextTick(() => {
      expect(selectors.copyMergeShaButton).not.toExist();
      expect(vm.$el.querySelector('.mr-info-list').innerText).not.toContain('with');
      done();
    });
  });

  it('shows merge commit SHA link', () => {
    expect(selectors.mergeCommitShaLink).toExist();
    expect(selectors.mergeCommitShaLink.text).toContain(vm.mr.shortMergeCommitSha);
    expect(selectors.mergeCommitShaLink.href).toBe(vm.mr.mergeCommitPath);
  });

  it('should not show source branch removed text', (done) => {
    vm.mr.sourceBranchRemoved = false;

    Vue.nextTick(() => {
      expect(vm.$el.innerText).toContain('You can remove source branch now');
      expect(vm.$el.innerText).not.toContain('The source branch has been removed');
      done();
    });
  });

  it('should show source branch removing text', (done) => {
    vm.mr.isRemovingSourceBranch = true;
    vm.mr.sourceBranchRemoved = false;

    Vue.nextTick(() => {
      expect(vm.$el.innerText).toContain('The source branch is being removed');
      expect(vm.$el.innerText).not.toContain('You can remove source branch now');
      expect(vm.$el.innerText).not.toContain('The source branch has been removed');
      done();
    });
  });

  it('should use mergedEvent mergedAt as tooltip title', () => {
    expect(
      vm.$el.querySelector('time').getAttribute('data-original-title'),
    ).toBe('Jan 24, 2018 1:02pm GMT+0000');
  });
});