debian-mirror-gitlab/spec/frontend/ide/stores/modules/pipelines/actions_spec.js

440 lines
11 KiB
JavaScript
Raw Normal View History

2018-11-08 19:23:39 +05:30
import MockAdapter from 'axios-mock-adapter';
2021-03-11 19:13:27 +05:30
import Visibility from 'visibilityjs';
2020-01-01 13:55:28 +05:30
import { TEST_HOST } from 'helpers/test_constants';
2021-03-08 18:12:59 +05:30
import testAction from 'helpers/vuex_action_helper';
2021-03-11 19:13:27 +05:30
import { rightSidebarViews } from '~/ide/constants';
2018-11-08 19:23:39 +05:30
import {
requestLatestPipeline,
receiveLatestPipelineError,
receiveLatestPipelineSuccess,
fetchLatestPipeline,
stopPipelinePolling,
clearEtagPoll,
requestJobs,
receiveJobsError,
receiveJobsSuccess,
fetchJobs,
toggleStageCollapsed,
setDetailJob,
2020-11-24 15:15:51 +05:30
requestJobLogs,
receiveJobLogsError,
receiveJobLogsSuccess,
fetchJobLogs,
2018-11-08 19:23:39 +05:30
resetLatestPipeline,
} from '~/ide/stores/modules/pipelines/actions';
import * as types from '~/ide/stores/modules/pipelines/mutation_types';
2021-03-11 19:13:27 +05:30
import state from '~/ide/stores/modules/pipelines/state';
import axios from '~/lib/utils/axios_utils';
2018-11-08 19:23:39 +05:30
import { pipelines, jobs } from '../../../mock_data';
describe('IDE pipelines actions', () => {
let mockedState;
let mock;
beforeEach(() => {
mockedState = state();
mock = new MockAdapter(axios);
gon.api_version = 'v4';
mockedState.currentProjectId = 'test/project';
});
afterEach(() => {
mock.restore();
});
describe('requestLatestPipeline', () => {
2021-03-08 18:12:59 +05:30
it('commits request', (done) => {
2018-11-08 19:23:39 +05:30
testAction(
requestLatestPipeline,
null,
mockedState,
[{ type: types.REQUEST_LATEST_PIPELINE }],
[],
done,
);
});
});
describe('receiveLatestPipelineError', () => {
2021-03-08 18:12:59 +05:30
it('commits error', (done) => {
2018-11-08 19:23:39 +05:30
testAction(
receiveLatestPipelineError,
{ status: 404 },
mockedState,
[{ type: types.RECEIVE_LASTEST_PIPELINE_ERROR }],
[{ type: 'stopPipelinePolling' }],
done,
);
});
2021-03-08 18:12:59 +05:30
it('dispatches setErrorMessage is not 404', (done) => {
2018-11-08 19:23:39 +05:30
testAction(
receiveLatestPipelineError,
{ status: 500 },
mockedState,
[{ type: types.RECEIVE_LASTEST_PIPELINE_ERROR }],
[
{
type: 'setErrorMessage',
payload: {
2020-03-13 15:44:24 +05:30
text: 'An error occurred while fetching the latest pipeline.',
2020-01-01 13:55:28 +05:30
action: expect.any(Function),
2018-11-08 19:23:39 +05:30
actionText: 'Please try again',
actionPayload: null,
},
},
{ type: 'stopPipelinePolling' },
],
done,
);
});
});
describe('receiveLatestPipelineSuccess', () => {
2020-01-01 13:55:28 +05:30
const rootGetters = { lastCommit: { id: '123' } };
2018-11-08 19:23:39 +05:30
let commit;
beforeEach(() => {
2020-01-01 13:55:28 +05:30
commit = jest.fn().mockName('commit');
2018-11-08 19:23:39 +05:30
});
it('commits pipeline', () => {
receiveLatestPipelineSuccess({ rootGetters, commit }, { pipelines });
2020-01-01 13:55:28 +05:30
expect(commit).toHaveBeenCalledWith(types.RECEIVE_LASTEST_PIPELINE_SUCCESS, pipelines[0]);
2018-11-08 19:23:39 +05:30
});
it('commits false when there are no pipelines', () => {
receiveLatestPipelineSuccess({ rootGetters, commit }, { pipelines: [] });
2020-01-01 13:55:28 +05:30
expect(commit).toHaveBeenCalledWith(types.RECEIVE_LASTEST_PIPELINE_SUCCESS, false);
2018-11-08 19:23:39 +05:30
});
});
describe('fetchLatestPipeline', () => {
afterEach(() => {
stopPipelinePolling();
clearEtagPoll();
});
describe('success', () => {
beforeEach(() => {
mock
.onGet('/abc/def/commit/abc123def456ghi789jkl/pipelines')
.reply(200, { data: { foo: 'bar' } }, { 'poll-interval': '10000' });
});
2021-03-08 18:12:59 +05:30
it('dispatches request', (done) => {
2020-01-01 13:55:28 +05:30
jest.spyOn(axios, 'get');
jest.spyOn(Visibility, 'hidden').mockReturnValue(false);
2018-11-08 19:23:39 +05:30
2020-01-01 13:55:28 +05:30
const dispatch = jest.fn().mockName('dispatch');
2018-11-08 19:23:39 +05:30
const rootGetters = {
lastCommit: { id: 'abc123def456ghi789jkl' },
currentProject: { path_with_namespace: 'abc/def' },
};
fetchLatestPipeline({ dispatch, rootGetters });
2020-01-01 13:55:28 +05:30
expect(dispatch).toHaveBeenCalledWith('requestLatestPipeline');
2018-11-08 19:23:39 +05:30
2020-01-01 13:55:28 +05:30
jest.advanceTimersByTime(1000);
2018-11-08 19:23:39 +05:30
2021-03-08 18:12:59 +05:30
new Promise((resolve) => requestAnimationFrame(resolve))
2018-11-08 19:23:39 +05:30
.then(() => {
expect(axios.get).toHaveBeenCalled();
2020-01-01 13:55:28 +05:30
expect(axios.get).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledWith(
2018-11-08 19:23:39 +05:30
'receiveLatestPipelineSuccess',
2020-01-01 13:55:28 +05:30
expect.anything(),
);
2018-11-08 19:23:39 +05:30
2020-01-01 13:55:28 +05:30
jest.advanceTimersByTime(10000);
2018-11-08 19:23:39 +05:30
})
2021-03-08 18:12:59 +05:30
.then(() => new Promise((resolve) => requestAnimationFrame(resolve)))
2018-11-08 19:23:39 +05:30
.then(() => {
expect(axios.get).toHaveBeenCalled();
2020-01-01 13:55:28 +05:30
expect(axios.get).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenCalledWith(
2018-11-08 19:23:39 +05:30
'receiveLatestPipelineSuccess',
2020-01-01 13:55:28 +05:30
expect.anything(),
);
2018-11-08 19:23:39 +05:30
})
.then(done)
.catch(done.fail);
});
});
describe('error', () => {
beforeEach(() => {
mock.onGet('/abc/def/commit/abc123def456ghi789jkl/pipelines').reply(500);
});
2021-03-08 18:12:59 +05:30
it('dispatches error', (done) => {
2020-01-01 13:55:28 +05:30
const dispatch = jest.fn().mockName('dispatch');
2018-11-08 19:23:39 +05:30
const rootGetters = {
lastCommit: { id: 'abc123def456ghi789jkl' },
currentProject: { path_with_namespace: 'abc/def' },
};
fetchLatestPipeline({ dispatch, rootGetters });
2020-01-01 13:55:28 +05:30
jest.advanceTimersByTime(1500);
2018-11-08 19:23:39 +05:30
2021-03-08 18:12:59 +05:30
new Promise((resolve) => requestAnimationFrame(resolve))
2018-11-08 19:23:39 +05:30
.then(() => {
2020-01-01 13:55:28 +05:30
expect(dispatch).toHaveBeenCalledWith('receiveLatestPipelineError', expect.anything());
2018-11-08 19:23:39 +05:30
})
.then(done)
.catch(done.fail);
});
});
});
describe('requestJobs', () => {
2021-03-08 18:12:59 +05:30
it('commits request', (done) => {
2018-11-08 19:23:39 +05:30
testAction(requestJobs, 1, mockedState, [{ type: types.REQUEST_JOBS, payload: 1 }], [], done);
});
});
describe('receiveJobsError', () => {
2021-03-08 18:12:59 +05:30
it('commits error', (done) => {
2018-11-08 19:23:39 +05:30
testAction(
receiveJobsError,
{ id: 1 },
mockedState,
[{ type: types.RECEIVE_JOBS_ERROR, payload: 1 }],
[
{
type: 'setErrorMessage',
payload: {
2020-03-13 15:44:24 +05:30
text: 'An error occurred while loading the pipelines jobs.',
2020-01-01 13:55:28 +05:30
action: expect.anything(),
2018-11-08 19:23:39 +05:30
actionText: 'Please try again',
actionPayload: { id: 1 },
},
},
],
done,
);
});
});
describe('receiveJobsSuccess', () => {
2021-03-08 18:12:59 +05:30
it('commits data', (done) => {
2018-11-08 19:23:39 +05:30
testAction(
receiveJobsSuccess,
{ id: 1, data: jobs },
mockedState,
[{ type: types.RECEIVE_JOBS_SUCCESS, payload: { id: 1, data: jobs } }],
[],
done,
);
});
});
describe('fetchJobs', () => {
2020-01-01 13:55:28 +05:30
const stage = { id: 1, dropdownPath: `${TEST_HOST}/jobs` };
2018-11-08 19:23:39 +05:30
describe('success', () => {
beforeEach(() => {
mock.onGet(stage.dropdownPath).replyOnce(200, jobs);
});
2021-03-08 18:12:59 +05:30
it('dispatches request', (done) => {
2018-11-08 19:23:39 +05:30
testAction(
fetchJobs,
stage,
mockedState,
[],
[
{ type: 'requestJobs', payload: stage.id },
{ type: 'receiveJobsSuccess', payload: { id: stage.id, data: jobs } },
],
done,
);
});
});
describe('error', () => {
beforeEach(() => {
mock.onGet(stage.dropdownPath).replyOnce(500);
});
2021-03-08 18:12:59 +05:30
it('dispatches error', (done) => {
2018-11-08 19:23:39 +05:30
testAction(
fetchJobs,
stage,
mockedState,
[],
[
{ type: 'requestJobs', payload: stage.id },
{ type: 'receiveJobsError', payload: stage },
],
done,
);
});
});
});
describe('toggleStageCollapsed', () => {
2021-03-08 18:12:59 +05:30
it('commits collapse', (done) => {
2018-11-08 19:23:39 +05:30
testAction(
toggleStageCollapsed,
1,
mockedState,
[{ type: types.TOGGLE_STAGE_COLLAPSE, payload: 1 }],
[],
done,
);
});
});
describe('setDetailJob', () => {
2021-03-08 18:12:59 +05:30
it('commits job', (done) => {
2018-11-08 19:23:39 +05:30
testAction(
setDetailJob,
'job',
mockedState,
[{ type: types.SET_DETAIL_JOB, payload: 'job' }],
2018-12-05 23:21:45 +05:30
[{ type: 'rightPane/open', payload: rightSidebarViews.jobsDetail }],
2018-11-08 19:23:39 +05:30
done,
);
});
2021-03-08 18:12:59 +05:30
it('dispatches rightPane/open as pipeline when job is null', (done) => {
2018-11-08 19:23:39 +05:30
testAction(
setDetailJob,
null,
mockedState,
2018-11-18 11:00:15 +05:30
[{ type: types.SET_DETAIL_JOB, payload: null }],
2018-12-05 23:21:45 +05:30
[{ type: 'rightPane/open', payload: rightSidebarViews.pipelines }],
2018-11-08 19:23:39 +05:30
done,
);
});
2021-03-08 18:12:59 +05:30
it('dispatches rightPane/open as job', (done) => {
2018-11-08 19:23:39 +05:30
testAction(
setDetailJob,
'job',
mockedState,
2018-11-18 11:00:15 +05:30
[{ type: types.SET_DETAIL_JOB, payload: 'job' }],
2018-12-05 23:21:45 +05:30
[{ type: 'rightPane/open', payload: rightSidebarViews.jobsDetail }],
2018-11-08 19:23:39 +05:30
done,
);
});
});
2020-11-24 15:15:51 +05:30
describe('requestJobLogs', () => {
2021-03-08 18:12:59 +05:30
it('commits request', (done) => {
2020-11-24 15:15:51 +05:30
testAction(requestJobLogs, null, mockedState, [{ type: types.REQUEST_JOB_LOGS }], [], done);
2018-11-08 19:23:39 +05:30
});
});
2020-11-24 15:15:51 +05:30
describe('receiveJobLogsError', () => {
2021-03-08 18:12:59 +05:30
it('commits error', (done) => {
2018-11-08 19:23:39 +05:30
testAction(
2020-11-24 15:15:51 +05:30
receiveJobLogsError,
2018-11-08 19:23:39 +05:30
null,
mockedState,
2020-11-24 15:15:51 +05:30
[{ type: types.RECEIVE_JOB_LOGS_ERROR }],
2018-11-08 19:23:39 +05:30
[
{
type: 'setErrorMessage',
payload: {
2020-11-24 15:15:51 +05:30
text: 'An error occurred while fetching the job logs.',
2020-01-01 13:55:28 +05:30
action: expect.any(Function),
2018-11-08 19:23:39 +05:30
actionText: 'Please try again',
actionPayload: null,
},
},
],
done,
);
});
});
2020-11-24 15:15:51 +05:30
describe('receiveJobLogsSuccess', () => {
2021-03-08 18:12:59 +05:30
it('commits data', (done) => {
2018-11-08 19:23:39 +05:30
testAction(
2020-11-24 15:15:51 +05:30
receiveJobLogsSuccess,
2018-11-08 19:23:39 +05:30
'data',
mockedState,
2020-11-24 15:15:51 +05:30
[{ type: types.RECEIVE_JOB_LOGS_SUCCESS, payload: 'data' }],
2018-11-08 19:23:39 +05:30
[],
done,
);
});
});
2020-11-24 15:15:51 +05:30
describe('fetchJobLogs', () => {
2018-11-08 19:23:39 +05:30
beforeEach(() => {
2020-01-01 13:55:28 +05:30
mockedState.detailJob = { path: `${TEST_HOST}/project/builds` };
2018-11-08 19:23:39 +05:30
});
describe('success', () => {
beforeEach(() => {
2020-01-01 13:55:28 +05:30
jest.spyOn(axios, 'get');
mock.onGet(`${TEST_HOST}/project/builds/trace`).replyOnce(200, { html: 'html' });
2018-11-08 19:23:39 +05:30
});
2021-03-08 18:12:59 +05:30
it('dispatches request', (done) => {
2018-11-08 19:23:39 +05:30
testAction(
2020-11-24 15:15:51 +05:30
fetchJobLogs,
2018-11-08 19:23:39 +05:30
null,
mockedState,
[],
[
2020-11-24 15:15:51 +05:30
{ type: 'requestJobLogs' },
{ type: 'receiveJobLogsSuccess', payload: { html: 'html' } },
2018-11-08 19:23:39 +05:30
],
done,
);
});
it('sends get request to correct URL', () => {
2020-11-24 15:15:51 +05:30
fetchJobLogs({
2020-01-01 13:55:28 +05:30
state: mockedState,
2018-11-08 19:23:39 +05:30
2020-01-01 13:55:28 +05:30
dispatch() {},
});
expect(axios.get).toHaveBeenCalledWith(`${TEST_HOST}/project/builds/trace`, {
2018-11-08 19:23:39 +05:30
params: { format: 'json' },
});
});
});
describe('error', () => {
beforeEach(() => {
2020-01-01 13:55:28 +05:30
mock.onGet(`${TEST_HOST}/project/builds/trace`).replyOnce(500);
2018-11-08 19:23:39 +05:30
});
2021-03-08 18:12:59 +05:30
it('dispatches error', (done) => {
2018-11-08 19:23:39 +05:30
testAction(
2020-11-24 15:15:51 +05:30
fetchJobLogs,
2018-11-08 19:23:39 +05:30
null,
mockedState,
[],
2020-11-24 15:15:51 +05:30
[{ type: 'requestJobLogs' }, { type: 'receiveJobLogsError' }],
2018-11-08 19:23:39 +05:30
done,
);
});
});
});
describe('resetLatestPipeline', () => {
2021-03-08 18:12:59 +05:30
it('commits reset mutations', (done) => {
2018-11-08 19:23:39 +05:30
testAction(
resetLatestPipeline,
null,
mockedState,
[
{ type: types.RECEIVE_LASTEST_PIPELINE_SUCCESS, payload: null },
{ type: types.SET_DETAIL_JOB, payload: null },
],
[],
done,
);
});
});
});