6be572d16a24f0aa62a860bdf7a0557576f4e25f
[sdc.git] /
1 /*
2  * -
3  *  ============LICENSE_START=======================================================
4  *  Copyright (C) 2022 Nordix Foundation.
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *       http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 import {async, ComponentFixture, TestBed} from '@angular/core/testing';
23
24 import {TypeWorkspacePropertiesComponent} from './type-workspace-properties.component';
25 import {FormsModule} from "@angular/forms";
26 import {TranslateModule} from "../../../shared/translator/translate.module";
27 import {TranslateService} from "../../../shared/translator/translate.service";
28 import {DataTypeService} from "../../../services/data-type.service";
29 import {Observable} from "rxjs/Observable";
30 import {DataTypeModel} from "../../../../models/data-types";
31 import {Component, ViewChild} from "@angular/core";
32 import {PropertyBEModel} from "../../../../models/properties-inputs/property-be-model";
33
34 describe('TypeWorkspacePropertiesComponent', () => {
35     const messages = require("../../../../../assets/languages/en_US.json");
36     let testHostComponent: TestHostComponent;
37     let testHostFixture: ComponentFixture<TestHostComponent>;
38     let dataTypeServiceMock: Partial<DataTypeService> = {
39         findAllProperties: jest.fn( (dataTypeId) => {
40             if (dataTypeId === 'dataTypeId') {
41                 const property1 = new PropertyBEModel();
42                 property1.name = 'property1'
43                 property1.type = 'string'
44                 return Observable.of([property1]);
45             }
46             return Observable.of([]);
47         })
48     };
49
50     let translateServiceMock: Partial<TranslateService> = {
51         'translate': jest.fn( (translateKey: string) => {
52             return messages[translateKey];
53         })
54     };
55
56     beforeEach(async(() => {
57         TestBed.configureTestingModule({
58             declarations: [TypeWorkspacePropertiesComponent, TestHostComponent],
59             imports: [
60                 TranslateModule,
61                 FormsModule
62             ],
63             providers: [
64                 {provide: DataTypeService, useValue: dataTypeServiceMock},
65                 {provide: TranslateService, useValue: translateServiceMock}
66             ]
67         })
68         .compileComponents();
69     }));
70
71     beforeEach(() => {
72         testHostFixture = TestBed.createComponent(TestHostComponent);
73         testHostComponent = testHostFixture.componentInstance;
74         testHostFixture.detectChanges();
75     });
76
77     it('should create', () => {
78         expect(testHostComponent).toBeTruthy();
79     });
80
81     it('empty property list', () => {
82         const element = testHostFixture.nativeElement;
83         const div: HTMLDivElement = element.querySelector('.no-row-text');
84         expect(div.textContent).toContain(messages.PROPERTY_LIST_EMPTY_MESSAGE);
85     });
86
87     it('test property list', () => {
88         testHostFixture = TestBed.createComponent(TestHostComponent);
89         testHostComponent = testHostFixture.componentInstance;
90         const dataType = new DataTypeModel();
91         dataType.uniqueId = 'dataTypeId';
92         testHostComponent.typeWorkspacePropertiesComponent.dataType = dataType;
93         testHostFixture.detectChanges();
94
95         const element = testHostFixture.nativeElement;
96         expect(element.querySelector('.no-row-text')).toBeFalsy();
97         const expectedPropertyName = 'property1';
98         const propertyNameLink: HTMLAnchorElement = element.querySelector(`a[data-tests-id^="property-name-${expectedPropertyName}"]`);
99         expect(propertyNameLink.textContent).toContain(expectedPropertyName);
100     });
101
102     @Component({
103         selector: 'host-component',
104         template: `<app-type-workspace-properties></app-type-workspace-properties>`
105     })
106     class TestHostComponent {
107         @ViewChild(TypeWorkspacePropertiesComponent)
108         public typeWorkspacePropertiesComponent: TypeWorkspacePropertiesComponent;
109     }
110
111 });