BigW Consortium Gitlab

fixed pipeline actions spec

parent a83dd664
...@@ -8,6 +8,7 @@ import * as types from './mutation_types'; ...@@ -8,6 +8,7 @@ import * as types from './mutation_types';
let eTagPoll; let eTagPoll;
export const clearEtagPoll = () => (eTagPoll = null);
export const stopPipelinePolling = () => eTagPoll.stop(); export const stopPipelinePolling = () => eTagPoll.stop();
export const restartPipelinePolling = () => eTagPoll.restart(); export const restartPipelinePolling = () => eTagPoll.restart();
......
import Visibility from 'visibilityjs'; import { refreshLastCommitData } from '~/ide/stores/actions';
import MockAdapter from 'axios-mock-adapter';
import { refreshLastCommitData, pollSuccessCallBack } from '~/ide/stores/actions';
import store from '~/ide/stores'; import store from '~/ide/stores';
import service from '~/ide/services'; import service from '~/ide/services';
import axios from '~/lib/utils/axios_utils';
import { fullPipelinesResponse } from '../../mock_data';
import { resetStore } from '../../helpers'; import { resetStore } from '../../helpers';
import testAction from '../../../helpers/vuex_action_helper'; import testAction from '../../../helpers/vuex_action_helper';
describe('IDE store project actions', () => { describe('IDE store project actions', () => {
const setProjectState = () => {
store.state.currentProjectId = 'abc/def';
store.state.currentBranchId = 'master';
store.state.projects['abc/def'] = {
id: 4,
path_with_namespace: 'abc/def',
branches: {
master: {
commit: {
id: 'abc123def456ghi789jkl',
title: 'example',
},
},
},
};
};
beforeEach(() => { beforeEach(() => {
store.state.projects['abc/def'] = {}; store.state.projects['abc/def'] = {};
}); });
...@@ -101,92 +80,4 @@ describe('IDE store project actions', () => { ...@@ -101,92 +80,4 @@ describe('IDE store project actions', () => {
); );
}); });
}); });
describe('pipelinePoll', () => {
let mock;
beforeEach(() => {
setProjectState();
jasmine.clock().install();
mock = new MockAdapter(axios);
mock
.onGet('/abc/def/commit/abc123def456ghi789jkl/pipelines')
.reply(200, { data: { foo: 'bar' } }, { 'poll-interval': '10000' });
});
afterEach(() => {
jasmine.clock().uninstall();
mock.restore();
store.dispatch('stopPipelinePolling');
});
it('calls service periodically', done => {
spyOn(axios, 'get').and.callThrough();
spyOn(Visibility, 'hidden').and.returnValue(false);
store
.dispatch('pipelinePoll')
.then(() => {
jasmine.clock().tick(1000);
expect(axios.get).toHaveBeenCalled();
expect(axios.get.calls.count()).toBe(1);
})
.then(() => new Promise(resolve => requestAnimationFrame(resolve)))
.then(() => {
jasmine.clock().tick(10000);
expect(axios.get.calls.count()).toBe(2);
})
.then(() => new Promise(resolve => requestAnimationFrame(resolve)))
.then(() => {
jasmine.clock().tick(10000);
expect(axios.get.calls.count()).toBe(3);
})
.then(() => new Promise(resolve => requestAnimationFrame(resolve)))
.then(() => {
jasmine.clock().tick(10000);
expect(axios.get.calls.count()).toBe(4);
})
.then(done)
.catch(done.fail);
});
});
describe('pollSuccessCallBack', () => {
beforeEach(() => {
setProjectState();
});
it('commits correct pipeline', done => {
testAction(
pollSuccessCallBack,
fullPipelinesResponse,
store.state,
[
{
type: 'SET_LAST_COMMIT_PIPELINE',
payload: {
projectId: 'abc/def',
branchId: 'master',
pipeline: {
id: '50',
commit: {
id: 'abc123def456ghi789jkl',
},
details: {
status: {
icon: 'status_passed',
text: 'passed',
},
},
},
},
},
], // mutations
[], // action
done,
);
});
});
}); });
import Visibility from 'visibilityjs';
import MockAdapter from 'axios-mock-adapter'; import MockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils'; import axios from '~/lib/utils/axios_utils';
import actions, { import actions, {
...@@ -5,15 +6,13 @@ import actions, { ...@@ -5,15 +6,13 @@ import actions, {
receiveLatestPipelineError, receiveLatestPipelineError,
receiveLatestPipelineSuccess, receiveLatestPipelineSuccess,
fetchLatestPipeline, fetchLatestPipeline,
requestStages, stopPipelinePolling,
receiveStagesError, clearEtagPoll,
receiveStagesSuccess,
fetchStages,
} from '~/ide/stores/modules/pipelines/actions'; } from '~/ide/stores/modules/pipelines/actions';
import state from '~/ide/stores/modules/pipelines/state'; import state from '~/ide/stores/modules/pipelines/state';
import * as types from '~/ide/stores/modules/pipelines/mutation_types'; import * as types from '~/ide/stores/modules/pipelines/mutation_types';
import testAction from '../../../../helpers/vuex_action_helper'; import testAction from '../../../../helpers/vuex_action_helper';
import { pipelines, stages } from '../../../mock_data'; import { pipelines } from '../../../mock_data';
describe('IDE pipelines actions', () => { describe('IDE pipelines actions', () => {
let mockedState; let mockedState;
...@@ -51,7 +50,7 @@ describe('IDE pipelines actions', () => { ...@@ -51,7 +50,7 @@ describe('IDE pipelines actions', () => {
null, null,
mockedState, mockedState,
[{ type: types.RECEIVE_LASTEST_PIPELINE_ERROR }], [{ type: types.RECEIVE_LASTEST_PIPELINE_ERROR }],
[], [{ type: 'stopPipelinePolling' }],
done, done,
); );
}); });
...@@ -59,7 +58,7 @@ describe('IDE pipelines actions', () => { ...@@ -59,7 +58,7 @@ describe('IDE pipelines actions', () => {
it('creates flash message', () => { it('creates flash message', () => {
const flashSpy = spyOnDependency(actions, 'flash'); const flashSpy = spyOnDependency(actions, 'flash');
receiveLatestPipelineError({ commit() {} }); receiveLatestPipelineError({ commit() {}, dispatch() {} });
expect(flashSpy).toHaveBeenCalled(); expect(flashSpy).toHaveBeenCalled();
}); });
...@@ -79,166 +78,88 @@ describe('IDE pipelines actions', () => { ...@@ -79,166 +78,88 @@ describe('IDE pipelines actions', () => {
}); });
describe('fetchLatestPipeline', () => { describe('fetchLatestPipeline', () => {
describe('success', () => {
beforeEach(() => { beforeEach(() => {
mock.onGet(/\/api\/v4\/projects\/(.*)\/pipelines(.*)/).replyOnce(200, pipelines); jasmine.clock().install();
});
it('dispatches request', done => {
testAction(
fetchLatestPipeline,
'123',
mockedState,
[],
[{ type: 'requestLatestPipeline' }, { type: 'receiveLatestPipelineSuccess' }],
done,
);
});
it('dispatches success with latest pipeline', done => {
testAction(
fetchLatestPipeline,
'123',
mockedState,
[],
[
{ type: 'requestLatestPipeline' },
{ type: 'receiveLatestPipelineSuccess', payload: pipelines[0] },
],
done,
);
}); });
it('calls axios with correct params', () => { afterEach(() => {
const apiSpy = spyOn(axios, 'get').and.callThrough(); jasmine.clock().uninstall();
stopPipelinePolling();
fetchLatestPipeline({ dispatch() {}, rootState: state }, '123'); clearEtagPoll();
expect(apiSpy).toHaveBeenCalledWith(jasmine.anything(), {
params: {
sha: '123',
per_page: '1',
},
});
});
}); });
describe('error', () => { describe('success', () => {
beforeEach(() => { beforeEach(() => {
mock.onGet(/\/api\/v4\/projects\/(.*)\/pipelines(.*)/).replyOnce(500); mock
.onGet('/abc/def/commit/abc123def456ghi789jkl/pipelines')
.reply(200, { data: { foo: 'bar' } }, { 'poll-interval': '10000' });
}); });
it('dispatches error', done => { it('dispatches request', done => {
testAction( spyOn(axios, 'get').and.callThrough();
fetchLatestPipeline, spyOn(Visibility, 'hidden').and.returnValue(false);
'123',
mockedState,
[],
[{ type: 'requestLatestPipeline' }, { type: 'receiveLatestPipelineError' }],
done,
);
});
});
});
describe('requestStages', () => {
it('commits request', done => {
testAction(requestStages, null, mockedState, [{ type: types.REQUEST_STAGES }], [], done);
});
});
describe('receiveJobsError', () => {
it('commits error', done => {
testAction(
receiveStagesError,
null,
mockedState,
[{ type: types.RECEIVE_STAGES_ERROR }],
[],
done,
);
});
it('creates flash message', () => {
const flashSpy = spyOnDependency(actions, 'flash');
receiveStagesError({ commit() {} }); const dispatch = jasmine.createSpy('dispatch');
const rootGetters = {
lastCommit: { id: 'abc123def456ghi789jkl' },
currentProject: { path_with_namespace: 'abc/def' },
};
expect(flashSpy).toHaveBeenCalled(); fetchLatestPipeline({ dispatch, rootGetters });
});
});
describe('receiveStagesSuccess', () => { expect(dispatch.calls.argsFor(0)).toEqual(['requestLatestPipeline']);
it('commits jobs', done => {
testAction(
receiveStagesSuccess,
stages,
mockedState,
[{ type: types.RECEIVE_STAGES_SUCCESS, payload: stages }],
[],
done,
);
});
});
describe('fetchStages', () => {
beforeEach(() => {
mockedState.latestPipeline = pipelines[0];
});
describe('success', () => {
beforeEach(() => {
mock.onGet(/\/(.*)\/pipelines\/(.*)\/builds.json/).replyOnce(200, stages);
});
it('dispatches request', done => { jasmine.clock().tick(1000);
testAction(
fetchStages,
null,
mockedState,
[],
[{ type: 'requestStages' }, { type: 'receiveStagesSuccess' }],
done,
);
});
it('dispatches success with latest pipeline', done => { new Promise(resolve => requestAnimationFrame(resolve))
testAction( .then(() => {
fetchStages, expect(axios.get).toHaveBeenCalled();
null, expect(axios.get.calls.count()).toBe(1);
mockedState,
[],
[{ type: 'requestStages' }, { type: 'receiveStagesSuccess', payload: stages }],
done,
);
});
it('calls axios with correct URL', () => { expect(dispatch.calls.argsFor(1)).toEqual([
const apiSpy = spyOn(axios, 'get').and.callThrough(); 'receiveLatestPipelineSuccess',
jasmine.anything(),
]);
fetchStages({ dispatch() {}, state: mockedState, rootState: mockedState }); jasmine.clock().tick(10000);
})
.then(() => new Promise(resolve => requestAnimationFrame(resolve)))
.then(() => {
expect(axios.get).toHaveBeenCalled();
expect(axios.get.calls.count()).toBe(2);
expect(apiSpy).toHaveBeenCalledWith( expect(dispatch.calls.argsFor(2)).toEqual([
'/test/project/pipelines/1/builds.json', 'receiveLatestPipelineSuccess',
jasmine.anything(), jasmine.anything(),
); ]);
})
.then(done)
.catch(done.fail);
}); });
}); });
describe('error', () => { describe('error', () => {
beforeEach(() => { beforeEach(() => {
mock.onGet(/\/(.*)\/pipelines\/(.*)\/builds.json/).replyOnce(500); mock.onGet('/abc/def/commit/abc123def456ghi789jkl/pipelines').reply(500);
}); });
it('dispatches error', done => { it('dispatches error', done => {
testAction( const dispatch = jasmine.createSpy('dispatch');
fetchStages, const rootGetters = {
null, lastCommit: { id: 'abc123def456ghi789jkl' },
mockedState, currentProject: { path_with_namespace: 'abc/def' },
[], };
[{ type: 'requestStages' }, { type: 'receiveStagesError' }],
done, fetchLatestPipeline({ dispatch, rootGetters });
);
jasmine.clock().tick(1500);
new Promise(resolve => requestAnimationFrame(resolve))
.then(() => {
expect(dispatch.calls.argsFor(1)).toEqual(['receiveLatestPipelineError']);
})
.then(done)
.catch(done.fail);
}); });
}); });
}); });
......
...@@ -37,40 +37,4 @@ describe('Multi-file store branch mutations', () => { ...@@ -37,40 +37,4 @@ describe('Multi-file store branch mutations', () => {
expect(localState.projects.Example.branches.master.commit.title).toBe('Example commit'); expect(localState.projects.Example.branches.master.commit.title).toBe('Example commit');
}); });
}); });
describe('SET_LAST_COMMIT_PIPELINE', () => {
it('sets the pipeline for the last commit on current project', () => {
localState.projects = {
Example: {
branches: {
master: {
commit: {},
},
},
},
};
mutations.SET_LAST_COMMIT_PIPELINE(localState, {
projectId: 'Example',
branchId: 'master',
pipeline: {
id: '50',
details: {
status: {
icon: 'status_passed',
text: 'passed',
},
},
},
});
expect(localState.projects.Example.branches.master.commit.pipeline.id).toBe('50');
expect(localState.projects.Example.branches.master.commit.pipeline.details.status.text).toBe(
'passed',
);
expect(localState.projects.Example.branches.master.commit.pipeline.details.status.icon).toBe(
'status_passed',
);
});
});
}); });
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment