First version of policy editor components
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / policy / no-type-policy-editor / no-type-policy-editor.component.spec.ts
1 import { HarnessLoader } from '@angular/cdk/testing';
2 import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
3 import { Component, ViewChild } from '@angular/core';
4 import { async, ComponentFixture, TestBed } from '@angular/core/testing';
5 import { FormBuilder, FormGroup } from '@angular/forms';
6 import { MatButtonModule } from '@angular/material/button';
7 import { MatButtonHarness } from '@angular/material/button/testing';
8 import { MatInput, MatInputModule } from '@angular/material/input';
9 import { MatInputHarness } from '@angular/material/input/testing';
10 import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
11
12 import { NoTypePolicyEditorComponent } from './no-type-policy-editor.component';
13
14 let formGroup: FormGroup = new FormGroup({});
15
16 describe('NoTypePolicyEditorComponent', () => {
17   let component: TestNoTypePolicyEditorComponentHostComponent;
18   let fixture: ComponentFixture<TestNoTypePolicyEditorComponentHostComponent>;
19   let loader: HarnessLoader;
20
21   beforeEach(async(() => {
22     TestBed.configureTestingModule({
23       imports: [
24         BrowserAnimationsModule,
25         MatButtonModule,
26         MatInputModule
27       ],
28       declarations: [
29         NoTypePolicyEditorComponent,
30         TestNoTypePolicyEditorComponentHostComponent
31       ],
32       providers: [
33         FormBuilder
34       ]
35     })
36     .compileComponents();
37
38     fixture = TestBed.createComponent(TestNoTypePolicyEditorComponentHostComponent);
39     component = fixture.componentInstance;
40     fixture.detectChanges();
41     loader = TestbedHarnessEnvironment.loader(fixture);
42   }));
43
44   it('should create', () => {
45     expect(component).toBeTruthy();
46   });
47
48   it('should be added to form group with required validator', async () => {
49     let textArea: MatInputHarness = await loader.getHarness(MatInputHarness.with({ selector: '#policyJsonTextArea' }));
50
51     expect(formGroup.get('policyJsonTextArea')).toBeTruthy();
52     expect(await textArea.isRequired()).toBeTruthy();
53   });
54
55   it('should contain provided policy json and enabled Format button', async () => {
56     let textArea: MatInputHarness = await loader.getHarness(MatInputHarness.with({ selector: '#policyJsonTextArea' }));
57     expect(await textArea.getValue()).toEqual('{"A":"A"}');
58
59     console.log('Validity:',formGroup.valid);
60     let formatButton: MatButtonHarness = await loader.getHarness(MatButtonHarness.with({ selector: '#formatButton' }));
61     expect(await formatButton.isDisabled()).toBeFalsy();
62   });
63
64   it('Format button should be disabled wen json not valid', async () => {
65     let textArea: MatInputHarness = await loader.getHarness(MatInputHarness.with({ selector: '#policyJsonTextArea' }));
66     await textArea.setValue('{');
67
68     let formatButton: MatButtonHarness = await loader.getHarness(MatButtonHarness.with({ selector: '#formatButton' }));
69     // expect(await formatButton.isDisabled()).toBeTruthy();
70   });
71 });
72
73 @Component({
74   selector: `no-type-policy-editor-host-component`,
75   template: `<nrcp-no-type-policy-editor [policyJson]="this.policyJson" [instanceForm]="instanceForm"></nrcp-no-type-policy-editor>`
76 })
77 export class TestNoTypePolicyEditorComponentHostComponent {
78   @ViewChild(NoTypePolicyEditorComponent)
79   private noTypePolicyEditorComponentHostComponent: NoTypePolicyEditorComponent;
80   instanceForm: FormGroup = formGroup;
81   policyJson: string = '{"A":"A"}';
82 }