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 } 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 component: TestTypedPolicyEditorComponentHostComponent;
32 let fixture: ComponentFixture<TestTypedPolicyEditorComponentHostComponent>;
34 beforeEach(async () => {
35 TestBed.configureTestingModule({
36 imports: [BrowserModule, BrowserAnimationsModule, MatIconModule],
38 TypedPolicyEditorComponent,
39 TestTypedPolicyEditorComponentHostComponent,
41 schemas: [CUSTOM_ELEMENTS_SCHEMA],
42 }).compileComponents();
46 fixture = TestBed.createComponent(
47 TestTypedPolicyEditorComponentHostComponent
49 component = fixture.componentInstance;
50 fixture.detectChanges();
53 it("should create", () => {
54 expect(component).toBeTruthy();
57 it("should have JSON form visible and JSON and JSON Schema not visible", () => {
58 let propertiesHeading = fixture.debugElement.nativeElement.querySelector(
61 expect(propertiesHeading).toBeTruthy();
62 expect(propertiesHeading.innerText).toContain("Properties");
63 let propertiesIcon = fixture.debugElement.nativeElement.querySelector(
66 expect(propertiesIcon).toBeTruthy();
67 expect(propertiesIcon.innerText).toEqual("expand_less");
68 let jsonForm = fixture.debugElement.nativeElement.querySelector(
71 expect(jsonForm).toBeTruthy();
73 let jsonHeading = fixture.debugElement.nativeElement.querySelector(
76 expect(jsonHeading).toBeTruthy();
77 expect(jsonHeading.innerText).toContain("JSON");
78 let jsonIcon = fixture.debugElement.nativeElement.querySelector(
81 expect(jsonIcon).toBeTruthy();
82 expect(jsonIcon.innerText).toEqual("expand_more");
83 let jsonDiv = fixture.debugElement.nativeElement.querySelector("#jsonDiv");
84 expect(jsonDiv).toBeFalsy();
86 let schemaHeading = fixture.debugElement.nativeElement.querySelector(
89 expect(schemaHeading).toBeTruthy();
90 expect(schemaHeading.innerText).toContain("JSON Schema");
91 let schemaIcon = fixture.debugElement.nativeElement.querySelector(
94 expect(schemaIcon).toBeTruthy();
95 expect(schemaIcon.innerText).toEqual("expand_more");
96 let schemaDiv = fixture.debugElement.nativeElement.querySelector(
99 expect(schemaDiv).toBeFalsy();
102 it("should hide JSON form", () => {
103 let propertiesHeading = fixture.debugElement.nativeElement.querySelector(
106 expect(propertiesHeading).toBeTruthy();
107 propertiesHeading.click();
108 fixture.detectChanges();
110 let propertiesIcon = fixture.debugElement.nativeElement.querySelector(
113 expect(propertiesIcon).toBeTruthy();
114 expect(propertiesIcon.innerText).toEqual("expand_more");
115 let propertiesDiv = fixture.debugElement.nativeElement.querySelector(
118 expect(propertiesDiv).toBeFalsy();
121 it("should show JSON with text for dark mode", () => {
122 let jsonHeading = fixture.debugElement.nativeElement.querySelector(
125 expect(jsonHeading).toBeTruthy();
127 fixture.detectChanges();
129 let jsonIcon = fixture.debugElement.nativeElement.querySelector(
132 expect(jsonIcon).toBeTruthy();
133 expect(jsonIcon.innerText).toEqual("expand_less");
134 let jsonDiv = fixture.debugElement.nativeElement.querySelector("#jsonDiv");
135 expect(jsonDiv).toBeTruthy();
136 let jsonText = jsonDiv.querySelector("pre");
137 expect(jsonText.classList).toContain("text__dark");
140 it("should show JSON Schema with text for dark mode", () => {
141 let schemaHeading = fixture.debugElement.nativeElement.querySelector(
144 expect(schemaHeading).toBeTruthy();
145 schemaHeading.click();
146 fixture.detectChanges();
148 let schemaIcon = fixture.debugElement.nativeElement.querySelector(
151 expect(schemaIcon).toBeTruthy();
152 expect(schemaIcon.innerText).toEqual("expand_less");
153 let schemaDiv = fixture.debugElement.nativeElement.querySelector(
156 expect(schemaDiv).toBeTruthy();
157 let jsonSchemaText = schemaDiv.querySelector("pre");
158 expect(jsonSchemaText.classList).toContain("text__dark");
162 selector: `typed-policy-editor-host-component`,
163 template: `<nrcp-typed-policy-editor
164 [jsonObject]="policyJson"
165 [jsonSchemaObject]="jsonSchemaObject"
167 ></nrcp-typed-policy-editor>`,
169 class TestTypedPolicyEditorComponentHostComponent {
170 policyJson: string = '{"A":"A"}';
171 jsonSchemaObject: string =
172 'policy_schema": { "description": "Type 1 policy type", "title": "1", "type": "object", "properties": { "A": "string" }, "required": [ "A" ]}';