Catalog alignment
[sdc.git] / catalog-ui / src / app / ng2 / pages / composition / graph / service-path-creator / link-row / link-row.component.spec.ts
1 import {async, ComponentFixture} from "@angular/core/testing";
2 import {CacheService} from "../../../../../services/cache.service";
3 import {ConfigureFn, configureTests} from "../../../../../../../jest/test-config.helper";
4 import {NO_ERRORS_SCHEMA} from "@angular/core";
5 import {LinkRowComponent} from "./link-row.component";
6 import {DropdownValue} from "../../../../../components/ui/form-components/dropdown/ui-element-dropdown.component";
7 import {MapItemData, ServicePathMapItem} from "../../../../../../models/graph/nodes-and-links-map";
8
9 describe('artifact form component', () => {
10
11     let fixture: ComponentFixture<LinkRowComponent>;
12     let cacheServiceMock: Partial<CacheService>;
13
14     beforeEach(
15         async(() => {
16
17
18             cacheServiceMock = {
19                 contains: jest.fn(),
20                 remove: jest.fn(),
21                 set: jest.fn(),
22                 get: jest.fn()
23             }
24
25             const configure: ConfigureFn = testBed => {
26                 testBed.configureTestingModule({
27                     declarations: [LinkRowComponent],
28                     imports: [],
29                     schemas: [NO_ERRORS_SCHEMA],
30                     providers: []
31                     ,
32                 });
33             };
34
35             configureTests(configure).then(testBed => {
36                 fixture = testBed.createComponent(LinkRowComponent);
37             });
38         })
39     );
40
41
42     it('should match current snapshot of artifact form component', () => {
43         expect(fixture).toMatchSnapshot();
44     });
45
46
47     it('ngOnChanges() -> in case data exist -> call to parseInitialData()' ,() => {
48         // init values / mock functions
49         let data = 'something';
50         fixture.componentInstance.parseInitialData = jest.fn();
51         fixture.componentInstance.data = data;
52
53         // call to the tested function
54         fixture.componentInstance.ngOnChanges();
55
56         // expect that
57         expect(fixture.componentInstance.parseInitialData).toHaveBeenCalledWith(data);
58     });
59
60     it('onSourceSelected() -> in case id -> srcCP, link.fromCP, link.toNode, link.toCP, target, targetCP should be updated accordingly' ,() => {
61         // init values / mock functions
62         let id = 'id';
63         let data = 'data';
64         let link = {
65             fromCP:'testVal',
66             toNode:'testVal',
67             toCP:'testVal'
68         }
69         let target = ['val1', 'val2'];
70         let targetCP = ['val1', 'val2'];
71
72         fixture.componentInstance.findOptions = jest.fn();
73         fixture.componentInstance.convertValuesToDropDownOptions = jest.fn(() => 'dummyConvertedVal');
74         fixture.componentInstance.data = data;
75         fixture.componentInstance.link = link;
76         fixture.componentInstance.target = target;
77         fixture.componentInstance.targetCP = targetCP;
78
79         // call to the tested function
80         fixture.componentInstance.onSourceSelected(id);
81
82         // expect that
83         expect(fixture.componentInstance.findOptions).toHaveBeenCalledWith(data, id);
84         expect(fixture.componentInstance.srcCP).toBe('dummyConvertedVal');
85         expect(fixture.componentInstance.link.fromCP).toBe('');
86         expect(fixture.componentInstance.link.toNode).toBe('');
87         expect(fixture.componentInstance.link.toCP).toBe('');
88         expect(fixture.componentInstance.target.length).toBe(0);
89         expect(fixture.componentInstance.targetCP.length).toBe(0);
90     });
91
92     it('onSourceSelected() -> in case id undefined -> No Change to srcCP, link.fromCP, link.toNode, link.toCP, target, targetCP' ,() => {
93         // init values / mock functions
94         let id;
95         let data = 'data';
96         let link = {
97             fromCP:'testVal',
98             toNode:'testVal',
99             toCP:'testVal'
100         }
101         let target = ['val1', 'val2'];
102         let targetCP = ['val1', 'val2'];
103
104         fixture.componentInstance.findOptions = jest.fn();
105         fixture.componentInstance.convertValuesToDropDownOptions = jest.fn(() => 'dummyConvertedVal');
106         fixture.componentInstance.data = data;
107         fixture.componentInstance.link = link;
108         fixture.componentInstance.target = target;
109         fixture.componentInstance.targetCP = targetCP;
110
111         // call to the tested function
112         fixture.componentInstance.onSourceSelected(id);
113
114         // expect that
115         expect(fixture.componentInstance.link.fromCP).toBe(link.fromCP);
116         expect(fixture.componentInstance.link.toNode).toBe(link.toNode);
117         expect(fixture.componentInstance.link.toCP).toBe(link.toCP);
118         expect(fixture.componentInstance.target.length).toBe(2);
119         expect(fixture.componentInstance.target[0]).toBe('val1')
120         expect(fixture.componentInstance.targetCP.length).toBe(2);
121         expect(fixture.componentInstance.targetCP[1]).toBe('val2');
122     });
123
124     it('onSrcCPSelected() -> in case id  -> Verify target, link.fromCPOriginId, link.toNode, link.toCP, targetCP.length' ,() => {
125         // init values / mock functions
126         let id = 'id';
127         let link = {
128             fromNode:'testVal',
129             toCPOriginId: 'initValue_ShouldBeChanged'
130         };
131         let option1 = {
132             id: 'something'
133         };
134         let option2 = {
135             id: 'id',
136             data: {"ownerId":1}
137         };
138
139         fixture.componentInstance.link = link;
140         fixture.componentInstance.findOptions = jest.fn(() => [option1, option2]);
141         fixture.componentInstance.convertValuesToDropDownOptions = jest.fn(() => 'dummyConvertedVal');
142
143         // call to the tested function
144         fixture.componentInstance.onSrcCPSelected(id);
145
146         // expect that
147         expect(fixture.componentInstance.target).toBe('dummyConvertedVal');
148         expect(fixture.componentInstance.link.fromCPOriginId).toBe(option2.data.ownerId);
149         expect(fixture.componentInstance.link.toNode).toBe('');
150         expect(fixture.componentInstance.link.toCP).toBe('');
151         expect(fixture.componentInstance.targetCP.length).toBe(0);
152
153     });
154
155     it('onSrcCPSelected() -> in case id undefined -> Verify target, link.fromCPOriginId, link.toNode, link.toCP, targetCP.length' ,() => {
156         // init values / mock functions
157         let id;
158
159         let targetInput:Array<DropdownValue> = [{value:'Value', label:'Label', hidden:true, selected:true}];
160
161         let linkInput = {
162             fromCPOriginId:'expectedLinkFromCPOriginId',
163             toNode:'expectedLinkToNode',
164             toCP:'expectedLinkToCP',
165             // Link Object
166             canEdit:true,
167             canRemove:true,
168             isFirst:true,
169             // ForwardingPathLink Object
170             ownerId:'',
171             fromNode:'',
172             fromCP:'',
173             toCPOriginId:''
174         }
175
176         fixture.componentInstance.target = targetInput;
177         fixture.componentInstance.link = linkInput;
178         fixture.componentInstance.targetCP = targetInput;
179
180
181         // call to the tested function
182         fixture.componentInstance.onSrcCPSelected(id);
183
184         // expect that
185         expect(fixture.componentInstance.target).toBe(targetInput);
186         expect(fixture.componentInstance.link.fromCPOriginId).toBe('expectedLinkFromCPOriginId');
187         expect(fixture.componentInstance.link.toNode).toBe('expectedLinkToNode');
188         expect(fixture.componentInstance.link.toCP).toBe('expectedLinkToCP');
189         expect(fixture.componentInstance.targetCP.length).toBe(1);
190     });
191
192     it('onTargetSelected() -> in case id  -> Verify targetCP & link.toCP' ,() => {
193         // init values / mock functions
194         let id = 'id';
195         let link = {
196             toCP:'testVal'
197         }
198         let targetCP = ['val1', 'val2'];
199
200         fixture.componentInstance.findOptions = jest.fn();
201         fixture.componentInstance.convertValuesToDropDownOptions = jest.fn(() => 'dummyConvertedVal');
202         fixture.componentInstance.link = link;
203         fixture.componentInstance.targetCP = targetCP;
204
205         // call to the tested function
206         fixture.componentInstance.onTargetSelected(id);
207
208         // expect that
209         expect(fixture.componentInstance.targetCP).toBe('dummyConvertedVal');
210         expect(fixture.componentInstance.link.toCP).toBe('');
211
212     });
213
214     it('onTargetSelected() -> in case id undefined -> Verify targetCP & link.toCP' ,() => {
215         // init values / mock functions
216         let id;
217         let link = {
218             toCP:'toCP_testVal'
219         }
220         let targetCP = ['val1', 'val2'];
221
222         fixture.componentInstance.findOptions = jest.fn();
223         fixture.componentInstance.convertValuesToDropDownOptions = jest.fn(() => 'dummyConvertedVal');
224         fixture.componentInstance.link = link;
225         fixture.componentInstance.targetCP = targetCP;
226
227         // call to the tested function
228         fixture.componentInstance.onTargetSelected(id);
229
230         // expect that
231         expect(fixture.componentInstance.targetCP.length).toBe(2);
232         expect(fixture.componentInstance.targetCP).toEqual(['val1', 'val2']);
233         expect(fixture.componentInstance.link.toCP).toBe('toCP_testVal');
234     });
235
236     it('onTargetCPSelected() -> in case id  -> Validate toCPOriginId' ,() => {
237         // init values / mock functions
238         let id = 'id';
239         let link = {
240             toNode:'testVal',
241             toCPOriginId: 'initValue_ShouldBeChanged'
242         };
243         let option1 = {
244              id: 'something'
245         };
246         let option2 = {
247              id: 'id',
248              data: {"ownerId":1}
249         };
250         fixture.componentInstance.link = link;
251         fixture.componentInstance.findOptions = jest.fn(() => [option1, option2]);
252
253         // call to the tested function
254         fixture.componentInstance.onTargetCPSelected(id);
255
256         // expect that
257         expect(fixture.componentInstance.link.toCPOriginId).toBe(option2.data.ownerId);
258     });
259
260     it('onTargetCPSelected() -> in case id undefined -> Validate toCPOriginId' ,() => {
261         // init values / mock functions
262         let id;
263         let link = {
264             toNode:'testVal',
265             toCPOriginId: 'initValue_ShouldRemain'
266         };
267         let option1 = {
268             id: 'something'
269         };
270         let option2 = {
271             id: 'id',
272             data: {"ownerId":1}
273         };
274         fixture.componentInstance.link = link;
275         fixture.componentInstance.findOptions = jest.fn(() => [option1, option2]);
276
277         // call to the tested function
278         fixture.componentInstance.onTargetCPSelected(id);
279
280         // expect that
281         expect(fixture.componentInstance.link.toCPOriginId).toBe('initValue_ShouldRemain');
282     });
283
284
285     it('findOptions() -> in case item.data.options -> Validate return item.data.options' ,() => {
286         // init values / mock functions
287         const innerMapItemData1: MapItemData = { id: 'innerMapItemData1_id', name: 'innerMapItemData1_name', options: []};
288         const innerServicePathItem: ServicePathMapItem = { id: 'innerServicePathItem_id', data: innerMapItemData1 };
289         const mapItemData1: MapItemData = { id: 'mapItemData1_id', name: 'mapItemData1_name', options: [innerServicePathItem]};
290
291         const servicePathItem: ServicePathMapItem = { id: 'servicePathItem_id', data: mapItemData1 };
292         const arrServicePathItems: ServicePathMapItem[] = [servicePathItem];
293
294         let nodeOrCPId: string = servicePathItem.id;
295
296         // call to the tested function
297         let res = fixture.componentInstance.findOptions(arrServicePathItems, nodeOrCPId);
298
299         // expect that
300         expect(res).toEqual([innerServicePathItem]);
301     });
302
303     it('findOptions() -> in case NOT item || item.data || item.data.options -> Validate return null' ,() => {
304         // init values / mock functions
305         let item = [{
306             // data: {
307                 data:{
308                     name:'data_name',
309                     id: 'data_id'
310                 },
311                 name:'name',
312                 id: 'id'
313             // }
314         }];
315         let items: Array<ServicePathMapItem> = item;
316         let nodeOrCPId: string = 'someString';
317
318         // call to the tested function
319         let res = fixture.componentInstance.findOptions(items, nodeOrCPId);
320
321         // expect that
322         expect(res).toBe(null);
323     });
324
325     it('convertValuesToDropDownOptions() -> Verify that the result is sorted' ,() => {
326         // init values / mock functions
327         const mapItemData1: MapItemData = { id: 'Z_ID', name: 'Z_NAME'};
328         const servicePathItem1: ServicePathMapItem = { id: 'Z_servicePathItem_id', data: mapItemData1 };
329
330         const mapItemData2: MapItemData = { id: 'A_ID', name: 'A_NAME'};
331         const servicePathItem2: ServicePathMapItem = { id: 'A_servicePathItem_id', data: mapItemData2 };
332
333         const mapItemData3: MapItemData = { id: 'M_ID', name: 'M_NAME'};
334         const servicePathItem3: ServicePathMapItem = { id: 'M_servicePathItem_id', data: mapItemData3 };
335
336         const arrServicePathItems: ServicePathMapItem[] = [servicePathItem1, servicePathItem2, servicePathItem3];
337
338         // call to the tested function
339         let res = fixture.componentInstance.convertValuesToDropDownOptions(arrServicePathItems);
340
341         // expect that
342         expect(res.length).toBe(3);
343         expect(res[0].value).toBe("A_servicePathItem_id");
344         expect(res[0].label).toBe("A_NAME");
345         expect(res[1].value).toBe("M_servicePathItem_id");
346         expect(res[1].label).toBe("M_NAME");
347         expect(res[2].value).toBe("Z_servicePathItem_id");
348         expect(res[2].label).toBe("Z_NAME");
349
350     });
351
352     it('parseInitialData() -> link.fromNode Exist => Verify srcCP' ,() => {
353         // init values / mock functions
354
355         //Simulate Array<ServicePathMapItem to pass to the function
356         const mapItemData1: MapItemData = { id: 'mapItemID', name: 'mapItemName'};
357         const servicePathItem1: ServicePathMapItem = { id: 'servicePathItemId', data: mapItemData1 };
358         const arrServicePathItems: ServicePathMapItem[] = [servicePathItem1];
359
360         //Simulate link
361         let link = {
362             fromNode:'testVal'
363         };
364         fixture.componentInstance.link = link;
365
366         //Simulate the response from convertValuesToDropDownOptions()
367         const value = "expected_id_fromNode";
368         const label = "expected_label_fromNode"
369         let result:Array<DropdownValue> = [];
370         result[0] =  new DropdownValue(value, label);
371         fixture.componentInstance.convertValuesToDropDownOptions = jest.fn(() => result);
372
373         //Simulate the response from findOptions()
374         const innerMapItemData1: MapItemData = { id: 'innerMapItemData1_id', name: 'innerMapItemData1_name', options: []};
375         const options: ServicePathMapItem = { id: 'innerServicePathItem_id', data: innerMapItemData1 };
376         fixture.componentInstance.findOptions = jest.fn(() => options);
377
378
379         // call to the tested function
380         fixture.componentInstance.parseInitialData(arrServicePathItems);
381
382         // expect that
383         expect(fixture.componentInstance.srcCP.length).toBe(1);
384         expect(fixture.componentInstance.srcCP[0]).toEqual({
385             "value": value,
386             "label": label,
387             "hidden": false,
388             "selected": false
389         });
390     });
391
392     it('parseInitialData() -> link.fromNode & link.fromCP Exist => Verify srcCP' ,() => {
393         // init values / mock functions
394
395         //Simulate Array<ServicePathMapItem to pass to the function
396         const mapItemData1: MapItemData = { id: 'mapItemID', name: 'mapItemName'};
397         const servicePathItem1: ServicePathMapItem = { id: 'servicePathItemId', data: mapItemData1 };
398         const arrServicePathItems: ServicePathMapItem[] = [servicePathItem1];
399
400         //Simulate link
401         let link = {
402             fromNode:'testVal',
403             fromCP: 'testVal'
404         };
405         fixture.componentInstance.link = link;
406
407         //Simulate the response from convertValuesToDropDownOptions()
408         const value = "expected_id_fromNode_and_fromCP";
409         const label = "expected_label_fromNode_and_fromCP"
410         let result:Array<DropdownValue> = [];
411         result[0] =  new DropdownValue(value, label);
412         fixture.componentInstance.convertValuesToDropDownOptions = jest.fn(() => result);
413
414         //Simulate the response from findOptions()
415         const innerMapItemData1: MapItemData = { id: 'innerMapItemData1_id', name: 'innerMapItemData1_name', options: []};
416         const options: ServicePathMapItem = { id: 'innerServicePathItem_id', data: innerMapItemData1 };
417         fixture.componentInstance.findOptions = jest.fn(() => options);
418
419
420         // call to the tested function
421         fixture.componentInstance.parseInitialData(arrServicePathItems);
422
423         // expect that
424         expect(fixture.componentInstance.srcCP.length).toBe(1);
425         expect(fixture.componentInstance.srcCP[0]).toEqual({
426             "value": value,
427             "label": label,
428             "hidden": false,
429             "selected": false
430         });
431     });
432
433
434     it('parseInitialData() -> link.fromNode & link.fromCP & link.toNode Exist => Verify srcCP' ,() => {
435         // init values / mock functions
436
437         //Simulate Array<ServicePathMapItem to pass to the function
438         const mapItemData1: MapItemData = { id: 'mapItemID', name: 'mapItemName'};
439         const servicePathItem1: ServicePathMapItem = { id: 'servicePathItemId', data: mapItemData1 };
440         const arrServicePathItems: ServicePathMapItem[] = [servicePathItem1];
441
442         //Simulate link
443         let link = {
444             fromNode:'testVal',
445             fromCP: 'testVal',
446             toNode: 'testVal'
447         };
448         fixture.componentInstance.link = link;
449
450         //Simulate the response from convertValuesToDropDownOptions()
451         const value = "expected_id_fromNode_and_fromCP_and_toNode";
452         const label = "expected_label_fromNode_and_fromCP_and_toNode"
453         let result:Array<DropdownValue> = [];
454         result[0] =  new DropdownValue(value, label);
455         fixture.componentInstance.convertValuesToDropDownOptions = jest.fn(() => result);
456
457         //Simulate the response from findOptions()
458         const innerMapItemData1: MapItemData = { id: 'innerMapItemData1_id', name: 'innerMapItemData1_name', options: []};
459         const options: ServicePathMapItem = { id: 'innerServicePathItem_id', data: innerMapItemData1 };
460         fixture.componentInstance.findOptions = jest.fn(() => options);
461
462
463         // call to the tested function
464         fixture.componentInstance.parseInitialData(arrServicePathItems);
465
466         // expect that
467         expect(fixture.componentInstance.srcCP.length).toBe(1);
468         expect(fixture.componentInstance.srcCP[0]).toEqual({
469             "value": value,
470             "label": label,
471             "hidden": false,
472             "selected": false
473         });
474     });
475
476
477
478 });