describe('pipelineConfigService', () => {
let service: PipelineConfigService,
$http: ng.IHttpBackendService,
$scope: ng.IScope,
API: Api;
const buildStage = (base: any): IStage => {
const stageDefaults: IStage = {
name: 'a',
type: 'wait',
refId: null,
isNew: false,
requisiteStageRefIds: []
};
Object.assign(stageDefaults, base);
return stageDefaults;
};
const buildPipeline = (base: any): IPipeline => {
const defaults: IPipeline = {
id: null,
index: 1,
name: 'some pipeline',
application: 'app',
lastModifiedBy: null,
limitConcurrent: true,
keepWaitingPipelines: false,
strategy: false,
triggers: [],
stages: [],
parameterConfig: null,
};
if (base.stages && base.stages.length) {
base.stages = base.stages.map((s: any) => buildStage(s));
}
Object.assign(defaults, base);
return defaults;
};
beforeEach(
mock.module(
PIPELINE_CONFIG_SERVICE,
API_SERVICE
)
);
beforeEach(mock.inject((pipelineConfigService: PipelineConfigService, $httpBackend: ng.IHttpBackendService, $rootScope: ng.IRootScopeService, _API_: Api) => {
service = pipelineConfigService;
$http = $httpBackend;
$scope = $rootScope.$new();
API = _API_;
}));
describe('savePipeline', () => {
it('clears isNew flags, stage name if not present', () => {
const pipeline: IPipeline = buildPipeline({
stages: [
{ name: 'explicit name', type: 'bake', isNew: true },
{ name: null, type: 'bake', isNew: true },
{ name: '', type: 'bake', isNew: true }
]
});
$http.expectPOST(API.baseUrl + '/pipelines').respond(200, '');
service.savePipeline(pipeline);
$scope.$digest();
expect(pipeline.stages[0].name).toBe('explicit name');
expect(pipeline.stages[1].name).toBeUndefined();
expect(pipeline.stages[2].name).toBeUndefined();
expect(pipeline.stages[0].isNew).toBeUndefined();
expect(pipeline.stages[1].isNew).toBeUndefined();
expect(pipeline.stages[2].isNew).toBeUndefined();
});
});
describe('getPipelines', () => {
it('should return pipelines sorted by index', () => {
let result: IPipeline[] = null;
const fromServer: IPipeline[] = [
buildPipeline({ id: 'a', name: 'second', application: 'app', index: 1, stages: [], triggers: [] }),
buildPipeline({ id: 'b', name: 'last', application: 'app', index: 3, stages: [], triggers: [] }),
buildPipeline({ id: 'c', name: 'first', application: 'app', index: 0, stages: [] }),
buildPipeline({ id: 'd', name: 'third', application: 'app', index: 2, stages: [] }),
];
$http.expectGET(API.baseUrl + '/applications/app/pipelineConfigs').respond(200, fromServer);
service.getPipelinesForApplication('app').then((pipelines: IPipeline[]) => {
result = pipelines;
});
$scope.$digest();
$http.flush();
//.........这里部分代码省略.........
请发表评论