77477ed8ef1e7bf970190be82710ae782b74fd27
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / policy / no-type-policy-editor / no-type-policy-editor.component.spec.ts
1 // -
2 //   ========================LICENSE_START=================================
3 //   O-RAN-SC
4 //   %%
5 //   Copyright (C) 2021: Nordix Foundation
6 //   %%
7 //   Licensed under the Apache License, Version 2.0 (the "License");
8 //   you may not use this file except in compliance with the License.
9 //   You may obtain a copy of the License at
10 //
11 //        http://www.apache.org/licenses/LICENSE-2.0
12 //
13 //   Unless required by applicable law or agreed to in writing, software
14 //   distributed under the License is distributed on an "AS IS" BASIS,
15 //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 //   See the License for the specific language governing permissions and
17 //   limitations under the License.
18 //   ========================LICENSE_END===================================
19 //  /
20 //
21
22 import { HarnessLoader } from '@angular/cdk/testing';
23 import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
24 import { Component, ViewChild, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
25 import { ComponentFixture, TestBed } from '@angular/core/testing';
26 import { FormBuilder, FormGroup } from '@angular/forms';
27 import { MatButtonModule } from '@angular/material/button';
28 import { MatButtonHarness } from '@angular/material/button/testing';
29 import { MatFormFieldModule } from '@angular/material/form-field';
30 import { MatInputModule } from '@angular/material/input';
31 import { MatInputHarness } from '@angular/material/input/testing';
32 import { BrowserModule } from '@angular/platform-browser';
33 import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
34
35 import { NoTypePolicyEditorComponent } from './no-type-policy-editor.component';
36
37 describe('NoTypePolicyEditorComponent', () => {
38   let formGroup: FormGroup = new FormGroup({});
39
40   let component: TestNoTypePolicyEditorComponentHostComponent;
41   let fixture: ComponentFixture<TestNoTypePolicyEditorComponentHostComponent>;
42   let loader: HarnessLoader;
43
44   beforeEach(async () => {
45     TestBed.configureTestingModule({
46       imports: [
47         BrowserModule,
48         BrowserAnimationsModule,
49         MatButtonModule,
50         MatFormFieldModule,
51         MatInputModule
52       ],
53       schemas: [
54         CUSTOM_ELEMENTS_SCHEMA
55       ],
56       declarations: [
57         NoTypePolicyEditorComponent,
58         TestNoTypePolicyEditorComponentHostComponent
59       ],
60       providers: [
61         FormBuilder
62       ]
63     })
64     .compileComponents();
65
66     fixture = TestBed.createComponent(TestNoTypePolicyEditorComponentHostComponent);
67     component = fixture.componentInstance;
68     fixture.detectChanges();
69     loader = TestbedHarnessEnvironment.loader(fixture);
70   });
71
72   it('should create', () => {
73     expect(component).toBeTruthy();
74   });
75
76   it('should be added to form group with required validator', async () => {
77     let textArea: MatInputHarness = await loader.getHarness(MatInputHarness.with({ selector: '#policyJsonTextArea' }));
78
79     expect(formGroup.get('policyJsonTextArea')).toBeTruthy();
80     expect(await textArea.isRequired()).toBeTruthy();
81   });
82
83   it('should contain provided policy json and enabled Format button', async () => {
84     let textArea: MatInputHarness = await loader.getHarness(MatInputHarness.with({ selector: '#policyJsonTextArea' }));
85     expect(await textArea.getValue()).toEqual('{"A":"A"}');
86
87     console.log('Validity:',formGroup.valid);
88     let formatButton: MatButtonHarness = await loader.getHarness(MatButtonHarness.with({ selector: '#formatButton' }));
89     expect(await formatButton.isDisabled()).toBeFalsy();
90   });
91
92   it('Format button should be disabled when json not valid', async () => {
93     const ele = formGroup.get('policyJsonTextArea');
94     ele.setValue('{');
95
96     let formatButton: MatButtonHarness = await loader.getHarness(MatButtonHarness.with({ selector: '#formatButton' }));
97     expect(await formatButton.isDisabled()).toBeTruthy();
98   });
99
100   it('should format unformatted json', async () => {
101     const textArea = formGroup.get('policyJsonTextArea');
102     textArea.setValue('{"A":"A"}');
103     component.noTypePolicyEditorComponent.formatJsonInput();
104     expect(component.noTypePolicyEditorComponent.policyJson).toEqual('{\n  "A": "A"\n}');
105   });
106
107   @Component({
108     selector: `no-type-policy-editor-host-component`,
109     template: `<nrcp-no-type-policy-editor [policyJson]="this.policyJson" [instanceForm]="instanceForm"></nrcp-no-type-policy-editor>`
110   })
111   class TestNoTypePolicyEditorComponentHostComponent {
112     @ViewChild(NoTypePolicyEditorComponent)
113     noTypePolicyEditorComponent: NoTypePolicyEditorComponent;
114     instanceForm: FormGroup = formGroup;
115     policyJson: string = '{"A":"A"}';
116   }
117 });