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", () => {
36 let component: TestNoTypePolicyEditorComponentHostComponent;
37 let fixture: ComponentFixture<TestNoTypePolicyEditorComponentHostComponent>;
38 let loader: HarnessLoader;
40 beforeEach(async () => {
41 TestBed.configureTestingModule({
44 BrowserAnimationsModule,
49 schemas: [CUSTOM_ELEMENTS_SCHEMA],
51 NoTypePolicyEditorComponent,
52 TestNoTypePolicyEditorComponentHostComponent,
54 }).compileComponents();
56 fixture = TestBed.createComponent(
57 TestNoTypePolicyEditorComponentHostComponent
59 component = fixture.componentInstance;
60 fixture.detectChanges();
61 loader = TestbedHarnessEnvironment.loader(fixture);
64 it("should create", () => {
65 expect(component).toBeTruthy();
68 it("should contain provided policy json and enabled Format button", async () => {
69 const textArea: MatInputHarness = await loader.getHarness(
70 MatInputHarness.with({ selector: "#policyJsonTextArea" })
72 expect(await textArea.getValue()).toEqual('{"A":"A"}');
74 const formatButton: MatButtonHarness = await loader.getHarness(
75 MatButtonHarness.with({ selector: "#formatButton" })
77 expect(await formatButton.isDisabled()).toBeFalsy();
80 it("Format button should be disabled when json not valid", async () => {
81 const ele = component.noTypePolicyEditorComponent.instanceForm.get(
86 const formatButton: MatButtonHarness = await loader.getHarness(
87 MatButtonHarness.with({ selector: "#formatButton" })
89 expect(await formatButton.isDisabled()).toBeTruthy();
92 it("should format unformatted json", async () => {
93 const textArea = component.noTypePolicyEditorComponent.instanceForm.get(
96 expect(textArea.value).toEqual('{"A":"A"}');
98 component.noTypePolicyEditorComponent.formatJsonInput();
99 expect(textArea.value).toEqual('{\n "A": "A"\n}');
102 it("should send valid json", async () => {
103 const textArea = component.noTypePolicyEditorComponent.instanceForm.get(
106 expect(textArea.value).toEqual('{"A":"A"}');
108 let validJson: string;
109 component.noTypePolicyEditorComponent.validJson.subscribe((json: string) => {
113 textArea.setValue('{"B":"B"}');
114 expect(validJson).toEqual('{"B":"B"}');
117 it("should send null when invalid json", async () => {
118 const textArea = component.noTypePolicyEditorComponent.instanceForm.get(
121 expect(textArea.value).toEqual('{"A":"A"}');
123 let invalidJson: string;
124 component.noTypePolicyEditorComponent.validJson.subscribe((json: string) => {
128 textArea.setValue('{');
129 expect(invalidJson).toBeFalsy();
133 selector: `no-type-policy-editor-host-component`,
134 template: `<nrcp-no-type-policy-editor
135 [policyJson]="this.policyJson"
136 ></nrcp-no-type-policy-editor>`,
138 class TestNoTypePolicyEditorComponentHostComponent {
139 @ViewChild(NoTypePolicyEditorComponent)
140 noTypePolicyEditorComponent: NoTypePolicyEditorComponent;
141 policyJson: string = '{"A":"A"}';