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 { CUSTOM_ELEMENTS_SCHEMA } from "@angular/compiler";
22 import { Component } from "@angular/core";
23 import { ComponentFixture, TestBed } from "@angular/core/testing";
24 import { MatIconModule } from "@angular/material/icon";
25 import { BrowserModule, By } from "@angular/platform-browser";
26 import { BrowserAnimationsModule } from "@angular/platform-browser/animations";
28 import { TypedPolicyEditorComponent } from "./typed-policy-editor.component";
30 describe("TypedPolicyEditorComponent", () => {
31 let hostComponent: TestTypedPolicyEditorComponentHostComponent;
32 let componentUnderTest: TypedPolicyEditorComponent;
33 let hostFixture: ComponentFixture<TestTypedPolicyEditorComponentHostComponent>;
35 beforeEach(async () => {
36 TestBed.configureTestingModule({
37 imports: [BrowserModule, BrowserAnimationsModule, MatIconModule],
39 TypedPolicyEditorComponent,
40 TestTypedPolicyEditorComponentHostComponent,
42 schemas: [CUSTOM_ELEMENTS_SCHEMA],
43 }).compileComponents();
47 hostFixture = TestBed.createComponent(
48 TestTypedPolicyEditorComponentHostComponent
50 hostComponent = hostFixture.componentInstance;
51 componentUnderTest = hostFixture.debugElement.query(
52 By.directive(TypedPolicyEditorComponent)
54 hostFixture.detectChanges();
57 it("should create", () => {
58 expect(hostComponent).toBeTruthy();
61 it("should have JSON form visible and JSON and JSON Schema not visible", () => {
62 let propertiesHeading = hostFixture.debugElement.nativeElement.querySelector(
65 expect(propertiesHeading).toBeTruthy();
66 expect(propertiesHeading.innerText).toContain("Properties");
68 let propertiesIcon = hostFixture.debugElement.nativeElement.querySelector(
71 expect(propertiesIcon).toBeTruthy();
72 expect(propertiesIcon.innerText).toEqual("expand_less");
74 let jsonForm = hostFixture.debugElement.nativeElement.querySelector(
77 expect(jsonForm).toBeTruthy();
79 let jsonHeading = hostFixture.debugElement.nativeElement.querySelector(
82 expect(jsonHeading).toBeTruthy();
83 expect(jsonHeading.innerText).toContain("JSON");
85 let jsonIcon = hostFixture.debugElement.nativeElement.querySelector(
88 expect(jsonIcon).toBeTruthy();
89 expect(jsonIcon.innerText).toEqual("expand_more");
91 let jsonDiv = hostFixture.debugElement.nativeElement.querySelector(
94 expect(jsonDiv).toBeFalsy();
96 let schemaHeading = hostFixture.debugElement.nativeElement.querySelector(
99 expect(schemaHeading).toBeTruthy();
100 expect(schemaHeading.innerText).toContain("JSON Schema");
102 let schemaIcon = hostFixture.debugElement.nativeElement.querySelector(
105 expect(schemaIcon).toBeTruthy();
106 expect(schemaIcon.innerText).toEqual("expand_more");
108 let schemaDiv = hostFixture.debugElement.nativeElement.querySelector(
111 expect(schemaDiv).toBeFalsy();
114 it("should hide JSON form", () => {
115 let propertiesHeading = hostFixture.debugElement.nativeElement.querySelector(
118 expect(propertiesHeading).toBeTruthy();
119 propertiesHeading.click();
120 hostFixture.detectChanges();
122 let propertiesIcon = hostFixture.debugElement.nativeElement.querySelector(
125 expect(propertiesIcon).toBeTruthy();
126 expect(propertiesIcon.innerText).toEqual("expand_more");
128 let propertiesDiv = hostFixture.debugElement.nativeElement.querySelector(
131 expect(propertiesDiv).toBeFalsy();
134 it("should show JSON with text for dark mode and correct content", () => {
135 let jsonHeading = hostFixture.debugElement.nativeElement.querySelector(
138 expect(jsonHeading).toBeTruthy();
140 hostFixture.detectChanges();
142 let jsonIcon = hostFixture.debugElement.nativeElement.querySelector(
145 expect(jsonIcon).toBeTruthy();
146 expect(jsonIcon.innerText).toEqual("expand_less");
148 componentUnderTest.onChanges('{ "qosObjectives": "test" }');
149 hostFixture.detectChanges();
151 let jsonDiv = hostFixture.debugElement.nativeElement.querySelector(
154 expect(jsonDiv).toBeTruthy();
155 let jsonText = jsonDiv.querySelector("pre");
156 expect(jsonText.classList).toContain("text__dark");
157 expect(jsonText.innerText).toEqual('"{ \\"qosObjectives\\": \\"test\\" }"');
160 it("should present error info in JSON div", () => {
164 dataPath: "/scope/qosObjectives",
165 schemaPath: "#/properties/scope/qosObjectives/required",
166 params: { missingProperty: "priorityLevel" },
167 message: "should have required property 'priorityLevel'",
170 componentUnderTest.validationErrors(errors);
171 hostFixture.detectChanges();
172 componentUnderTest.prettyValidationErrors;
173 hostFixture.detectChanges();
176 let jsonHeading = hostFixture.debugElement.nativeElement.querySelector(
179 expect(jsonHeading).toBeTruthy();
181 hostFixture.detectChanges();
183 let jsonDiv = hostFixture.debugElement.nativeElement.querySelector(
186 expect(jsonDiv.innerText).toContain("Not valid — errors:");
187 expect(jsonDiv.innerText).toContain(
188 "scope.qosObjectives: should have required property 'priorityLevel'"
192 it("should show JSON Schema with text for dark mode and correct content", () => {
193 let schemaHeading = hostFixture.debugElement.nativeElement.querySelector(
196 expect(schemaHeading).toBeTruthy();
197 schemaHeading.click();
198 hostFixture.detectChanges();
200 let schemaIcon = hostFixture.debugElement.nativeElement.querySelector(
203 expect(schemaIcon).toBeTruthy();
204 expect(schemaIcon.innerText).toEqual("expand_less");
206 componentUnderTest.schemaAsString;
207 hostFixture.detectChanges();
208 let schemaDiv = hostFixture.debugElement.nativeElement.querySelector(
211 expect(schemaDiv).toBeTruthy();
213 let jsonSchemaText = schemaDiv.querySelector("pre");
214 expect(jsonSchemaText.classList).toContain("text__dark");
215 expect(jsonSchemaText.innerText).toContain('qosObjectives: {');
218 it("should send a valid json", () => {
219 let emittedValidJson: string;
220 componentUnderTest.validJson.subscribe((json: string) => {
221 emittedValidJson = json;
224 componentUnderTest.onChanges('{ "qosObjectives": "test" }');
225 hostFixture.detectChanges();
226 componentUnderTest.isValid(true);
228 expect(emittedValidJson).toEqual('"{ \\"qosObjectives\\": \\"test\\" }"');
231 it("should send null when invalid json", () => {
232 let emittedValidJson: string;
233 componentUnderTest.validJson.subscribe((json: string) => {
234 emittedValidJson = json;
237 componentUnderTest.isValid(false);
239 expect(emittedValidJson).toBeFalsy();
243 selector: `typed-policy-editor-host-component`,
244 template: `<nrcp-typed-policy-editor
245 [jsonObject]="policyJson"
246 [jsonSchemaObject]="jsonSchemaObject"
248 ></nrcp-typed-policy-editor>`,
250 class TestTypedPolicyEditorComponentHostComponent {
251 policyJson: string = "{ jsonSchemaObject: 'test' }";
252 jsonSchemaObject: string =
253 "{type: 'object',properties: {qosObjectives: {additionalProperties: false,type: 'object',properties: {priorityLevel: {type: 'number'}},required: ['priorityLevel']}},required: ['qosObjectives']}";