Catalog alignment
[sdc.git] / catalog-ui / src / app / ng2 / components / forms / artifacts-form / artifact-form.component.spec.ts
1 import {ArtifactFormComponent} from "./artifact-form.component";
2 import {async, ComponentFixture} from "@angular/core/testing";
3 import {ConfigureFn, configureTests} from "../../../../../jest/test-config.helper";
4 import {NO_ERRORS_SCHEMA} from "@angular/core";
5 import {TranslateModule} from "../../../shared/translator/translate.module";
6 import {CacheService} from "../../../services/cache.service";
7 import {TranslateService} from "../../../shared/translator/translate.service";
8 import {ArtifactModel} from "../../../../models/artifacts";
9 import {IDropDownOption} from "onap-ui-angular/dist/form-elements/dropdown/dropdown-models";
10 import {Observable, Subject} from "rxjs";
11 import {getValue} from "@ngxs/store";
12
13
14 describe('artifact form component', () => {
15
16     let fixture: ComponentFixture<ArtifactFormComponent>;
17     let cacheServiceMock: Partial<CacheService>;
18     let onValidationChangeMock: Partial<Subject<boolean>>;
19
20     let artifactModel = new ArtifactModel();
21
22
23     beforeEach(
24         async(() => {
25
26             onValidationChangeMock = {
27                 next: jest.fn()
28             }
29
30             cacheServiceMock = {
31                 contains: jest.fn(),
32                 remove: jest.fn(),
33                 set: jest.fn(),
34                 get: jest.fn()
35             }
36
37             const configure: ConfigureFn = testBed => {
38                 testBed.configureTestingModule({
39                     declarations: [ArtifactFormComponent],
40                     imports: [TranslateModule],
41                     schemas: [NO_ERRORS_SCHEMA],
42                     providers: [
43                         {provide: CacheService, useValue: cacheServiceMock},
44                         {provide: TranslateService, useValue: {}}
45                     ],
46                 });
47             };
48
49             configureTests(configure).then(testBed => {
50                 fixture = testBed.createComponent(ArtifactFormComponent);
51             });
52         })
53     );
54
55
56     it('should verify initArtifactTypes for DEPLOYMENT and ArtifactType = HEAT_ENV', () =>{
57
58         cacheServiceMock.get.mockImplementation(() =>{
59             return {
60                 artifacts: {
61                     deployment:{
62                         resourceInstanceDeploymentArtifacts: [{name: "Dummy Value Returned from ui api"}],
63                     }
64                 }
65             }
66         });
67
68         fixture.componentInstance.artifactType = 'DEPLOYMENT';
69         fixture.componentInstance.artifact = artifactModel;
70         fixture.componentInstance.artifact.artifactType = 'HEAT_ENV';
71
72         fixture.componentInstance.initArtifactTypes();
73
74         expect(fixture.componentInstance.selectedFileType).toEqual(undefined);
75
76     });
77
78     it('should verify initArtifactTypes for DEPLOYMENT and ComponentType = RESOURCE', () =>{
79
80         cacheServiceMock.get.mockImplementation(() =>{
81             return {
82                 artifacts: {
83                     deployment:{
84                         resourceDeploymentArtifacts: [{name: "Dummy Value Returned from ui api"}],
85                     }
86                 }
87             }
88         });
89
90         fixture.componentInstance.artifactType = 'DEPLOYMENT';
91         fixture.componentInstance.artifact = artifactModel;
92         fixture.componentInstance.componentType = 'RESOURCE';
93
94         fixture.componentInstance.initArtifactTypes();
95
96         expect(fixture.componentInstance.selectedFileType).toEqual(undefined);
97
98     });
99
100     it('should verify initArtifactTypes for DEPLOYMENT and NOT ComponentType = RESOURCE OR NOT ArtifactType = HEAT_ENV', () =>{
101
102         cacheServiceMock.get.mockImplementation(() =>{
103             return {
104                 artifacts: {
105                     deployment:{
106                         serviceDeploymentArtifacts: [{name: "Dummy Value Returned from ui api"}],
107                     }
108                 }
109             }
110         });
111
112         fixture.componentInstance.artifactType = 'DEPLOYMENT';
113         fixture.componentInstance.artifact = artifactModel;
114         // fixture.componentInstance.componentType = 'RESOURCE';
115
116         fixture.componentInstance.initArtifactTypes();
117
118         expect(fixture.componentInstance.selectedFileType).toEqual(undefined);
119
120     });
121
122     it('should verify initArtifactTypes for INFORMATION', () =>{
123
124         cacheServiceMock.get.mockImplementation(() =>{
125             return {
126                 artifacts: {
127                     other: [{name: "Val1"}, {name: "ExpectedValToBeSelected"}, {name: "Val3"}]
128                 }
129             }
130         });
131
132         fixture.componentInstance.artifactType = 'INFORMATIONAL';
133         fixture.componentInstance.artifact = artifactModel;
134         fixture.componentInstance.artifact.artifactType = 'ExpectedValToBeSelected';
135
136         fixture.componentInstance.initArtifactTypes();
137
138         expect(fixture.componentInstance.selectedFileType).toEqual({"label": "ExpectedValToBeSelected", "value": "ExpectedValToBeSelected"});
139
140     });
141
142
143     it('should match current snapshot of artifact form component', () => {
144         expect(fixture).toMatchSnapshot();
145     });
146
147     it('should verify onUploadFile -> file gets file name', () => {
148         let file = {
149             filename:'dummyFileName'
150         }
151
152         fixture.componentInstance.verifyTypeAndFileWereFilled = jest.fn();
153         fixture.componentInstance.artifact = artifactModel;
154         fixture.componentInstance.onUploadFile(file);
155
156         expect(fixture.componentInstance.artifact.artifactName).toBe('dummyFileName');
157
158         const spy1 = jest.spyOn(fixture.componentInstance,'verifyTypeAndFileWereFilled');
159         expect(spy1).toHaveBeenCalled();
160     });
161
162     it('should verify onUploadFile -> file is null', () => {
163         let file = null;
164         fixture.componentInstance.artifact = artifactModel;
165         fixture.componentInstance.onUploadFile(file);
166
167         expect(fixture.componentInstance.artifact.artifactName).toBe(null);
168     });
169
170     it('should verify onTypeChange -> verifyTypeAndFileWereFilled is being called', () => {
171         let selectedFileType:IDropDownOption;
172         selectedFileType = {"label": "dummyLabel", "value": "dummyValue"};
173         fixture.componentInstance.verifyTypeAndFileWereFilled = jest.fn();
174
175         fixture.componentInstance.artifact = artifactModel;
176         fixture.componentInstance.onTypeChange(selectedFileType);
177
178         const spy1 = jest.spyOn(fixture.componentInstance,'verifyTypeAndFileWereFilled');
179         expect(spy1).toHaveBeenCalled();
180     });
181
182     it('should verify onDescriptionChange -> verifyTypeAndFileWereFilled is being called', () => {
183         fixture.componentInstance.verifyTypeAndFileWereFilled = jest.fn();
184         fixture.componentInstance.onValidationChange.next = jest.fn(() => true);
185
186         fixture.componentInstance.artifact = artifactModel;
187         fixture.componentInstance.onDescriptionChange();
188
189         const spy1 = jest.spyOn(fixture.componentInstance,'verifyTypeAndFileWereFilled');
190         expect(spy1).toHaveBeenCalled();
191     });
192
193     it('should verify onLabelChange -> verifyTypeAndFileWereFilled is being called', () => {
194         fixture.componentInstance.verifyTypeAndFileWereFilled = jest.fn();
195         fixture.componentInstance.onValidationChange.next = jest.fn(() => true);
196
197         fixture.componentInstance.artifact = artifactModel;
198         fixture.componentInstance.onLabelChange(true);
199
200         const spy1 = jest.spyOn(fixture.componentInstance,'verifyTypeAndFileWereFilled');
201         expect(spy1).toHaveBeenCalled();
202     });
203
204     it('should verify verifyTypeAndFileWereFilled -> verify branch this.artifact.artifactType !== \'DEPLOYMENT\' ==>> onValidationChange.next(false)', () => {
205         fixture.componentInstance.artifact = artifactModel;
206         fixture.componentInstance.artifact.artifactType = 'NOT_DEPLOYMENT';
207         fixture.componentInstance.artifact.mandatory = true;
208         fixture.componentInstance.descriptionIsValid = false;
209
210         let onValidationChangeResult;
211
212         fixture.componentInstance.onValidationChange.subscribe((data) => {
213             onValidationChangeResult = data;
214             // console.log("Subscriber got data >>>>> "+ data);
215         });
216
217         fixture.componentInstance.verifyTypeAndFileWereFilled();
218
219         expect(onValidationChangeResult).toBe(false);
220     });
221
222     it('should verify verifyTypeAndFileWereFilled -> verify branch this.artifact.artifactType !== \'DEPLOYMENT\' ==>> onValidationChange.next(true)', () => {
223         fixture.componentInstance.artifact = artifactModel;
224         fixture.componentInstance.artifact.artifactType = 'NOT_DEPLOYMENT';
225         fixture.componentInstance.artifact.mandatory = true;
226         fixture.componentInstance.artifact.artifactName = 'Something';
227         fixture.componentInstance.descriptionIsValid = true;
228
229         let onValidationChangeResult;
230
231         fixture.componentInstance.onValidationChange.subscribe((data) => {
232             onValidationChangeResult = data;
233             // console.log("Subscriber got data >>>>> "+ data);
234         });
235
236         fixture.componentInstance.verifyTypeAndFileWereFilled();
237
238         expect(onValidationChangeResult).toBe(true);
239     });
240
241
242 });