Clean up and add license headings
[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 import { HarnessLoader } from '@angular/cdk/testing';
22 import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
23 import { Component, ViewChild, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
24 import { ComponentFixture, TestBed } from '@angular/core/testing';
25 import { FormBuilder, FormGroup } from '@angular/forms';
26 import { MatButtonModule } from '@angular/material/button';
27 import { MatButtonHarness } from '@angular/material/button/testing';
28 import { MatFormFieldModule } from '@angular/material/form-field';
29 import { MatInputModule } from '@angular/material/input';
30 import { MatInputHarness } from '@angular/material/input/testing';
31 import { BrowserModule } from '@angular/platform-browser';
32 import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
33
34 import { NoTypePolicyEditorComponent } from './no-type-policy-editor.component';
35
36 describe('NoTypePolicyEditorComponent', () => {
37   let formGroup: FormGroup = new FormGroup({});
38
39   let component: TestNoTypePolicyEditorComponentHostComponent;
40   let fixture: ComponentFixture<TestNoTypePolicyEditorComponentHostComponent>;
41   let loader: HarnessLoader;
42
43   beforeEach(async () => {
44     TestBed.configureTestingModule({
45       imports: [
46         BrowserModule,
47         BrowserAnimationsModule,
48         MatButtonModule,
49         MatFormFieldModule,
50         MatInputModule
51       ],
52       schemas: [
53         CUSTOM_ELEMENTS_SCHEMA
54       ],
55       declarations: [
56         NoTypePolicyEditorComponent,
57         TestNoTypePolicyEditorComponentHostComponent
58       ],
59       providers: [
60         FormBuilder
61       ]
62     })
63     .compileComponents();
64
65     fixture = TestBed.createComponent(TestNoTypePolicyEditorComponentHostComponent);
66     component = fixture.componentInstance;
67     fixture.detectChanges();
68     loader = TestbedHarnessEnvironment.loader(fixture);
69   });
70
71   it('should create', () => {
72     expect(component).toBeTruthy();
73   });
74
75   it('should be added to form group with required validator', async () => {
76     let textArea: MatInputHarness = await loader.getHarness(MatInputHarness.with({ selector: '#policyJsonTextArea' }));
77
78     expect(formGroup.get('policyJsonTextArea')).toBeTruthy();
79     expect(await textArea.isRequired()).toBeTruthy();
80   });
81
82   it('should contain provided policy json and enabled Format button', async () => {
83     let textArea: MatInputHarness = await loader.getHarness(MatInputHarness.with({ selector: '#policyJsonTextArea' }));
84     expect(await textArea.getValue()).toEqual('{"A":"A"}');
85
86     console.log('Validity:',formGroup.valid);
87     let formatButton: MatButtonHarness = await loader.getHarness(MatButtonHarness.with({ selector: '#formatButton' }));
88     expect(await formatButton.isDisabled()).toBeFalsy();
89   });
90
91   it('Format button should be disabled when json not valid', async () => {
92     const ele = formGroup.get('policyJsonTextArea');
93     ele.setValue('{');
94
95     let formatButton: MatButtonHarness = await loader.getHarness(MatButtonHarness.with({ selector: '#formatButton' }));
96     expect(await formatButton.isDisabled()).toBeTruthy();
97   });
98
99   it('should format unformatted json', async () => {
100     const textArea = formGroup.get('policyJsonTextArea');
101     textArea.setValue('{"A":"A"}');
102     component.noTypePolicyEditorComponent.formatJsonInput();
103     expect(component.noTypePolicyEditorComponent.policyJson).toEqual('{\n  "A": "A"\n}');
104   });
105
106   @Component({
107     selector: `no-type-policy-editor-host-component`,
108     template: `<nrcp-no-type-policy-editor [policyJson]="this.policyJson" [instanceForm]="instanceForm"></nrcp-no-type-policy-editor>`
109   })
110   class TestNoTypePolicyEditorComponentHostComponent {
111     @ViewChild(NoTypePolicyEditorComponent)
112     noTypePolicyEditorComponent: NoTypePolicyEditorComponent;
113     instanceForm: FormGroup = formGroup;
114     policyJson: string = '{"A":"A"}';
115   }
116 });