Add test of typed policy editor component
[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: [
37         BrowserModule,
38         BrowserAnimationsModule,
39         MatIconModule
40       ],
41       declarations: [
42         TypedPolicyEditorComponent,
43         TestTypedPolicyEditorComponentHostComponent
44       ],
45       schemas: [
46         CUSTOM_ELEMENTS_SCHEMA
47       ]
48     })
49     .compileComponents();
50   });
51
52   beforeEach(() => {
53     fixture = TestBed.createComponent(TestTypedPolicyEditorComponentHostComponent);
54     component = fixture.componentInstance;
55     fixture.detectChanges();
56   });
57
58   it('should create', () => {
59     expect(component).toBeTruthy();
60   });
61
62   it('should have JSON form visible and JSON and JSON Schema not visible', () => {
63     let propertiesHeading = fixture.debugElement.nativeElement.querySelector('#propertiesHeading');
64     expect(propertiesHeading).toBeTruthy();
65     expect(propertiesHeading.innerText).toContain('Properties');
66     let propertiesIcon = fixture.debugElement.nativeElement.querySelector('#propertiesIcon');
67     expect(propertiesIcon).toBeTruthy();
68     expect(propertiesIcon.innerText).toEqual('expand_less');
69     let jsonForm = fixture.debugElement.nativeElement.querySelector('json-schema-form');
70     expect(jsonForm).toBeTruthy();
71
72     let jsonHeading = fixture.debugElement.nativeElement.querySelector('#jsonHeading');
73     expect(jsonHeading).toBeTruthy();
74     expect(jsonHeading.innerText).toContain('JSON')
75     let jsonIcon = fixture.debugElement.nativeElement.querySelector('#jsonIcon');
76     expect(jsonIcon).toBeTruthy();
77     expect(jsonIcon.innerText).toEqual('expand_more');
78     let jsonDiv = fixture.debugElement.nativeElement.querySelector('#jsonDiv');
79     expect(jsonDiv).toBeFalsy();
80
81     let schemaHeading = fixture.debugElement.nativeElement.querySelector('#schemaHeading');
82     expect(schemaHeading).toBeTruthy();
83     expect(schemaHeading.innerText).toContain('JSON Schema');
84     let schemaIcon = fixture.debugElement.nativeElement.querySelector('#schemaIcon');
85     expect(schemaIcon).toBeTruthy();
86     expect(schemaIcon.innerText).toEqual('expand_more');
87     let schemaDiv = fixture.debugElement.nativeElement.querySelector('#schemaDiv');
88     expect(schemaDiv).toBeFalsy();
89   });
90
91   it('should hide JSON form', () => {
92     let propertiesHeading = fixture.debugElement.nativeElement.querySelector('#propertiesHeading');
93     expect(propertiesHeading).toBeTruthy();
94     propertiesHeading.click();
95     fixture.detectChanges();
96
97     let propertiesIcon = fixture.debugElement.nativeElement.querySelector('#propertiesIcon');
98     expect(propertiesIcon).toBeTruthy();
99     expect(propertiesIcon.innerText).toEqual('expand_more');
100     let propertiesDiv = fixture.debugElement.nativeElement.querySelector('propertiesDiv');
101     expect(propertiesDiv).toBeFalsy();
102   });
103
104   it('should show JSON with text for dark mode', () => {
105     let jsonHeading = fixture.debugElement.nativeElement.querySelector('#jsonHeading');
106     expect(jsonHeading).toBeTruthy();
107     jsonHeading.click();
108     fixture.detectChanges();
109
110     let jsonIcon = fixture.debugElement.nativeElement.querySelector('#jsonIcon');
111     expect(jsonIcon).toBeTruthy();
112     expect(jsonIcon.innerText).toEqual('expand_less');
113     let jsonDiv = fixture.debugElement.nativeElement.querySelector('#jsonDiv');
114     expect(jsonDiv).toBeTruthy();
115     let jsonText = jsonDiv.querySelector('pre');
116     expect(jsonText.classList).toContain('text__dark');
117   });
118
119   it('should show JSON Schema with text for dark mode', () => {
120     let schemaHeading = fixture.debugElement.nativeElement.querySelector('#schemaHeading');
121     expect(schemaHeading).toBeTruthy();
122     schemaHeading.click();
123     fixture.detectChanges();
124
125     let schemaIcon = fixture.debugElement.nativeElement.querySelector('#schemaIcon');
126     expect(schemaIcon).toBeTruthy();
127     expect(schemaIcon.innerText).toEqual('expand_less');
128     let schemaDiv = fixture.debugElement.nativeElement.querySelector('#schemaDiv');
129     expect(schemaDiv).toBeTruthy();
130     let jsonSchemaText = schemaDiv.querySelector('pre');
131     expect(jsonSchemaText.classList).toContain('text__dark');
132   });
133
134   @Component({
135     selector: `typed-policy-editor-host-component`,
136     template: `<nrcp-typed-policy-editor [jsonObject]="policyJson" [jsonSchemaObject]="jsonSchemaObject" [darkMode]="true"></nrcp-typed-policy-editor>`
137   })
138   class TestTypedPolicyEditorComponentHostComponent {
139     policyJson: string = '{"A":"A"}';
140     jsonSchemaObject: string = 'policy_schema": { "$schema": "http://json-schema.org/draft-07/schema#", "description": "Type 1 policy type", "additionalProperties": false, "title": "1", "type": "object", "properties": { "A": "string" }, "required": [ "A" ]}';
141   }
142 });