Catalog alignment
[sdc.git] / catalog-ui / src / app / ng2 / components / logic / properties-table / properties-table.component.spec.ts
1 import { NO_ERRORS_SCHEMA, SimpleChange } from '@angular/core';
2 import { ComponentFixture } from '@angular/core/testing';
3 import { ConfigureFn, configureTests } from '../../../../../jest/test-config.helper';
4 import { DerivedFEProperty } from '../../../../models/properties-inputs/derived-fe-property';
5 import { PropertyBEModel } from '../../../../models/properties-inputs/property-be-model';
6 import { PropertyFEModel } from '../../../../models/properties-inputs/property-fe-model';
7 import { ContentAfterLastDotPipe } from '../../../pipes/contentAfterLastDot.pipe';
8 import { KeysPipe } from '../../../pipes/keys.pipe';
9 import { PropertiesOrderByPipe } from '../../../pipes/properties-order-by.pipe';
10 import { SearchFilterPipe } from '../../../pipes/searchFilter.pipe';
11 import { ModalService } from '../../../services/modal.service';
12 import { PropertiesService } from '../../../services/properties.service';
13 import { PropertiesTableComponent, PropertyRowSelectedEvent } from './properties-table.component';
14
15 describe('properties-table component', () => {
16
17     let fixture: ComponentFixture<PropertiesTableComponent>;
18     let propertiesServiceMock: Partial<PropertiesService>;
19     let modalServiceMock: Partial<ModalService>;
20
21     beforeEach(
22         () => {
23             propertiesServiceMock = {
24                 undoDisableRelatedProperties: jest.fn(),
25                 disableRelatedProperties: jest.fn()
26             };
27             modalServiceMock = {
28
29             };
30
31             const configure: ConfigureFn = (testBed) => {
32                 testBed.configureTestingModule({
33                     declarations: [
34                         PropertiesTableComponent,
35                         KeysPipe,
36                         PropertiesOrderByPipe,
37                         SearchFilterPipe,
38                         ContentAfterLastDotPipe
39                     ],
40                     imports: [],
41                     schemas: [NO_ERRORS_SCHEMA],
42                     providers: [
43                         {provide: PropertiesService, useValue: propertiesServiceMock},
44                         {provide: ModalService, useValue: modalServiceMock}
45                     ],
46                 });
47             };
48
49             configureTests(configure).then((testBed) => {
50                 fixture = testBed.createComponent(PropertiesTableComponent);
51             });
52         }
53     );
54
55     it('When Properties assignment page is loaded, it is sorted by property name (acsending)', () => {
56         const fePropertiesMapValues = new SimpleChange('previousValue', 'currentValue', true);
57         const changes = {
58             fePropertiesMap: fePropertiesMapValues
59         };
60
61         // init values before ngOnChanges was called
62         fixture.componentInstance.sortBy = 'existingValue';
63
64         fixture.componentInstance.ngOnChanges(changes);
65
66         expect (fixture.componentInstance.reverse).toEqual(true);
67         // expect (fixture.componentInstance.direction).toEqual(1);
68         expect (fixture.componentInstance.direction).toEqual(fixture.componentInstance.ascUpperLettersFirst);
69         expect (fixture.componentInstance.sortBy).toEqual('name');
70         expect (fixture.componentInstance.path.length).toEqual(1);
71         expect (fixture.componentInstance.path[0]).toEqual('name');
72     });
73
74     it('When ngOnChanges is called without fePropertiesMap,' +
75         ' sortBy will remain as it was', () => {
76         const fePropertiesMapValues = new SimpleChange('previousValue', 'currentValue', true);
77         const changes = {
78             dummyKey: fePropertiesMapValues
79         };
80
81         // init values before ngOnChanges was called
82         fixture.componentInstance.sortBy = 'existingValue';
83         fixture.componentInstance.sort = jest.fn();
84
85         fixture.componentInstance.ngOnChanges(changes);
86
87         expect (fixture.componentInstance.sortBy).toEqual('existingValue');
88     });
89
90     it ('When sort is called init this.direction to 1', () => {
91         // init values
92         fixture.componentInstance.reverse = false;
93         fixture.componentInstance.direction = 0;
94         fixture.componentInstance.sortBy = 'initialize.Value';
95         fixture.componentInstance.path = [];
96
97         // call sore function
98         fixture.componentInstance.sort('initialize.Value');
99
100         // expect that
101         expect (fixture.componentInstance.reverse).toBe(true);
102         expect (fixture.componentInstance.direction).toBe(fixture.componentInstance.ascUpperLettersFirst);
103         expect (fixture.componentInstance.sortBy).toBe('initialize.Value');
104         expect (fixture.componentInstance.path.length).toBe(2);
105         expect (fixture.componentInstance.path[0]).toBe('initialize');
106         expect (fixture.componentInstance.path[1]).toBe('Value');
107     });
108
109     it ('When sort is called init this.direction to -1', () => {
110         // init values
111         fixture.componentInstance.reverse = true;
112         fixture.componentInstance.direction = 0;
113         fixture.componentInstance.sortBy = 'initialize.Value';
114         fixture.componentInstance.path = [];
115
116         // call sore function
117         fixture.componentInstance.sort('initialize.Value');
118
119         // expect that
120         expect (fixture.componentInstance.reverse).toBe(false);
121         expect (fixture.componentInstance.direction).toBe(fixture.componentInstance.descLowerLettersFirst);
122     });
123
124     it ('When onPropertyChanged is called, event is emitted' , () => {
125         spyOn(fixture.componentInstance.emitter, 'emit');
126         fixture.componentInstance.onPropertyChanged('testProperty');
127         expect(fixture.componentInstance.emitter.emit).toHaveBeenCalledWith('testProperty');
128     });
129
130     it ('When onClickPropertyRow is called, selectedPropertyId is updated and event is emitted.' , () => {
131         const propertyFEModel = new PropertyFEModel(new PropertyBEModel());
132         propertyFEModel.name = 'propertyName';
133         const propertyRowSelectedEvent: PropertyRowSelectedEvent = new PropertyRowSelectedEvent(propertyFEModel, 'instanceName');
134
135         spyOn(fixture.componentInstance.selectPropertyRow, 'emit');
136         fixture.componentInstance.onClickPropertyRow(propertyFEModel, 'instanceName');
137
138         expect (fixture.componentInstance.selectedPropertyId).toBe('propertyName');
139         expect (fixture.componentInstance.selectPropertyRow.emit).toHaveBeenCalledWith(propertyRowSelectedEvent);
140     });
141
142     it ('When onClickPropertyInnerRow is called, event is emitted.' , () => {
143         const derivedFEProperty = new DerivedFEProperty(new PropertyBEModel());
144         const propertyRowSelectedEvent: PropertyRowSelectedEvent = new PropertyRowSelectedEvent(derivedFEProperty, 'instanceName');
145         spyOn(fixture.componentInstance.selectPropertyRow, 'emit');
146         fixture.componentInstance.onClickPropertyInnerRow(derivedFEProperty, 'instanceName');
147
148         expect (fixture.componentInstance.selectPropertyRow.emit).toHaveBeenCalledWith(propertyRowSelectedEvent);
149     });
150
151     it ('When propertyChecked is called, propertiesService.undoDisableRelatedProperties is called and event is emitted.' , () => {
152
153         const propertyFEModel = new PropertyFEModel(new PropertyBEModel());
154         propertyFEModel.isSelected = false;
155         const propertyRowSelectedEvent: PropertyRowSelectedEvent = new PropertyRowSelectedEvent(propertyFEModel, 'instanceName1');
156
157         spyOn(fixture.componentInstance.updateCheckedPropertyCount, 'emit');
158         fixture.componentInstance.propertyChecked(propertyFEModel);
159         expect (propertiesServiceMock.undoDisableRelatedProperties).toHaveBeenCalledWith(propertyFEModel, undefined);
160         expect (fixture.componentInstance.updateCheckedPropertyCount.emit).toHaveBeenCalledWith(false);
161     });
162
163     it ('When propertyChecked is called, propertiesService.disableRelatedProperties is called and event is emitted.' , () => {
164
165         const propertyFEModel = new PropertyFEModel(new PropertyBEModel());
166         propertyFEModel.isSelected = true;
167         const propertyRowSelectedEvent: PropertyRowSelectedEvent = new PropertyRowSelectedEvent(propertyFEModel, 'instanceName1');
168
169         spyOn(fixture.componentInstance.updateCheckedPropertyCount, 'emit');
170         fixture.componentInstance.propertyChecked(propertyFEModel);
171         expect (propertiesServiceMock.disableRelatedProperties).toHaveBeenCalledWith(propertyFEModel, undefined);
172         expect (fixture.componentInstance.updateCheckedPropertyCount.emit).toHaveBeenCalledWith(true);
173     });
174
175 });