summaryrefslogtreecommitdiff
path: root/spec/javascripts/pages/admin/jobs/index/components/stop_jobs_modal_spec.js
blob: a6fe9fb65e9c173053d9912a4aab8fb807f4fc89 (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
import Vue from 'vue';

import axios from '~/lib/utils/axios_utils';
import stopJobsModal from '~/pages/admin/jobs/index/components/stop_jobs_modal.vue';
import * as urlUtility from '~/lib/utils/url_utility';

import mountComponent from 'spec/helpers/vue_mount_component_helper';

describe('stop_jobs_modal.vue', () => {
  const props = {
    url: `${gl.TEST_HOST}/stop_jobs_modal.vue/stopAll`,
  };
  let vm;

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

  beforeEach(() => {
    const Component = Vue.extend(stopJobsModal);
    vm = mountComponent(Component, props);
  });

  describe('onSubmit', () => {
    it('stops jobs and redirects to overview page', (done) => {
      const responseURL = `${gl.TEST_HOST}/stop_jobs_modal.vue/jobs`;
      const redirectSpy = spyOn(urlUtility, 'redirectTo');
      spyOn(axios, 'post').and.callFake((url) => {
        expect(url).toBe(props.url);
        return Promise.resolve({
          request: {
            responseURL,
          },
        });
      });

      vm.onSubmit()
      .then(() => {
        expect(redirectSpy).toHaveBeenCalledWith(responseURL);
      })
      .then(done)
      .catch(done.fail);
    });

    it('displays error if stopping jobs failed', (done) => {
      const dummyError = new Error('stopping jobs failed');
      const redirectSpy = spyOn(urlUtility, 'redirectTo');
      spyOn(axios, 'post').and.callFake((url) => {
        expect(url).toBe(props.url);
        return Promise.reject(dummyError);
      });

      vm.onSubmit()
        .then(done.fail)
        .catch((error) => {
          expect(error).toBe(dummyError);
          expect(redirectSpy).not.toHaveBeenCalled();
        })
        .then(done)
        .catch(done.fail);
    });
  });
});