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===================================
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';
34 import { NoTypePolicyEditorComponent } from './no-type-policy-editor.component';
36 describe('NoTypePolicyEditorComponent', () => {
37 let formGroup: FormGroup = new FormGroup({});
39 let component: TestNoTypePolicyEditorComponentHostComponent;
40 let fixture: ComponentFixture<TestNoTypePolicyEditorComponentHostComponent>;
41 let loader: HarnessLoader;
43 beforeEach(async () => {
44 TestBed.configureTestingModule({
47 BrowserAnimationsModule,
53 CUSTOM_ELEMENTS_SCHEMA
56 NoTypePolicyEditorComponent,
57 TestNoTypePolicyEditorComponentHostComponent
65 fixture = TestBed.createComponent(TestNoTypePolicyEditorComponentHostComponent);
66 component = fixture.componentInstance;
67 fixture.detectChanges();
68 loader = TestbedHarnessEnvironment.loader(fixture);
71 it('should create', () => {
72 expect(component).toBeTruthy();
75 it('should be added to form group with required validator', async () => {
76 let textArea: MatInputHarness = await loader.getHarness(MatInputHarness.with({ selector: '#policyJsonTextArea' }));
78 expect(formGroup.get('policyJsonTextArea')).toBeTruthy();
79 expect(await textArea.isRequired()).toBeTruthy();
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"}');
86 console.log('Validity:',formGroup.valid);
87 let formatButton: MatButtonHarness = await loader.getHarness(MatButtonHarness.with({ selector: '#formatButton' }));
88 expect(await formatButton.isDisabled()).toBeFalsy();
91 it('Format button should be disabled when json not valid', async () => {
92 const ele = formGroup.get('policyJsonTextArea');
95 let formatButton: MatButtonHarness = await loader.getHarness(MatButtonHarness.with({ selector: '#formatButton' }));
96 expect(await formatButton.isDisabled()).toBeTruthy();
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}');
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>`
110 class TestNoTypePolicyEditorComponentHostComponent {
111 @ViewChild(NoTypePolicyEditorComponent)
112 noTypePolicyEditorComponent: NoTypePolicyEditorComponent;
113 instanceForm: FormGroup = formGroup;
114 policyJson: string = '{"A":"A"}';