2021-02-14 18:09:20 +00:00
|
|
|
import { GlDropdown, GlDropdownItem } from '@gitlab/ui';
|
2021-02-24 21:11:16 +00:00
|
|
|
import { shallowMount } from '@vue/test-utils';
|
2020-05-06 18:09:38 +00:00
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
2020-08-17 21:09:56 +00:00
|
|
|
import waitForPromises from 'helpers/wait_for_promises';
|
2021-02-14 18:09:20 +00:00
|
|
|
import { TEST_HOST } from 'spec/test_constants';
|
2021-02-04 12:09:25 +00:00
|
|
|
import createFlash from '~/flash';
|
2020-05-06 18:09:38 +00:00
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2021-02-25 12:11:01 +00:00
|
|
|
import PipelinesManualActions from '~/pipelines/components/pipelines_list/pipelines_manual_actions.vue';
|
2020-05-06 18:09:38 +00:00
|
|
|
import GlCountdown from '~/vue_shared/components/gl_countdown.vue';
|
|
|
|
|
2021-02-04 12:09:25 +00:00
|
|
|
jest.mock('~/flash');
|
|
|
|
|
2020-05-06 18:09:38 +00:00
|
|
|
describe('Pipelines Actions dropdown', () => {
|
|
|
|
let wrapper;
|
|
|
|
let mock;
|
|
|
|
|
2021-02-04 12:09:25 +00:00
|
|
|
const createComponent = (props, mountFn = shallowMount) => {
|
2021-02-25 12:11:01 +00:00
|
|
|
wrapper = mountFn(PipelinesManualActions, {
|
2020-05-06 18:09:38 +00:00
|
|
|
propsData: {
|
2021-02-04 12:09:25 +00:00
|
|
|
...props,
|
2020-05-06 18:09:38 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-02-04 12:09:25 +00:00
|
|
|
const findDropdown = () => wrapper.find(GlDropdown);
|
|
|
|
const findAllDropdownItems = () => wrapper.findAll(GlDropdownItem);
|
2020-05-06 18:09:38 +00:00
|
|
|
const findAllCountdowns = () => wrapper.findAll(GlCountdown);
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mock = new MockAdapter(axios);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
wrapper.destroy();
|
|
|
|
wrapper = null;
|
|
|
|
|
|
|
|
mock.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('manual actions', () => {
|
|
|
|
const mockActions = [
|
|
|
|
{
|
|
|
|
name: 'stop_review',
|
|
|
|
path: `${TEST_HOST}/root/review-app/builds/1893/play`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'foo',
|
|
|
|
path: `${TEST_HOST}/disabled/pipeline/action`,
|
|
|
|
playable: false,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2021-02-04 12:09:25 +00:00
|
|
|
createComponent({ actions: mockActions });
|
2020-05-06 18:09:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('renders a dropdown with the provided actions', () => {
|
|
|
|
expect(findAllDropdownItems()).toHaveLength(mockActions.length);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("renders a disabled action when it's not playable", () => {
|
2020-12-23 12:10:26 +00:00
|
|
|
expect(findAllDropdownItems().at(1).attributes('disabled')).toBe('true');
|
2020-05-06 18:09:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('on click', () => {
|
2021-02-04 12:09:25 +00:00
|
|
|
it('makes a request and toggles the loading state', async () => {
|
2020-05-06 18:09:38 +00:00
|
|
|
mock.onPost(mockActions.path).reply(200);
|
|
|
|
|
2021-02-04 12:09:25 +00:00
|
|
|
findAllDropdownItems().at(0).vm.$emit('click');
|
|
|
|
|
|
|
|
await wrapper.vm.$nextTick();
|
|
|
|
expect(findDropdown().props('loading')).toBe(true);
|
|
|
|
|
|
|
|
await waitForPromises();
|
|
|
|
expect(findDropdown().props('loading')).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('makes a failed request and toggles the loading state', async () => {
|
|
|
|
mock.onPost(mockActions.path).reply(500);
|
|
|
|
|
|
|
|
findAllDropdownItems().at(0).vm.$emit('click');
|
2020-05-06 18:09:38 +00:00
|
|
|
|
2021-02-04 12:09:25 +00:00
|
|
|
await wrapper.vm.$nextTick();
|
|
|
|
expect(findDropdown().props('loading')).toBe(true);
|
2020-05-06 18:09:38 +00:00
|
|
|
|
2021-02-04 12:09:25 +00:00
|
|
|
await waitForPromises();
|
|
|
|
expect(findDropdown().props('loading')).toBe(false);
|
|
|
|
expect(createFlash).toHaveBeenCalledTimes(1);
|
2020-05-06 18:09:38 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('scheduled jobs', () => {
|
|
|
|
const scheduledJobAction = {
|
|
|
|
name: 'scheduled action',
|
|
|
|
path: `${TEST_HOST}/scheduled/job/action`,
|
|
|
|
playable: true,
|
|
|
|
scheduled_at: '2063-04-05T00:42:00Z',
|
|
|
|
};
|
|
|
|
const expiredJobAction = {
|
|
|
|
name: 'expired action',
|
|
|
|
path: `${TEST_HOST}/expired/job/action`,
|
|
|
|
playable: true,
|
|
|
|
scheduled_at: '2018-10-05T08:23:00Z',
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.spyOn(Date, 'now').mockImplementation(() => new Date('2063-04-04T00:42:00Z').getTime());
|
2021-02-04 12:09:25 +00:00
|
|
|
createComponent({ actions: [scheduledJobAction, expiredJobAction] });
|
2020-05-06 18:09:38 +00:00
|
|
|
});
|
|
|
|
|
2021-02-04 12:09:25 +00:00
|
|
|
it('makes post request after confirming', async () => {
|
2020-05-06 18:09:38 +00:00
|
|
|
mock.onPost(scheduledJobAction.path).reply(200);
|
|
|
|
jest.spyOn(window, 'confirm').mockReturnValue(true);
|
|
|
|
|
2020-12-23 12:10:26 +00:00
|
|
|
findAllDropdownItems().at(0).vm.$emit('click');
|
2020-05-06 18:09:38 +00:00
|
|
|
|
|
|
|
expect(window.confirm).toHaveBeenCalled();
|
|
|
|
|
2021-02-04 12:09:25 +00:00
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
expect(mock.history.post).toHaveLength(1);
|
2020-05-06 18:09:38 +00:00
|
|
|
});
|
|
|
|
|
2021-02-04 12:09:25 +00:00
|
|
|
it('does not make post request if confirmation is cancelled', async () => {
|
2020-05-06 18:09:38 +00:00
|
|
|
mock.onPost(scheduledJobAction.path).reply(200);
|
|
|
|
jest.spyOn(window, 'confirm').mockReturnValue(false);
|
|
|
|
|
2020-12-23 12:10:26 +00:00
|
|
|
findAllDropdownItems().at(0).vm.$emit('click');
|
2020-05-06 18:09:38 +00:00
|
|
|
|
|
|
|
expect(window.confirm).toHaveBeenCalled();
|
2021-02-04 12:09:25 +00:00
|
|
|
|
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
expect(mock.history.post).toHaveLength(0);
|
2020-05-06 18:09:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('displays the remaining time in the dropdown', () => {
|
2020-12-23 12:10:26 +00:00
|
|
|
expect(findAllCountdowns().at(0).props('endDateString')).toBe(
|
|
|
|
scheduledJobAction.scheduled_at,
|
|
|
|
);
|
2020-05-06 18:09:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('displays 00:00:00 for expired jobs in the dropdown', () => {
|
2020-12-23 12:10:26 +00:00
|
|
|
expect(findAllCountdowns().at(1).props('endDateString')).toBe(expiredJobAction.scheduled_at);
|
2020-05-06 18:09:38 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|