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 { MatButtonModule } from "@angular/material/button";
26 import { MatButtonHarness } from "@angular/material/button/testing";
27 import { MatFormFieldModule } from "@angular/material/form-field";
28 import { MatInputModule } from "@angular/material/input";
29 import { MatInputHarness } from "@angular/material/input/testing";
30 import { BrowserModule } from "@angular/platform-browser";
31 import { BrowserAnimationsModule } from "@angular/platform-browser/animations";
33 import { NoTypePolicyEditorComponent } from "./no-type-policy-editor.component";
35 describe("NoTypePolicyEditorComponent", () => {
37 let component: TestNoTypePolicyEditorComponentHostComponent;
38 let fixture: ComponentFixture<TestNoTypePolicyEditorComponentHostComponent>;
39 let loader: HarnessLoader;
41 beforeEach(async () => {
42 TestBed.configureTestingModule({
45 BrowserAnimationsModule,
50 schemas: [CUSTOM_ELEMENTS_SCHEMA],
52 NoTypePolicyEditorComponent,
53 TestNoTypePolicyEditorComponentHostComponent,
55 }).compileComponents();
57 fixture = TestBed.createComponent(
58 TestNoTypePolicyEditorComponentHostComponent
60 component = fixture.componentInstance;
61 fixture.detectChanges();
62 loader = TestbedHarnessEnvironment.loader(fixture);
65 it("should create", () => {
66 expect(component).toBeTruthy();
69 it("should contain provided policy json and enabled Format button", async () => {
70 let textArea: MatInputHarness = await loader.getHarness(
71 MatInputHarness.with({ selector: "#policyJsonTextArea" })
73 expect(await textArea.getValue()).toEqual('{"A":"A"}');
75 let formatButton: MatButtonHarness = await loader.getHarness(
76 MatButtonHarness.with({ selector: "#formatButton" })
78 expect(await formatButton.isDisabled()).toBeFalsy();
81 it("Format button should be disabled when json not valid", async () => {
82 const ele = component.noTypePolicyEditorComponent.instanceForm.get(
87 let formatButton: MatButtonHarness = await loader.getHarness(
88 MatButtonHarness.with({ selector: "#formatButton" })
90 expect(await formatButton.isDisabled()).toBeTruthy();
93 it("should format unformatted json", async () => {
94 const textArea = component.noTypePolicyEditorComponent.instanceForm.get(
97 textArea.setValue('{"A":"A"}');
98 component.noTypePolicyEditorComponent.formatJsonInput();
99 expect(component.noTypePolicyEditorComponent.policyJson).toEqual(
105 selector: `no-type-policy-editor-host-component`,
106 template: `<nrcp-no-type-policy-editor
107 [policyJson]="this.policyJson"
108 ></nrcp-no-type-policy-editor>`,
110 class TestNoTypePolicyEditorComponentHostComponent {
111 @ViewChild(NoTypePolicyEditorComponent)
112 noTypePolicyEditorComponent: NoTypePolicyEditorComponent;
113 policyJson: string = '{"A":"A"}';