Welcome to mirror list, hosted at ThFree Co, Russian Federation.

cancel_jobs_modal_spec.js « components « jobs « admin « pages « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d90393d8ab31e7f66f8a1df195166ec3b8e37089 (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
import { nextTick } from 'vue';
import { mount } from '@vue/test-utils';
import { GlModal } from '@gitlab/ui';
import { TEST_HOST } from 'helpers/test_constants';
import axios from '~/lib/utils/axios_utils';
import { redirectTo } from '~/lib/utils/url_utility'; // eslint-disable-line import/no-deprecated
import CancelJobsModal from '~/pages/admin/jobs/components/cancel_jobs_modal.vue';
import { setVueErrorHandler } from '../../../../__helpers__/set_vue_error_handler';

jest.mock('~/lib/utils/url_utility', () => ({
  ...jest.requireActual('~/lib/utils/url_utility'),
  redirectTo: jest.fn(),
}));

describe('Cancel jobs modal', () => {
  const props = {
    url: `${TEST_HOST}/cancel_jobs_modal.vue/cancelAll`,
    modalId: 'cancel-jobs-modal',
  };
  let wrapper;

  beforeEach(() => {
    wrapper = mount(CancelJobsModal, { propsData: props });
  });

  describe('on submit', () => {
    it('cancels jobs and redirects to overview page', async () => {
      const responseURL = `${TEST_HOST}/cancel_jobs_modal.vue/jobs`;
      // TODO: We can't use axios-mock-adapter because our current version
      // does not support responseURL
      //
      // see https://gitlab.com/gitlab-org/gitlab/-/issues/375308 for details
      jest.spyOn(axios, 'post').mockImplementation((url) => {
        expect(url).toBe(props.url);
        return Promise.resolve({
          request: {
            responseURL,
          },
        });
      });

      wrapper.findComponent(GlModal).vm.$emit('primary');
      await nextTick();

      expect(redirectTo).toHaveBeenCalledWith(responseURL); // eslint-disable-line import/no-deprecated
    });

    it('displays error if canceling jobs failed', async () => {
      const dummyError = new Error('canceling jobs failed');
      // TODO: We can't use axios-mock-adapter because our current version
      // does not support responseURL
      //
      // see https://gitlab.com/gitlab-org/gitlab/-/issues/375308 for details
      jest.spyOn(axios, 'post').mockImplementation((url) => {
        expect(url).toBe(props.url);
        return Promise.reject(dummyError);
      });

      setVueErrorHandler({ instance: wrapper.vm, handler: () => {} }); // silencing thrown error
      wrapper.findComponent(GlModal).vm.$emit('primary');
      await nextTick();

      expect(redirectTo).not.toHaveBeenCalled(); // eslint-disable-line import/no-deprecated
    });
  });
});