debian-mirror-gitlab/spec/frontend/jobs/store/actions_spec.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

499 lines
13 KiB
JavaScript
Raw Normal View History

2018-11-20 20:47:30 +05:30
import MockAdapter from 'axios-mock-adapter';
2021-03-08 18:12:59 +05:30
import { TEST_HOST } from 'helpers/test_constants';
2021-03-11 19:13:27 +05:30
import testAction from 'helpers/vuex_action_helper';
2018-11-20 20:47:30 +05:30
import {
setJobEndpoint,
2021-11-18 22:05:49 +05:30
setJobLogOptions,
2018-11-20 20:47:30 +05:30
clearEtagPoll,
stopPolling,
requestJob,
fetchJob,
receiveJobSuccess,
receiveJobError,
scrollTop,
scrollBottom,
2021-11-18 22:05:49 +05:30
requestJobLog,
fetchJobLog,
startPollingJobLog,
stopPollingJobLog,
receiveJobLogSuccess,
receiveJobLogError,
2019-12-04 20:38:33 +05:30
toggleCollapsibleLine,
2018-11-20 20:47:30 +05:30
requestJobsForStage,
fetchJobsForStage,
receiveJobsForStageSuccess,
receiveJobsForStageError,
2018-12-13 13:39:08 +05:30
hideSidebar,
showSidebar,
toggleSidebar,
2018-11-20 20:47:30 +05:30
} from '~/jobs/store/actions';
import * as types from '~/jobs/store/mutation_types';
2021-03-11 19:13:27 +05:30
import state from '~/jobs/store/state';
import axios from '~/lib/utils/axios_utils';
2018-11-20 20:47:30 +05:30
describe('Job State actions', () => {
let mockedState;
beforeEach(() => {
mockedState = state();
});
describe('setJobEndpoint', () => {
2022-06-21 17:19:12 +05:30
it('should commit SET_JOB_ENDPOINT mutation', () => {
return testAction(
2018-11-20 20:47:30 +05:30
setJobEndpoint,
'job/872324.json',
mockedState,
[{ type: types.SET_JOB_ENDPOINT, payload: 'job/872324.json' }],
[],
);
});
});
2021-11-18 22:05:49 +05:30
describe('setJobLogOptions', () => {
2022-06-21 17:19:12 +05:30
it('should commit SET_JOB_LOG_OPTIONS mutation', () => {
return testAction(
2021-11-18 22:05:49 +05:30
setJobLogOptions,
2018-12-13 13:39:08 +05:30
{ pagePath: 'job/872324/trace.json' },
2018-11-20 20:47:30 +05:30
mockedState,
2021-11-18 22:05:49 +05:30
[{ type: types.SET_JOB_LOG_OPTIONS, payload: { pagePath: 'job/872324/trace.json' } }],
2018-11-20 20:47:30 +05:30
[],
);
});
});
2018-12-13 13:39:08 +05:30
describe('hideSidebar', () => {
2022-06-21 17:19:12 +05:30
it('should commit HIDE_SIDEBAR mutation', () => {
return testAction(hideSidebar, null, mockedState, [{ type: types.HIDE_SIDEBAR }], []);
2018-11-20 20:47:30 +05:30
});
});
2018-12-13 13:39:08 +05:30
describe('showSidebar', () => {
2022-06-21 17:19:12 +05:30
it('should commit SHOW_SIDEBAR mutation', () => {
return testAction(showSidebar, null, mockedState, [{ type: types.SHOW_SIDEBAR }], []);
2018-12-13 13:39:08 +05:30
});
});
describe('toggleSidebar', () => {
describe('when isSidebarOpen is true', () => {
2022-06-21 17:19:12 +05:30
it('should dispatch hideSidebar', () => {
return testAction(toggleSidebar, null, mockedState, [], [{ type: 'hideSidebar' }]);
2018-12-13 13:39:08 +05:30
});
});
describe('when isSidebarOpen is false', () => {
2022-06-21 17:19:12 +05:30
it('should dispatch showSidebar', () => {
2018-12-13 13:39:08 +05:30
mockedState.isSidebarOpen = false;
2022-06-21 17:19:12 +05:30
return testAction(toggleSidebar, null, mockedState, [], [{ type: 'showSidebar' }]);
2018-12-13 13:39:08 +05:30
});
2018-11-20 20:47:30 +05:30
});
});
describe('requestJob', () => {
2022-06-21 17:19:12 +05:30
it('should commit REQUEST_JOB mutation', () => {
return testAction(requestJob, null, mockedState, [{ type: types.REQUEST_JOB }], []);
2018-11-20 20:47:30 +05:30
});
});
describe('fetchJob', () => {
let mock;
beforeEach(() => {
mockedState.jobEndpoint = `${TEST_HOST}/endpoint.json`;
mock = new MockAdapter(axios);
});
afterEach(() => {
mock.restore();
stopPolling();
clearEtagPoll();
});
describe('success', () => {
2022-06-21 17:19:12 +05:30
it('dispatches requestJob and receiveJobSuccess ', () => {
2018-11-20 20:47:30 +05:30
mock.onGet(`${TEST_HOST}/endpoint.json`).replyOnce(200, { id: 121212, name: 'karma' });
2022-06-21 17:19:12 +05:30
return testAction(
2018-11-20 20:47:30 +05:30
fetchJob,
null,
mockedState,
[],
[
{
type: 'requestJob',
},
{
payload: { id: 121212, name: 'karma' },
type: 'receiveJobSuccess',
},
],
);
});
});
describe('error', () => {
beforeEach(() => {
mock.onGet(`${TEST_HOST}/endpoint.json`).reply(500);
});
2022-06-21 17:19:12 +05:30
it('dispatches requestJob and receiveJobError ', () => {
return testAction(
2018-11-20 20:47:30 +05:30
fetchJob,
null,
mockedState,
[],
[
{
type: 'requestJob',
},
{
type: 'receiveJobError',
},
],
);
});
});
});
describe('receiveJobSuccess', () => {
2022-06-21 17:19:12 +05:30
it('should commit RECEIVE_JOB_SUCCESS mutation', () => {
return testAction(
2018-11-20 20:47:30 +05:30
receiveJobSuccess,
{ id: 121232132 },
mockedState,
[{ type: types.RECEIVE_JOB_SUCCESS, payload: { id: 121232132 } }],
[],
);
});
});
describe('receiveJobError', () => {
2022-06-21 17:19:12 +05:30
it('should commit RECEIVE_JOB_ERROR mutation', () => {
return testAction(
receiveJobError,
null,
mockedState,
[{ type: types.RECEIVE_JOB_ERROR }],
[],
);
2018-11-20 20:47:30 +05:30
});
});
describe('scrollTop', () => {
2022-06-21 17:19:12 +05:30
it('should dispatch toggleScrollButtons action', () => {
return testAction(scrollTop, null, mockedState, [], [{ type: 'toggleScrollButtons' }]);
2018-11-20 20:47:30 +05:30
});
});
describe('scrollBottom', () => {
2022-06-21 17:19:12 +05:30
it('should dispatch toggleScrollButtons action', () => {
return testAction(scrollBottom, null, mockedState, [], [{ type: 'toggleScrollButtons' }]);
2018-11-20 20:47:30 +05:30
});
});
2021-11-18 22:05:49 +05:30
describe('requestJobLog', () => {
2022-06-21 17:19:12 +05:30
it('should commit REQUEST_JOB_LOG mutation', () => {
return testAction(requestJobLog, null, mockedState, [{ type: types.REQUEST_JOB_LOG }], []);
2018-11-20 20:47:30 +05:30
});
});
2021-11-18 22:05:49 +05:30
describe('fetchJobLog', () => {
2018-11-20 20:47:30 +05:30
let mock;
beforeEach(() => {
2021-11-18 22:05:49 +05:30
mockedState.jobLogEndpoint = `${TEST_HOST}/endpoint`;
2018-11-20 20:47:30 +05:30
mock = new MockAdapter(axios);
});
afterEach(() => {
mock.restore();
stopPolling();
clearEtagPoll();
});
describe('success', () => {
2022-06-21 17:19:12 +05:30
it('dispatches requestJobLog, receiveJobLogSuccess and stopPollingJobLog when job is complete', () => {
2018-11-20 20:47:30 +05:30
mock.onGet(`${TEST_HOST}/endpoint/trace.json`).replyOnce(200, {
html: 'I, [2018-08-17T22:57:45.707325 #1841] INFO -- :',
complete: true,
});
2022-06-21 17:19:12 +05:30
return testAction(
2021-11-18 22:05:49 +05:30
fetchJobLog,
2018-11-20 20:47:30 +05:30
null,
mockedState,
[],
[
{
2018-12-13 13:39:08 +05:30
type: 'toggleScrollisInBottom',
payload: true,
2018-11-20 20:47:30 +05:30
},
{
payload: {
2018-12-05 23:21:45 +05:30
html: 'I, [2018-08-17T22:57:45.707325 #1841] INFO -- :',
complete: true,
2018-11-20 20:47:30 +05:30
},
2021-11-18 22:05:49 +05:30
type: 'receiveJobLogSuccess',
2018-11-20 20:47:30 +05:30
},
{
2021-11-18 22:05:49 +05:30
type: 'stopPollingJobLog',
2018-11-20 20:47:30 +05:30
},
],
);
});
2020-03-13 15:44:24 +05:30
describe('when job is incomplete', () => {
2021-11-18 22:05:49 +05:30
let jobLogPayload;
2020-03-13 15:44:24 +05:30
beforeEach(() => {
2021-11-18 22:05:49 +05:30
jobLogPayload = {
2020-03-13 15:44:24 +05:30
html: 'I, [2018-08-17T22:57:45.707325 #1841] INFO -- :',
complete: false,
};
2021-11-18 22:05:49 +05:30
mock.onGet(`${TEST_HOST}/endpoint/trace.json`).replyOnce(200, jobLogPayload);
2020-03-13 15:44:24 +05:30
});
2022-06-21 17:19:12 +05:30
it('dispatches startPollingJobLog', () => {
return testAction(
2021-11-18 22:05:49 +05:30
fetchJobLog,
2020-03-13 15:44:24 +05:30
null,
mockedState,
[],
[
{ type: 'toggleScrollisInBottom', payload: true },
2021-11-18 22:05:49 +05:30
{ type: 'receiveJobLogSuccess', payload: jobLogPayload },
{ type: 'startPollingJobLog' },
2020-03-13 15:44:24 +05:30
],
);
});
2022-06-21 17:19:12 +05:30
it('does not dispatch startPollingJobLog when timeout is non-empty', () => {
2021-11-18 22:05:49 +05:30
mockedState.jobLogTimeout = 1;
2020-03-13 15:44:24 +05:30
2022-06-21 17:19:12 +05:30
return testAction(
2021-11-18 22:05:49 +05:30
fetchJobLog,
2020-03-13 15:44:24 +05:30
null,
mockedState,
[],
[
{ type: 'toggleScrollisInBottom', payload: true },
2021-11-18 22:05:49 +05:30
{ type: 'receiveJobLogSuccess', payload: jobLogPayload },
2020-03-13 15:44:24 +05:30
],
);
});
});
2018-11-20 20:47:30 +05:30
});
describe('error', () => {
beforeEach(() => {
mock.onGet(`${TEST_HOST}/endpoint/trace.json`).reply(500);
});
2022-06-21 17:19:12 +05:30
it('dispatches requestJobLog and receiveJobLogError ', () => {
return testAction(
2021-11-18 22:05:49 +05:30
fetchJobLog,
2018-11-20 20:47:30 +05:30
null,
mockedState,
[],
[
{
2021-11-18 22:05:49 +05:30
type: 'receiveJobLogError',
2018-11-20 20:47:30 +05:30
},
],
);
});
});
});
2021-11-18 22:05:49 +05:30
describe('startPollingJobLog', () => {
2020-03-13 15:44:24 +05:30
let dispatch;
let commit;
beforeEach(() => {
2020-05-24 23:13:21 +05:30
dispatch = jest.fn();
commit = jest.fn();
2020-03-13 15:44:24 +05:30
2021-11-18 22:05:49 +05:30
startPollingJobLog({ dispatch, commit });
2020-03-13 15:44:24 +05:30
});
afterEach(() => {
2020-05-24 23:13:21 +05:30
jest.clearAllTimers();
2020-03-13 15:44:24 +05:30
});
2021-11-18 22:05:49 +05:30
it('should save the timeout id but not call fetchJobLog', () => {
expect(commit).toHaveBeenCalledWith(types.SET_JOB_LOG_TIMEOUT, expect.any(Number));
2020-05-24 23:13:21 +05:30
expect(commit.mock.calls[0][1]).toBeGreaterThan(0);
2021-11-18 22:05:49 +05:30
expect(dispatch).not.toHaveBeenCalledWith('fetchJobLog');
2020-03-13 15:44:24 +05:30
});
describe('after timeout has passed', () => {
beforeEach(() => {
2020-05-24 23:13:21 +05:30
jest.advanceTimersByTime(4000);
2020-03-13 15:44:24 +05:30
});
2021-11-18 22:05:49 +05:30
it('should clear the timeout id and fetchJobLog', () => {
expect(commit).toHaveBeenCalledWith(types.SET_JOB_LOG_TIMEOUT, 0);
expect(dispatch).toHaveBeenCalledWith('fetchJobLog');
2020-03-13 15:44:24 +05:30
});
});
});
2021-11-18 22:05:49 +05:30
describe('stopPollingJobLog', () => {
2020-03-13 15:44:24 +05:30
let origTimeout;
beforeEach(() => {
// Can't use spyOn(window, 'clearTimeout') because this caused unrelated specs to timeout
// https://gitlab.com/gitlab-org/gitlab/-/merge_requests/23838#note_280277727
origTimeout = window.clearTimeout;
2020-05-24 23:13:21 +05:30
window.clearTimeout = jest.fn();
2020-03-13 15:44:24 +05:30
});
afterEach(() => {
window.clearTimeout = origTimeout;
});
2022-06-21 17:19:12 +05:30
it('should commit STOP_POLLING_JOB_LOG mutation ', async () => {
2021-11-18 22:05:49 +05:30
const jobLogTimeout = 7;
2020-03-13 15:44:24 +05:30
2022-06-21 17:19:12 +05:30
await testAction(
2021-11-18 22:05:49 +05:30
stopPollingJobLog,
2018-11-20 20:47:30 +05:30
null,
2021-11-18 22:05:49 +05:30
{ ...mockedState, jobLogTimeout },
[{ type: types.SET_JOB_LOG_TIMEOUT, payload: 0 }, { type: types.STOP_POLLING_JOB_LOG }],
2018-11-20 20:47:30 +05:30
[],
2022-06-21 17:19:12 +05:30
);
expect(window.clearTimeout).toHaveBeenCalledWith(jobLogTimeout);
2018-11-20 20:47:30 +05:30
});
});
2021-11-18 22:05:49 +05:30
describe('receiveJobLogSuccess', () => {
2022-06-21 17:19:12 +05:30
it('should commit RECEIVE_JOB_LOG_SUCCESS mutation ', () => {
return testAction(
2021-11-18 22:05:49 +05:30
receiveJobLogSuccess,
2018-11-20 20:47:30 +05:30
'hello world',
mockedState,
2021-11-18 22:05:49 +05:30
[{ type: types.RECEIVE_JOB_LOG_SUCCESS, payload: 'hello world' }],
2018-11-20 20:47:30 +05:30
[],
);
});
});
2021-11-18 22:05:49 +05:30
describe('receiveJobLogError', () => {
2022-06-21 17:19:12 +05:30
it('should commit stop polling job log', () => {
return testAction(receiveJobLogError, null, mockedState, [], [{ type: 'stopPollingJobLog' }]);
2018-11-20 20:47:30 +05:30
});
});
2019-12-04 20:38:33 +05:30
describe('toggleCollapsibleLine', () => {
2022-06-21 17:19:12 +05:30
it('should commit TOGGLE_COLLAPSIBLE_LINE mutation ', () => {
return testAction(
2019-12-04 20:38:33 +05:30
toggleCollapsibleLine,
{ isClosed: true },
mockedState,
[{ type: types.TOGGLE_COLLAPSIBLE_LINE, payload: { isClosed: true } }],
[],
);
});
});
2018-11-20 20:47:30 +05:30
describe('requestJobsForStage', () => {
2022-06-21 17:19:12 +05:30
it('should commit REQUEST_JOBS_FOR_STAGE mutation ', () => {
return testAction(
2018-11-20 20:47:30 +05:30
requestJobsForStage,
2018-12-05 23:21:45 +05:30
{ name: 'deploy' },
2018-11-20 20:47:30 +05:30
mockedState,
2018-12-05 23:21:45 +05:30
[{ type: types.REQUEST_JOBS_FOR_STAGE, payload: { name: 'deploy' } }],
2018-11-20 20:47:30 +05:30
[],
);
});
});
describe('fetchJobsForStage', () => {
let mock;
beforeEach(() => {
mock = new MockAdapter(axios);
});
afterEach(() => {
mock.restore();
});
describe('success', () => {
2022-06-21 17:19:12 +05:30
it('dispatches requestJobsForStage and receiveJobsForStageSuccess ', () => {
2018-12-05 23:21:45 +05:30
mock
.onGet(`${TEST_HOST}/jobs.json`)
.replyOnce(200, { latest_statuses: [{ id: 121212, name: 'build' }], retried: [] });
2018-11-20 20:47:30 +05:30
2022-06-21 17:19:12 +05:30
return testAction(
2018-11-20 20:47:30 +05:30
fetchJobsForStage,
2018-12-05 23:21:45 +05:30
{ dropdown_path: `${TEST_HOST}/jobs.json` },
2018-11-20 20:47:30 +05:30
mockedState,
[],
[
{
type: 'requestJobsForStage',
2018-12-05 23:21:45 +05:30
payload: { dropdown_path: `${TEST_HOST}/jobs.json` },
2018-11-20 20:47:30 +05:30
},
{
payload: [{ id: 121212, name: 'build' }],
type: 'receiveJobsForStageSuccess',
},
],
);
});
});
describe('error', () => {
beforeEach(() => {
2018-12-05 23:21:45 +05:30
mock.onGet(`${TEST_HOST}/jobs.json`).reply(500);
2018-11-20 20:47:30 +05:30
});
2022-06-21 17:19:12 +05:30
it('dispatches requestJobsForStage and receiveJobsForStageError', () => {
return testAction(
2018-11-20 20:47:30 +05:30
fetchJobsForStage,
2018-12-05 23:21:45 +05:30
{ dropdown_path: `${TEST_HOST}/jobs.json` },
2018-11-20 20:47:30 +05:30
mockedState,
[],
[
{
type: 'requestJobsForStage',
2018-12-05 23:21:45 +05:30
payload: { dropdown_path: `${TEST_HOST}/jobs.json` },
2018-11-20 20:47:30 +05:30
},
{
type: 'receiveJobsForStageError',
},
],
);
});
});
});
describe('receiveJobsForStageSuccess', () => {
2022-06-21 17:19:12 +05:30
it('should commit RECEIVE_JOBS_FOR_STAGE_SUCCESS mutation ', () => {
return testAction(
2018-11-20 20:47:30 +05:30
receiveJobsForStageSuccess,
[{ id: 121212, name: 'karma' }],
mockedState,
[{ type: types.RECEIVE_JOBS_FOR_STAGE_SUCCESS, payload: [{ id: 121212, name: 'karma' }] }],
[],
);
});
});
describe('receiveJobsForStageError', () => {
2022-06-21 17:19:12 +05:30
it('should commit RECEIVE_JOBS_FOR_STAGE_ERROR mutation ', () => {
return testAction(
2018-11-20 20:47:30 +05:30
receiveJobsForStageError,
null,
mockedState,
[{ type: types.RECEIVE_JOBS_FOR_STAGE_ERROR }],
[],
);
});
});
});