mr_widget_options_spec.js 13.2 KB
Newer Older
Fatih Acet's avatar
Fatih Acet committed
1
import Vue from 'vue';
2
import mrWidgetOptions from '~/vue_merge_request_widget/mr_widget_options.vue';
Fatih Acet's avatar
Fatih Acet committed
3
import eventHub from '~/vue_merge_request_widget/event_hub';
4
import notify from '~/lib/utils/notify';
5
import { stateKey } from '~/vue_merge_request_widget/stores/state_maps';
6
import mountComponent from 'spec/helpers/vue_mount_component_helper';
Fatih Acet's avatar
Fatih Acet committed
7 8 9 10
import mockData from './mock_data';

const returnPromise = data => new Promise((resolve) => {
  resolve({
11
    data,
Fatih Acet's avatar
Fatih Acet committed
12 13 14
  });
});

Phil Hughes's avatar
Phil Hughes committed
15
describe('mrWidgetOptions', () => {
Fatih Acet's avatar
Fatih Acet committed
16
  let vm;
17
  let MrWidgetOptions;
Fatih Acet's avatar
Fatih Acet committed
18 19

  beforeEach(() => {
20 21 22 23 24 25 26
    // Prevent component mounting
    delete mrWidgetOptions.el;

    MrWidgetOptions = Vue.extend(mrWidgetOptions);
    vm = mountComponent(MrWidgetOptions, {
      mrData: { ...mockData },
    });
Fatih Acet's avatar
Fatih Acet committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
  });

  describe('data', () => {
    it('should instantiate Store and Service', () => {
      expect(vm.mr).toBeDefined();
      expect(vm.service).toBeDefined();
    });
  });

  describe('computed', () => {
    describe('componentName', () => {
      it('should return merged component', () => {
        expect(vm.componentName).toEqual('mr-widget-merged');
      });

      it('should return conflicts component', () => {
        vm.mr.state = 'conflicts';
        expect(vm.componentName).toEqual('mr-widget-conflicts');
      });
    });

    describe('shouldRenderMergeHelp', () => {
49
      it('should return false for the initial merged state', () => {
Fatih Acet's avatar
Fatih Acet committed
50 51 52
        expect(vm.shouldRenderMergeHelp).toBeFalsy();
      });

53 54
      it('should return true for a state which requires help widget', () => {
        vm.mr.state = 'conflicts';
Fatih Acet's avatar
Fatih Acet committed
55 56 57 58 59
        expect(vm.shouldRenderMergeHelp).toBeTruthy();
      });
    });

    describe('shouldRenderPipelines', () => {
60 61
      it('should return true when hasCI is true', () => {
        vm.mr.hasCI = true;
Fatih Acet's avatar
Fatih Acet committed
62 63 64 65

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

66
      it('should return false when hasCI is false', () => {
Fatih Acet's avatar
Fatih Acet committed
67 68 69 70 71 72 73 74 75 76 77 78
        vm.mr.hasCI = false;

        expect(vm.shouldRenderPipelines).toBeFalsy();
      });
    });

    describe('shouldRenderRelatedLinks', () => {
      it('should return false for the initial data', () => {
        expect(vm.shouldRenderRelatedLinks).toBeFalsy();
      });

      it('should return true if there is relatedLinks in MR', () => {
79
        Vue.set(vm.mr, 'relatedLinks', {});
Fatih Acet's avatar
Fatih Acet committed
80 81 82 83
        expect(vm.shouldRenderRelatedLinks).toBeTruthy();
      });
    });

84
    describe('shouldRenderSourceBranchRemovalStatus', () => {
85 86 87 88
      beforeEach(() => {
        vm.mr.state = 'readyToMerge';
      });

89 90 91 92
      it('should return true when cannot remove source branch and branch will be removed', () => {
        vm.mr.canRemoveSourceBranch = false;
        vm.mr.shouldRemoveSourceBranch = true;

93
        expect(vm.shouldRenderSourceBranchRemovalStatus).toEqual(true);
94 95 96 97 98 99
      });

      it('should return false when can remove source branch and branch will be removed', () => {
        vm.mr.canRemoveSourceBranch = true;
        vm.mr.shouldRemoveSourceBranch = true;

100
        expect(vm.shouldRenderSourceBranchRemovalStatus).toEqual(false);
101 102 103 104 105 106
      });

      it('should return false when cannot remove source branch and branch will not be removed', () => {
        vm.mr.canRemoveSourceBranch = false;
        vm.mr.shouldRemoveSourceBranch = false;

107
        expect(vm.shouldRenderSourceBranchRemovalStatus).toEqual(false);
108
      });
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

      it('should return false when in merged state', () => {
        vm.mr.canRemoveSourceBranch = false;
        vm.mr.shouldRemoveSourceBranch = true;
        vm.mr.state = 'merged';

        expect(vm.shouldRenderSourceBranchRemovalStatus).toEqual(false);
      });

      it('should return false when in nothing to merge state', () => {
        vm.mr.canRemoveSourceBranch = false;
        vm.mr.shouldRemoveSourceBranch = true;
        vm.mr.state = 'nothingToMerge';

        expect(vm.shouldRenderSourceBranchRemovalStatus).toEqual(false);
      });
125
    });
Fatih Acet's avatar
Fatih Acet committed
126 127 128 129 130 131 132
  });

  describe('methods', () => {
    describe('checkStatus', () => {
      it('should tell service to check status', (done) => {
        spyOn(vm.service, 'checkStatus').and.returnValue(returnPromise(mockData));
        spyOn(vm.mr, 'setData');
133 134
        spyOn(vm, 'handleNotification');

Fatih Acet's avatar
Fatih Acet committed
135 136 137 138 139 140 141 142 143 144
        let isCbExecuted = false;
        const cb = () => {
          isCbExecuted = true;
        };

        vm.checkStatus(cb);

        setTimeout(() => {
          expect(vm.service.checkStatus).toHaveBeenCalled();
          expect(vm.mr.setData).toHaveBeenCalled();
145
          expect(vm.handleNotification).toHaveBeenCalledWith(mockData);
Fatih Acet's avatar
Fatih Acet committed
146 147 148 149 150 151 152 153
          expect(isCbExecuted).toBeTruthy();
          done();
        }, 333);
      });
    });

    describe('initPolling', () => {
      it('should call SmartInterval', () => {
154 155
        spyOn(vm, 'checkStatus').and.returnValue(Promise.resolve());
        jasmine.clock().install();
Fatih Acet's avatar
Fatih Acet committed
156 157
        vm.initPolling();

158 159 160 161
        expect(vm.checkStatus).not.toHaveBeenCalled();

        jasmine.clock().tick(10000);

Fatih Acet's avatar
Fatih Acet committed
162
        expect(vm.pollingInterval).toBeDefined();
163 164 165
        expect(vm.checkStatus).toHaveBeenCalled();

        jasmine.clock().uninstall();
Fatih Acet's avatar
Fatih Acet committed
166 167 168 169 170
      });
    });

    describe('initDeploymentsPolling', () => {
      it('should call SmartInterval', () => {
171
        spyOn(vm, 'fetchDeployments').and.returnValue(Promise.resolve());
Fatih Acet's avatar
Fatih Acet committed
172 173 174
        vm.initDeploymentsPolling();

        expect(vm.deploymentsInterval).toBeDefined();
175
        expect(vm.fetchDeployments).toHaveBeenCalled();
Fatih Acet's avatar
Fatih Acet committed
176 177 178 179 180
      });
    });

    describe('fetchDeployments', () => {
      it('should fetch deployments', (done) => {
Phil Hughes's avatar
Phil Hughes committed
181
        spyOn(vm.service, 'fetchDeployments').and.returnValue(returnPromise([{ id: 1 }]));
Fatih Acet's avatar
Fatih Acet committed
182 183 184 185 186 187

        vm.fetchDeployments();

        setTimeout(() => {
          expect(vm.service.fetchDeployments).toHaveBeenCalled();
          expect(vm.mr.deployments.length).toEqual(1);
Phil Hughes's avatar
Phil Hughes committed
188
          expect(vm.mr.deployments[0].id).toBe(1);
Fatih Acet's avatar
Fatih Acet committed
189
          done();
Phil Hughes's avatar
Phil Hughes committed
190
        });
Fatih Acet's avatar
Fatih Acet committed
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
      });
    });

    describe('fetchActionsContent', () => {
      it('should fetch content of Cherry Pick and Revert modals', (done) => {
        spyOn(vm.service, 'fetchMergeActionsContent').and.returnValue(returnPromise('hello world'));

        vm.fetchActionsContent();

        setTimeout(() => {
          expect(vm.service.fetchMergeActionsContent).toHaveBeenCalled();
          expect(document.body.textContent).toContain('hello world');
          done();
        }, 333);
      });
    });

    describe('bindEventHubListeners', () => {
      it('should bind eventHub listeners', () => {
        spyOn(vm, 'checkStatus').and.returnValue(() => {});
        spyOn(vm.service, 'checkStatus').and.returnValue(returnPromise(mockData));
        spyOn(vm, 'fetchActionsContent');
        spyOn(vm.mr, 'setData');
        spyOn(vm, 'resumePolling');
        spyOn(vm, 'stopPolling');
        spyOn(eventHub, '$on');

        vm.bindEventHubListeners();

        eventHub.$emit('SetBranchRemoveFlag', ['flag']);
        expect(vm.mr.isRemovingSourceBranch).toEqual('flag');

        eventHub.$emit('FailedToMerge');
        expect(vm.mr.state).toEqual('failedToMerge');

        eventHub.$emit('UpdateWidgetData', mockData);
        expect(vm.mr.setData).toHaveBeenCalledWith(mockData);

        eventHub.$emit('EnablePolling');
        expect(vm.resumePolling).toHaveBeenCalled();

        eventHub.$emit('DisablePolling');
        expect(vm.stopPolling).toHaveBeenCalled();

        const listenersWithServiceRequest = {
          MRWidgetUpdateRequested: true,
          FetchActionsContent: true,
        };

        const allArgs = eventHub.$on.calls.allArgs();
        allArgs.forEach((params) => {
          const eventName = params[0];
          const callback = params[1];

          if (listenersWithServiceRequest[eventName]) {
            listenersWithServiceRequest[eventName] = callback;
          }
        });

        listenersWithServiceRequest.MRWidgetUpdateRequested();
        expect(vm.checkStatus).toHaveBeenCalled();

        listenersWithServiceRequest.FetchActionsContent();
        expect(vm.fetchActionsContent).toHaveBeenCalled();
      });
    });

    describe('handleMounted', () => {
      it('should call required methods to do the initial kick-off', () => {
        spyOn(vm, 'initDeploymentsPolling');
261
        spyOn(vm, 'setFaviconHelper');
Fatih Acet's avatar
Fatih Acet committed
262 263 264

        vm.handleMounted();

265
        expect(vm.setFaviconHelper).toHaveBeenCalled();
Fatih Acet's avatar
Fatih Acet committed
266 267 268 269 270
        expect(vm.initDeploymentsPolling).toHaveBeenCalled();
      });
    });

    describe('setFavicon', () => {
271 272 273 274 275 276 277
      let faviconElement;

      beforeEach(() => {
        const favicon = document.createElement('link');
        favicon.setAttribute('id', 'favicon');
        document.body.appendChild(favicon);

Phil Hughes's avatar
Phil Hughes committed
278
        faviconElement = document.getElementById('favicon');
279 280 281 282 283 284
      });

      afterEach(() => {
        document.body.removeChild(document.getElementById('favicon'));
      });

Fatih Acet's avatar
Fatih Acet committed
285
      it('should call setFavicon method', () => {
Phil Hughes's avatar
Phil Hughes committed
286
        vm.setFaviconHelper();
Fatih Acet's avatar
Fatih Acet committed
287

288
        expect(faviconElement.getAttribute('href')).toEqual(vm.mr.ciStatusFaviconPath);
Fatih Acet's avatar
Fatih Acet committed
289 290 291 292
      });

      it('should not call setFavicon when there is no ciStatusFaviconPath', () => {
        vm.mr.ciStatusFaviconPath = null;
293
        vm.setFaviconHelper();
Fatih Acet's avatar
Fatih Acet committed
294

295
        expect(faviconElement.getAttribute('href')).toEqual(null);
Fatih Acet's avatar
Fatih Acet committed
296 297 298
      });
    });

299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
    describe('handleNotification', () => {
      const data = {
        ci_status: 'running',
        title: 'title',
        pipeline: { details: { status: { label: 'running-label' } } },
      };

      beforeEach(() => {
        spyOn(notify, 'notifyMe');

        vm.mr.ciStatus = 'failed';
        vm.mr.gitlabLogo = 'logo.png';
      });

      it('should call notifyMe', () => {
        vm.handleNotification(data);

        expect(notify.notifyMe).toHaveBeenCalledWith(
          'Pipeline running-label',
          'Pipeline running-label for "title"',
          'logo.png',
        );
      });

      it('should not call notifyMe if the status has not changed', () => {
        vm.mr.ciStatus = data.ci_status;

        vm.handleNotification(data);

        expect(notify.notifyMe).not.toHaveBeenCalled();
      });
330 331 332 333 334 335 336 337 338

      it('should not notify if no pipeline provided', () => {
        vm.handleNotification({
          ...data,
          pipeline: undefined,
        });

        expect(notify.notifyMe).not.toHaveBeenCalled();
      });
339 340
    });

Fatih Acet's avatar
Fatih Acet committed
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
    describe('resumePolling', () => {
      it('should call stopTimer on pollingInterval', () => {
        spyOn(vm.pollingInterval, 'resume');

        vm.resumePolling();
        expect(vm.pollingInterval.resume).toHaveBeenCalled();
      });
    });

    describe('stopPolling', () => {
      it('should call stopTimer on pollingInterval', () => {
        spyOn(vm.pollingInterval, 'stopTimer');

        vm.stopPolling();
        expect(vm.pollingInterval.stopTimer).toHaveBeenCalled();
      });
    });
  });

360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
  describe('rendering relatedLinks', () => {
    beforeEach((done) => {
      vm.mr.relatedLinks = {
        assignToMe: null,
        closing: `
          <a class="close-related-link" href="#'>
            Close
          </a>
        `,
        mentioned: '',
      };
      Vue.nextTick(done);
    });

    it('renders if there are relatedLinks', () => {
      expect(vm.$el.querySelector('.close-related-link')).toBeDefined();
    });

    it('does not render if state is nothingToMerge', (done) => {
      vm.mr.state = stateKey.nothingToMerge;
      Vue.nextTick(() => {
        expect(vm.$el.querySelector('.close-related-link')).toBeNull();
        done();
      });
    });
  });
386 387 388 389 390

  describe('rendering source branch removal status', () => {
    it('renders when user cannot remove branch and branch should be removed', (done) => {
      vm.mr.canRemoveSourceBranch = false;
      vm.mr.shouldRemoveSourceBranch = true;
Phil Hughes's avatar
Phil Hughes committed
391
      vm.mr.state = 'readyToMerge';
392 393 394 395 396 397 398 399 400 401 402 403

      vm.$nextTick(() => {
        const tooltip = vm.$el.querySelector('.fa-question-circle');

        expect(vm.$el.textContent).toContain('Removes source branch');
        expect(tooltip.getAttribute('data-original-title')).toBe(
          'A user with write access to the source branch selected this option',
        );

        done();
      });
    });
404 405 406 407 408 409 410 411 412 413 414 415 416

    it('does not render in merged state', (done) => {
      vm.mr.canRemoveSourceBranch = false;
      vm.mr.shouldRemoveSourceBranch = true;
      vm.mr.state = 'merged';

      vm.$nextTick(() => {
        expect(vm.$el.textContent).toContain('The source branch has been removed');
        expect(vm.$el.textContent).not.toContain('Removes source branch');

        done();
      });
    });
417
  });
Phil Hughes's avatar
Phil Hughes committed
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447

  describe('rendering deployments', () => {
    const deploymentMockData = {
      id: 15,
      name: 'review/diplo',
      url: '/root/acets-review-apps/environments/15',
      stop_url: '/root/acets-review-apps/environments/15/stop',
      metrics_url: '/root/acets-review-apps/environments/15/deployments/1/metrics',
      metrics_monitoring_url: '/root/acets-review-apps/environments/15/metrics',
      external_url: 'http://diplo.',
      external_url_formatted: 'diplo.',
      deployed_at: '2017-03-22T22:44:42.258Z',
      deployed_at_formatted: 'Mar 22, 2017 10:44pm',
    };

    beforeEach((done) => {
      vm.mr.deployments.push({
        ...deploymentMockData,
      }, {
        ...deploymentMockData,
        id: deploymentMockData.id + 1,
      });

      vm.$nextTick(done);
    });

    it('renders multiple deployments', () => {
      expect(vm.$el.querySelectorAll('.deploy-heading').length).toBe(2);
    });
  });
Fatih Acet's avatar
Fatih Acet committed
448
});