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";
9 describe('artifact form component', () => {
11 let fixture: ComponentFixture<LinkRowComponent>;
12 let cacheServiceMock: Partial<CacheService>;
25 const configure: ConfigureFn = testBed => {
26 testBed.configureTestingModule({
27 declarations: [LinkRowComponent],
29 schemas: [NO_ERRORS_SCHEMA],
35 configureTests(configure).then(testBed => {
36 fixture = testBed.createComponent(LinkRowComponent);
42 it('should match current snapshot of artifact form component', () => {
43 expect(fixture).toMatchSnapshot();
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;
53 // call to the tested function
54 fixture.componentInstance.ngOnChanges();
57 expect(fixture.componentInstance.parseInitialData).toHaveBeenCalledWith(data);
60 it('onSourceSelected() -> in case id -> srcCP, link.fromCP, link.toNode, link.toCP, target, targetCP should be updated accordingly' ,() => {
61 // init values / mock functions
69 let target = ['val1', 'val2'];
70 let targetCP = ['val1', 'val2'];
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;
79 // call to the tested function
80 fixture.componentInstance.onSourceSelected(id);
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);
92 it('onSourceSelected() -> in case id undefined -> No Change to srcCP, link.fromCP, link.toNode, link.toCP, target, targetCP' ,() => {
93 // init values / mock functions
101 let target = ['val1', 'val2'];
102 let targetCP = ['val1', 'val2'];
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;
111 // call to the tested function
112 fixture.componentInstance.onSourceSelected(id);
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');
124 it('onSrcCPSelected() -> in case id -> Verify target, link.fromCPOriginId, link.toNode, link.toCP, targetCP.length' ,() => {
125 // init values / mock functions
129 toCPOriginId: 'initValue_ShouldBeChanged'
139 fixture.componentInstance.link = link;
140 fixture.componentInstance.findOptions = jest.fn(() => [option1, option2]);
141 fixture.componentInstance.convertValuesToDropDownOptions = jest.fn(() => 'dummyConvertedVal');
143 // call to the tested function
144 fixture.componentInstance.onSrcCPSelected(id);
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);
155 it('onSrcCPSelected() -> in case id undefined -> Verify target, link.fromCPOriginId, link.toNode, link.toCP, targetCP.length' ,() => {
156 // init values / mock functions
159 let targetInput:Array<DropdownValue> = [{value:'Value', label:'Label', hidden:true, selected:true}];
162 fromCPOriginId:'expectedLinkFromCPOriginId',
163 toNode:'expectedLinkToNode',
164 toCP:'expectedLinkToCP',
169 // ForwardingPathLink Object
176 fixture.componentInstance.target = targetInput;
177 fixture.componentInstance.link = linkInput;
178 fixture.componentInstance.targetCP = targetInput;
181 // call to the tested function
182 fixture.componentInstance.onSrcCPSelected(id);
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);
192 it('onTargetSelected() -> in case id -> Verify targetCP & link.toCP' ,() => {
193 // init values / mock functions
198 let targetCP = ['val1', 'val2'];
200 fixture.componentInstance.findOptions = jest.fn();
201 fixture.componentInstance.convertValuesToDropDownOptions = jest.fn(() => 'dummyConvertedVal');
202 fixture.componentInstance.link = link;
203 fixture.componentInstance.targetCP = targetCP;
205 // call to the tested function
206 fixture.componentInstance.onTargetSelected(id);
209 expect(fixture.componentInstance.targetCP).toBe('dummyConvertedVal');
210 expect(fixture.componentInstance.link.toCP).toBe('');
214 it('onTargetSelected() -> in case id undefined -> Verify targetCP & link.toCP' ,() => {
215 // init values / mock functions
220 let targetCP = ['val1', 'val2'];
222 fixture.componentInstance.findOptions = jest.fn();
223 fixture.componentInstance.convertValuesToDropDownOptions = jest.fn(() => 'dummyConvertedVal');
224 fixture.componentInstance.link = link;
225 fixture.componentInstance.targetCP = targetCP;
227 // call to the tested function
228 fixture.componentInstance.onTargetSelected(id);
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');
236 it('onTargetCPSelected() -> in case id -> Validate toCPOriginId' ,() => {
237 // init values / mock functions
241 toCPOriginId: 'initValue_ShouldBeChanged'
250 fixture.componentInstance.link = link;
251 fixture.componentInstance.findOptions = jest.fn(() => [option1, option2]);
253 // call to the tested function
254 fixture.componentInstance.onTargetCPSelected(id);
257 expect(fixture.componentInstance.link.toCPOriginId).toBe(option2.data.ownerId);
260 it('onTargetCPSelected() -> in case id undefined -> Validate toCPOriginId' ,() => {
261 // init values / mock functions
265 toCPOriginId: 'initValue_ShouldRemain'
274 fixture.componentInstance.link = link;
275 fixture.componentInstance.findOptions = jest.fn(() => [option1, option2]);
277 // call to the tested function
278 fixture.componentInstance.onTargetCPSelected(id);
281 expect(fixture.componentInstance.link.toCPOriginId).toBe('initValue_ShouldRemain');
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]};
291 const servicePathItem: ServicePathMapItem = { id: 'servicePathItem_id', data: mapItemData1 };
292 const arrServicePathItems: ServicePathMapItem[] = [servicePathItem];
294 let nodeOrCPId: string = servicePathItem.id;
296 // call to the tested function
297 let res = fixture.componentInstance.findOptions(arrServicePathItems, nodeOrCPId);
300 expect(res).toEqual([innerServicePathItem]);
303 it('findOptions() -> in case NOT item || item.data || item.data.options -> Validate return null' ,() => {
304 // init values / mock functions
315 let items: Array<ServicePathMapItem> = item;
316 let nodeOrCPId: string = 'someString';
318 // call to the tested function
319 let res = fixture.componentInstance.findOptions(items, nodeOrCPId);
322 expect(res).toBe(null);
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 };
330 const mapItemData2: MapItemData = { id: 'A_ID', name: 'A_NAME'};
331 const servicePathItem2: ServicePathMapItem = { id: 'A_servicePathItem_id', data: mapItemData2 };
333 const mapItemData3: MapItemData = { id: 'M_ID', name: 'M_NAME'};
334 const servicePathItem3: ServicePathMapItem = { id: 'M_servicePathItem_id', data: mapItemData3 };
336 const arrServicePathItems: ServicePathMapItem[] = [servicePathItem1, servicePathItem2, servicePathItem3];
338 // call to the tested function
339 let res = fixture.componentInstance.convertValuesToDropDownOptions(arrServicePathItems);
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");
352 it('parseInitialData() -> link.fromNode Exist => Verify srcCP' ,() => {
353 // init values / mock functions
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];
364 fixture.componentInstance.link = link;
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);
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);
379 // call to the tested function
380 fixture.componentInstance.parseInitialData(arrServicePathItems);
383 expect(fixture.componentInstance.srcCP.length).toBe(1);
384 expect(fixture.componentInstance.srcCP[0]).toEqual({
392 it('parseInitialData() -> link.fromNode & link.fromCP Exist => Verify srcCP' ,() => {
393 // init values / mock functions
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];
405 fixture.componentInstance.link = link;
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);
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);
420 // call to the tested function
421 fixture.componentInstance.parseInitialData(arrServicePathItems);
424 expect(fixture.componentInstance.srcCP.length).toBe(1);
425 expect(fixture.componentInstance.srcCP[0]).toEqual({
434 it('parseInitialData() -> link.fromNode & link.fromCP & link.toNode Exist => Verify srcCP' ,() => {
435 // init values / mock functions
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];
448 fixture.componentInstance.link = link;
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);
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);
463 // call to the tested function
464 fixture.componentInstance.parseInitialData(arrServicePathItems);
467 expect(fixture.componentInstance.srcCP.length).toBe(1);
468 expect(fixture.componentInstance.srcCP[0]).toEqual({