Fix PolicyInstanceDialogComponent
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / policy / typed-policy-editor / typed-policy-editor.component.spec.ts
1 // -
2 //   ========================LICENSE_START=================================
3 //   O-RAN-SC
4 //   %%
5 //   Copyright (C) 2021: Nordix Foundation
6 //   %%
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
10 //
11 //        http://www.apache.org/licenses/LICENSE-2.0
12 //
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===================================
19 //
20
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";
27
28 import { TypedPolicyEditorComponent } from "./typed-policy-editor.component";
29
30 describe("TypedPolicyEditorComponent", () => {
31   let component: TestTypedPolicyEditorComponentHostComponent;
32   let fixture: ComponentFixture<TestTypedPolicyEditorComponentHostComponent>;
33
34   beforeEach(async () => {
35     TestBed.configureTestingModule({
36       imports: [BrowserModule, BrowserAnimationsModule, MatIconModule],
37       declarations: [
38         TypedPolicyEditorComponent,
39         TestTypedPolicyEditorComponentHostComponent,
40       ],
41       schemas: [CUSTOM_ELEMENTS_SCHEMA],
42     }).compileComponents();
43   });
44
45   beforeEach(() => {
46     fixture = TestBed.createComponent(
47       TestTypedPolicyEditorComponentHostComponent
48     );
49     component = fixture.componentInstance;
50     fixture.detectChanges();
51   });
52
53   it("should create", () => {
54     expect(component).toBeTruthy();
55   });
56
57   it("should have JSON form visible and JSON and JSON Schema not visible", () => {
58     let propertiesHeading = fixture.debugElement.nativeElement.querySelector(
59       "#propertiesHeading"
60     );
61     expect(propertiesHeading).toBeTruthy();
62     expect(propertiesHeading.innerText).toContain("Properties");
63     let propertiesIcon = fixture.debugElement.nativeElement.querySelector(
64       "#propertiesIcon"
65     );
66     expect(propertiesIcon).toBeTruthy();
67     expect(propertiesIcon.innerText).toEqual("expand_less");
68     let jsonForm = fixture.debugElement.nativeElement.querySelector(
69       "json-schema-form"
70     );
71     expect(jsonForm).toBeTruthy();
72
73     let jsonHeading = fixture.debugElement.nativeElement.querySelector(
74       "#jsonHeading"
75     );
76     expect(jsonHeading).toBeTruthy();
77     expect(jsonHeading.innerText).toContain("JSON");
78     let jsonIcon = fixture.debugElement.nativeElement.querySelector(
79       "#jsonIcon"
80     );
81     expect(jsonIcon).toBeTruthy();
82     expect(jsonIcon.innerText).toEqual("expand_more");
83     let jsonDiv = fixture.debugElement.nativeElement.querySelector("#jsonDiv");
84     expect(jsonDiv).toBeFalsy();
85
86     let schemaHeading = fixture.debugElement.nativeElement.querySelector(
87       "#schemaHeading"
88     );
89     expect(schemaHeading).toBeTruthy();
90     expect(schemaHeading.innerText).toContain("JSON Schema");
91     let schemaIcon = fixture.debugElement.nativeElement.querySelector(
92       "#schemaIcon"
93     );
94     expect(schemaIcon).toBeTruthy();
95     expect(schemaIcon.innerText).toEqual("expand_more");
96     let schemaDiv = fixture.debugElement.nativeElement.querySelector(
97       "#schemaDiv"
98     );
99     expect(schemaDiv).toBeFalsy();
100   });
101
102   it("should hide JSON form", () => {
103     let propertiesHeading = fixture.debugElement.nativeElement.querySelector(
104       "#propertiesHeading"
105     );
106     expect(propertiesHeading).toBeTruthy();
107     propertiesHeading.click();
108     fixture.detectChanges();
109
110     let propertiesIcon = fixture.debugElement.nativeElement.querySelector(
111       "#propertiesIcon"
112     );
113     expect(propertiesIcon).toBeTruthy();
114     expect(propertiesIcon.innerText).toEqual("expand_more");
115     let propertiesDiv = fixture.debugElement.nativeElement.querySelector(
116       "propertiesDiv"
117     );
118     expect(propertiesDiv).toBeFalsy();
119   });
120
121   it("should show JSON with text for dark mode", () => {
122     let jsonHeading = fixture.debugElement.nativeElement.querySelector(
123       "#jsonHeading"
124     );
125     expect(jsonHeading).toBeTruthy();
126     jsonHeading.click();
127     fixture.detectChanges();
128
129     let jsonIcon = fixture.debugElement.nativeElement.querySelector(
130       "#jsonIcon"
131     );
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");
138   });
139
140   it("should show JSON Schema with text for dark mode", () => {
141     let schemaHeading = fixture.debugElement.nativeElement.querySelector(
142       "#schemaHeading"
143     );
144     expect(schemaHeading).toBeTruthy();
145     schemaHeading.click();
146     fixture.detectChanges();
147
148     let schemaIcon = fixture.debugElement.nativeElement.querySelector(
149       "#schemaIcon"
150     );
151     expect(schemaIcon).toBeTruthy();
152     expect(schemaIcon.innerText).toEqual("expand_less");
153     let schemaDiv = fixture.debugElement.nativeElement.querySelector(
154       "#schemaDiv"
155     );
156     expect(schemaDiv).toBeTruthy();
157     let jsonSchemaText = schemaDiv.querySelector("pre");
158     expect(jsonSchemaText.classList).toContain("text__dark");
159   });
160
161   @Component({
162     selector: `typed-policy-editor-host-component`,
163     template: `<nrcp-typed-policy-editor
164       [jsonObject]="policyJson"
165       [jsonSchemaObject]="jsonSchemaObject"
166       [darkMode]="true"
167     ></nrcp-typed-policy-editor>`,
168   })
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" ]}';
173   }
174 });