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,
52 schemas: [CUSTOM_ELEMENTS_SCHEMA],
54 NoTypePolicyEditorComponent,
55 TestNoTypePolicyEditorComponentHostComponent,
57 providers: [FormBuilder],
58 }).compileComponents();
60 fixture = TestBed.createComponent(
61 TestNoTypePolicyEditorComponentHostComponent
63 component = fixture.componentInstance;
64 fixture.detectChanges();
65 loader = TestbedHarnessEnvironment.loader(fixture);
68 it("should create", () => {
69 expect(component).toBeTruthy();
72 it("should contain provided policy json and enabled Format button", async () => {
73 let textArea: MatInputHarness = await loader.getHarness(
74 MatInputHarness.with({ selector: "#policyJsonTextArea" })
76 expect(await textArea.getValue()).toEqual('{"A":"A"}');
78 let formatButton: MatButtonHarness = await loader.getHarness(
79 MatButtonHarness.with({ selector: "#formatButton" })
81 expect(await formatButton.isDisabled()).toBeFalsy();
84 it("Format button should be disabled when json not valid", async () => {
85 const ele = component.noTypePolicyEditorComponent.instanceForm.get("policyJsonTextArea");
88 let formatButton: MatButtonHarness = await loader.getHarness(
89 MatButtonHarness.with({ selector: "#formatButton" })
91 expect(await formatButton.isDisabled()).toBeTruthy();
94 it("should format unformatted json", async () => {
95 const textArea = component.noTypePolicyEditorComponent.instanceForm.get("policyJsonTextArea");
96 textArea.setValue('{"A":"A"}');
97 component.noTypePolicyEditorComponent.formatJsonInput();
98 expect(component.noTypePolicyEditorComponent.policyJson).toEqual(
104 selector: `no-type-policy-editor-host-component`,
105 template: `<nrcp-no-type-policy-editor
106 [policyJson]="this.policyJson"
107 ></nrcp-no-type-policy-editor>`,
109 class TestNoTypePolicyEditorComponentHostComponent {
110 @ViewChild(NoTypePolicyEditorComponent)
111 noTypePolicyEditorComponent: NoTypePolicyEditorComponent;
112 policyJson: string = '{"A":"A"}';