-
Notifications
You must be signed in to change notification settings - Fork 902
/
editPipelineJsonModal.controller.spec.ts
92 lines (75 loc) · 2.81 KB
/
editPipelineJsonModal.controller.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import {mock, IControllerService} from 'angular';
import {EDIT_PIPELINE_JSON_MODAL_CONTROLLER, EditPipelineJsonModalCtrl} from './editPipelineJsonModal.controller';
import {IPipeline} from 'core/domain';
describe('Controller: editPipelineJsonModal', () => {
let $ctrl: IControllerService,
controller: EditPipelineJsonModalCtrl,
$uibModalInstance: any;
beforeEach(mock.module(EDIT_PIPELINE_JSON_MODAL_CONTROLLER));
beforeEach(mock.inject(($controller: IControllerService) => $ctrl = $controller));
function initializeController(pipeline: IPipeline) {
$uibModalInstance = {close: () => {}};
controller = $ctrl(EditPipelineJsonModalCtrl, {$uibModalInstance, pipeline});
}
it('controller removes name, application, appConfig, all fields and hash keys', () => {
const pipeline: any = {
name: 'foo',
application: 'myApp',
appConfig: 'appConfig',
stage: {
foo: [{}],
bar: {},
baz: '',
bat: 4
}
};
initializeController(pipeline);
// sprinkle some hash keys into the pipeline
pipeline.stage.$$hashKey = '01D';
pipeline.stage.foo[0].$$hashKey = '01F';
pipeline.stage.bar.$$hashKey = '01G';
pipeline.plain = () => pipeline;
controller.$onInit();
const converted: any = JSON.parse(controller.command.pipelineJSON);
expect(converted.name).toBeUndefined();
expect(converted.application).toBeUndefined();
expect(converted.appConfig).toBe('appConfig');
expect(converted.stage.$$hashKey).toBeUndefined();
expect(converted.stage.foo[0].$$hashKey).toBeUndefined();
expect(converted.stage.bar.$$hashKey).toBeUndefined();
});
it('updatePipeline updates fields, removing name if added', () => {
const pipeline: any = {
application: 'myApp',
name: 'foo',
stage: {
foo: [
{}
],
bar: {},
baz: '',
bat: 4
}
};
initializeController(pipeline);
controller.$onInit();
spyOn($uibModalInstance, 'close');
const converted: any = JSON.parse(controller.command.pipelineJSON);
converted.application = 'someOtherApp';
converted.name = 'replacedName';
converted.bar = {updated: true};
controller.command.pipelineJSON = JSON.stringify(converted);
controller.updatePipeline();
expect(pipeline.application).toBe('myApp');
expect(pipeline.bar.updated).toBe(true);
expect($uibModalInstance.close).toHaveBeenCalled();
});
it('updatePipeline generates an error message when malformed JSON provided', () => {
const pipeline: any = {};
initializeController(pipeline);
controller.command = {pipelineJSON: 'This is not very good JSON', locked: false};
controller.updatePipeline();
expect(controller.command.invalid).toBe(true);
expect(controller.command.errorMessage).not.toBe(null);
});
});