Merge "First version of policy editor components"
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / ei-coordinator / jobs-list / jobs-list.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 import { HarnessLoader } from '@angular/cdk/testing';
21 import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
22 import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
23 import { async, ComponentFixture, TestBed } from '@angular/core/testing';
24 import { ReactiveFormsModule, FormBuilder } from '@angular/forms';
25 import { MatInputHarness } from '@angular/material/input/testing';
26 import { MatTableModule } from '@angular/material/table';
27 import { MatTableHarness } from '@angular/material/table/testing';
28 import { of } from 'rxjs/observable/of';
29 import { EIJob } from 'src/app/interfaces/ei.types';
30 import { UiService } from 'src/app/services/ui/ui.service';
31 import { EIJobDataSource } from '../ei-job.datasource';
32
33 import { JobsListComponent } from './jobs-list.component';
34
35 describe('JobsListComponent', () => {
36   let component: JobsListComponent;
37   let fixture: ComponentFixture<JobsListComponent>;
38   let loader: HarnessLoader;
39   let eiJobComponent: jasmine.SpyObj<JobsListComponent>;
40   let jobDataSourceSpy: jasmine.SpyObj<EIJobDataSource>;
41
42   const job1 = {
43     ei_job_identity: 'job1',
44     ei_type_identity: 'type1',
45     owner: 'owner1',
46     target_uri: 'http://one'
47   } as EIJob;
48   const job2 = {
49     ei_job_identity: 'job2',
50     ei_type_identity: 'type2',
51     owner: 'owner2',
52     target_uri: 'http://two'
53   } as EIJob;
54
55   beforeEach(async(() => {
56     eiJobComponent = jasmine.createSpyObj('producersListSpy', ['refresh']);
57     jobDataSourceSpy = jasmine.createSpyObj('EIJobDataSource', ['loadJobs', 'eiJobs', 'eiJobsSubject']);
58
59     jobDataSourceSpy.eiJobsSubject.and.returnValue(of([job1, job2]));
60
61     TestBed.configureTestingModule({
62       imports: [
63         MatTableModule,
64         ReactiveFormsModule
65       ],
66       schemas: [
67         CUSTOM_ELEMENTS_SCHEMA
68       ],
69       declarations: [JobsListComponent],
70       providers: [
71         { provide: EIJobDataSource, useValue: jobDataSourceSpy },
72         UiService,
73         FormBuilder,
74       ]
75     })
76       .compileComponents();
77   }));
78
79   const expectedJob1Row = { id: 'job1', typeId: 'type1', owner: 'owner1', targetUri: 'http://one' };
80
81   beforeEach(() => {
82     fixture = TestBed.createComponent(JobsListComponent);
83     component = fixture.componentInstance;
84     fixture.detectChanges();
85     loader = TestbedHarnessEnvironment.loader(fixture);
86   });
87
88   it('should create', () => {
89     expect(component).toBeTruthy();
90   });
91
92   it('should contain job table with correct columns', async () => {
93     let producersTable = await loader.getHarness(MatTableHarness.with({ selector: '#jobsTable' }));
94     let headerRow = (await producersTable.getHeaderRows())[0];
95     let headers = await headerRow.getCellTextByColumnName();
96
97     expect(headers).toEqual({ id: 'Job ID', typeId: 'Type ID', owner: 'Owner', targetUri: 'Target URI' });
98   });
99
100   it('should contain data after initialization', async () => {
101     component.ngOnInit();
102     const expectedJobRows = [
103       expectedJob1Row,
104       { id: 'job2', typeId: 'type2', owner: 'owner2', targetUri: 'http://two' }
105     ];
106     let jobsTable = await loader.getHarness(MatTableHarness.with({ selector: '#jobsTable' }));
107     let jobRows = await jobsTable.getRows();
108     expect(jobRows.length).toEqual(2);
109     jobRows.forEach(row => {
110       row.getCellTextByColumnName().then(values => {
111         expect(expectedJobRows).toContain(jasmine.objectContaining(values));
112       });
113     });
114   });
115
116   it('should display default values for non required properties ', async () => {
117     const jobMissingProperties = {
118       ei_job_identity: 'job1',
119       target_uri: 'http://one'
120     } as EIJob;
121     const jobs: EIJob[] = [jobMissingProperties];
122     jobDataSourceSpy.eiJobsSubject.and.returnValue(of(jobs));
123     component.ngOnInit();
124
125     const expectedJobRow = { id: 'job1', typeId: '< No type >', owner: '< No owner >', targetUri: 'http://one' };
126     let jobsTable = await loader.getHarness(MatTableHarness.with({ selector: '#jobsTable' }));
127     let jobRows = await jobsTable.getRows();
128     expect(await jobRows[0].getCellTextByColumnName()).toEqual(expectedJobRow);
129   });
130
131   it('filtering', async () => {
132     component.ngOnInit();
133     let jobsTable = await loader.getHarness(MatTableHarness.with({ selector: '#jobsTable' }));
134
135     let idFilterInput = await loader.getHarness(MatInputHarness.with({ selector: '#jobIdFilter' }));
136     await idFilterInput.setValue("1");
137     let jobRows = await jobsTable.getRows();
138     expect(jobRows.length).toEqual(1);
139     expect(await jobRows[0].getCellTextByColumnName()).toEqual(expectedJob1Row);
140
141     idFilterInput.setValue('');
142     let typeIdFilterInput = await loader.getHarness(MatInputHarness.with({ selector: '#jobTypeIdFilter' }));
143     await typeIdFilterInput.setValue("1");
144     jobRows = await jobsTable.getRows();
145     expect(jobRows.length).toEqual(1);
146
147     typeIdFilterInput.setValue('');
148     let ownerFilterInput = await loader.getHarness(MatInputHarness.with({ selector: '#jobOwnerFilter' }));
149     await ownerFilterInput.setValue("1");
150     jobRows = await jobsTable.getRows();
151     expect(jobRows.length).toEqual(1);
152     expect(await jobRows[0].getCellTextByColumnName()).toEqual(expectedJob1Row);
153
154     ownerFilterInput.setValue('');
155     let targetUriFilterInput = await loader.getHarness(MatInputHarness.with({ selector: '#jobTargetUriFilter' }));
156     await targetUriFilterInput.setValue("one");
157     jobRows = await jobsTable.getRows();
158     expect(jobRows.length).toEqual(1);
159     expect(await jobRows[0].getCellTextByColumnName()).toEqual(expectedJob1Row);
160   });
161 });