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';
15 describe('properties-table component', () => {
17 let fixture: ComponentFixture<PropertiesTableComponent>;
18 let propertiesServiceMock: Partial<PropertiesService>;
19 let modalServiceMock: Partial<ModalService>;
23 propertiesServiceMock = {
24 undoDisableRelatedProperties: jest.fn(),
25 disableRelatedProperties: jest.fn()
31 const configure: ConfigureFn = (testBed) => {
32 testBed.configureTestingModule({
34 PropertiesTableComponent,
36 PropertiesOrderByPipe,
38 ContentAfterLastDotPipe
41 schemas: [NO_ERRORS_SCHEMA],
43 {provide: PropertiesService, useValue: propertiesServiceMock},
44 {provide: ModalService, useValue: modalServiceMock}
49 configureTests(configure).then((testBed) => {
50 fixture = testBed.createComponent(PropertiesTableComponent);
55 it('When Properties assignment page is loaded, it is sorted by property name (acsending)', () => {
56 const fePropertiesMapValues = new SimpleChange('previousValue', 'currentValue', true);
58 fePropertiesMap: fePropertiesMapValues
61 // init values before ngOnChanges was called
62 fixture.componentInstance.sortBy = 'existingValue';
64 fixture.componentInstance.ngOnChanges(changes);
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');
74 it('When ngOnChanges is called without fePropertiesMap,' +
75 ' sortBy will remain as it was', () => {
76 const fePropertiesMapValues = new SimpleChange('previousValue', 'currentValue', true);
78 dummyKey: fePropertiesMapValues
81 // init values before ngOnChanges was called
82 fixture.componentInstance.sortBy = 'existingValue';
83 fixture.componentInstance.sort = jest.fn();
85 fixture.componentInstance.ngOnChanges(changes);
87 expect (fixture.componentInstance.sortBy).toEqual('existingValue');
90 it ('When sort is called init this.direction to 1', () => {
92 fixture.componentInstance.reverse = false;
93 fixture.componentInstance.direction = 0;
94 fixture.componentInstance.sortBy = 'initialize.Value';
95 fixture.componentInstance.path = [];
98 fixture.componentInstance.sort('initialize.Value');
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');
109 it ('When sort is called init this.direction to -1', () => {
111 fixture.componentInstance.reverse = true;
112 fixture.componentInstance.direction = 0;
113 fixture.componentInstance.sortBy = 'initialize.Value';
114 fixture.componentInstance.path = [];
116 // call sore function
117 fixture.componentInstance.sort('initialize.Value');
120 expect (fixture.componentInstance.reverse).toBe(false);
121 expect (fixture.componentInstance.direction).toBe(fixture.componentInstance.descLowerLettersFirst);
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');
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');
135 spyOn(fixture.componentInstance.selectPropertyRow, 'emit');
136 fixture.componentInstance.onClickPropertyRow(propertyFEModel, 'instanceName');
138 expect (fixture.componentInstance.selectedPropertyId).toBe('propertyName');
139 expect (fixture.componentInstance.selectPropertyRow.emit).toHaveBeenCalledWith(propertyRowSelectedEvent);
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');
148 expect (fixture.componentInstance.selectPropertyRow.emit).toHaveBeenCalledWith(propertyRowSelectedEvent);
151 it ('When propertyChecked is called, propertiesService.undoDisableRelatedProperties is called and event is emitted.' , () => {
153 const propertyFEModel = new PropertyFEModel(new PropertyBEModel());
154 propertyFEModel.isSelected = false;
155 const propertyRowSelectedEvent: PropertyRowSelectedEvent = new PropertyRowSelectedEvent(propertyFEModel, 'instanceName1');
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);
163 it ('When propertyChecked is called, propertiesService.disableRelatedProperties is called and event is emitted.' , () => {
165 const propertyFEModel = new PropertyFEModel(new PropertyBEModel());
166 propertyFEModel.isSelected = true;
167 const propertyRowSelectedEvent: PropertyRowSelectedEvent = new PropertyRowSelectedEvent(propertyFEModel, 'instanceName1');
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);