2 // ========================LICENSE_START=================================
5 // Copyright (C) 2021: Nordix Foundation
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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===================================
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';
35 import { NoTypePolicyEditorComponent } from './no-type-policy-editor.component';
37 describe('NoTypePolicyEditorComponent', () => {
38 let formGroup: FormGroup = new FormGroup({});
40 let component: TestNoTypePolicyEditorComponentHostComponent;
41 let fixture: ComponentFixture<TestNoTypePolicyEditorComponentHostComponent>;
42 let loader: HarnessLoader;
44 beforeEach(async () => {
45 TestBed.configureTestingModule({
48 BrowserAnimationsModule,
54 CUSTOM_ELEMENTS_SCHEMA
57 NoTypePolicyEditorComponent,
58 TestNoTypePolicyEditorComponentHostComponent
66 fixture = TestBed.createComponent(TestNoTypePolicyEditorComponentHostComponent);
67 component = fixture.componentInstance;
68 fixture.detectChanges();
69 loader = TestbedHarnessEnvironment.loader(fixture);
72 it('should create', () => {
73 expect(component).toBeTruthy();
76 it('should be added to form group with required validator', async () => {
77 let textArea: MatInputHarness = await loader.getHarness(MatInputHarness.with({ selector: '#policyJsonTextArea' }));
79 expect(formGroup.get('policyJsonTextArea')).toBeTruthy();
80 expect(await textArea.isRequired()).toBeTruthy();
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"}');
87 console.log('Validity:',formGroup.valid);
88 let formatButton: MatButtonHarness = await loader.getHarness(MatButtonHarness.with({ selector: '#formatButton' }));
89 expect(await formatButton.isDisabled()).toBeFalsy();
92 it('Format button should be disabled when json not valid', async () => {
93 const ele = formGroup.get('policyJsonTextArea');
96 let formatButton: MatButtonHarness = await loader.getHarness(MatButtonHarness.with({ selector: '#formatButton' }));
97 expect(await formatButton.isDisabled()).toBeTruthy();
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}');
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>`
111 class TestNoTypePolicyEditorComponentHostComponent {
112 @ViewChild(NoTypePolicyEditorComponent)
113 noTypePolicyEditorComponent: NoTypePolicyEditorComponent;
114 instanceForm: FormGroup = formGroup;
115 policyJson: string = '{"A":"A"}';