Create component for EI Producers list
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / ei-coordinator / ei-coordinator.component.spec.ts
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 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 { ComponentFixture, TestBed } from '@angular/core/testing';
21 import { BrowserAnimationsModule } from '@angular/platform-browser/animations'
22 import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
23 import { FormBuilder, ReactiveFormsModule } from '@angular/forms';
24 import { HarnessLoader } from '@angular/cdk/testing';
25 import { MatButtonModule } from '@angular/material/button';
26 import { MatButtonHarness } from '@angular/material/button/testing';
27 import { MatIconModule } from '@angular/material/icon';
28 import { MatInputHarness } from '@angular/material/input/testing'
29 import { MatTableModule } from '@angular/material/table';
30 import { MatTableHarness } from '@angular/material/table/testing';
31 import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
32 import { of } from 'rxjs';
33
34 import { EICoordinatorComponent } from './ei-coordinator.component';
35 import { EIJobDataSource } from './ei-job.datasource';
36 import { UiService } from '../services/ui/ui.service';
37 import { EIJob } from '../interfaces/ei.types';
38 import { ProducersListComponent } from './producers-list/producers-list.component';
39
40 describe('EICoordinatorComponent', () => {
41   let component: EICoordinatorComponent;
42   let fixture: ComponentFixture<EICoordinatorComponent>;
43   let loader: HarnessLoader;
44   let producersListSpy: jasmine.SpyObj<ProducersListComponent>;
45   let jobDataSourceSpy: jasmine.SpyObj<EIJobDataSource>;
46
47   const job1 = {
48     ei_job_identity: 'job1',
49     ei_type_identity: 'type1',
50     owner: 'owner1',
51     target_uri: 'http://one'
52   } as EIJob;
53   const job2 = {
54     ei_job_identity: 'job2',
55     ei_type_identity: 'type2',
56     owner: 'owner2',
57     target_uri: 'http://two'
58   } as EIJob;
59
60   beforeEach(async () => {
61     producersListSpy = jasmine.createSpyObj('producersListSpy', ['refresh']);
62     jobDataSourceSpy = jasmine.createSpyObj('EIJobDataSource', [ 'loadJobs', 'eiJobs', 'eiJobsSubject' ]);
63
64     jobDataSourceSpy.eiJobsSubject.and.returnValue(of([ job1, job2 ]));
65
66     await TestBed.configureTestingModule({
67       imports: [
68         MatButtonModule,
69         MatIconModule,
70         MatTableModule,
71         BrowserAnimationsModule,
72         ReactiveFormsModule
73       ],
74       schemas: [
75         CUSTOM_ELEMENTS_SCHEMA
76       ],
77       declarations: [
78         EICoordinatorComponent
79       ],
80       providers: [
81         { provide: ProducersListComponent, useValue: producersListSpy },
82         { provide: EIJobDataSource, useValue: jobDataSourceSpy },
83         UiService,
84         FormBuilder,
85       ]
86     })
87       .compileComponents();
88
89     fixture = TestBed.createComponent(EICoordinatorComponent);
90     component = fixture.componentInstance;
91     fixture.detectChanges();
92     loader = TestbedHarnessEnvironment.loader(fixture);
93   });
94
95   it('should create', () => {
96     expect(component).toBeTruthy();
97   });
98
99   describe('#content', () => {
100     it('should contain refresh button with coorect icon', async () => {
101       let refreshButton = await loader.getHarness(MatButtonHarness.with({ selector: '#refreshButton' }));
102       expect(refreshButton).toBeTruthy();
103       expect(await refreshButton.getText()).toEqual('refresh');
104     });
105
106     it('should contain producers table', async () => {
107       const producersTableComponent = fixture.debugElement.nativeElement.querySelector('nrcp-producers-list');
108       expect(producersTableComponent).toBeTruthy();
109     });
110
111     it('should contain jobs table with correct columns', async () => {
112       let producersTable = await loader.getHarness(MatTableHarness.with({ selector: '#jobsTable' }));
113       let headerRow = (await producersTable.getHeaderRows())[0];
114       let headers = await headerRow.getCellTextByColumnName();
115
116       expect(headers).toEqual({ id: 'Job ID', typeId: 'Type ID', owner: 'Owner', targetUri: 'Target URI' });
117     });
118
119     it('should set correct dark mode from UIService', () => {
120       const uiService: UiService = TestBed.inject(UiService);
121       expect(component.darkMode).toBeTruthy();
122
123       uiService.darkModeState.next(false);
124       fixture.detectChanges();
125       expect(component.darkMode).toBeFalsy();
126
127     });
128
129     it('should refresh tables', async () => {
130       let refreshButton = await loader.getHarness(MatButtonHarness.with({ selector: '#refreshButton' }));
131       await refreshButton.click();
132
133       expect(producersListSpy.refresh).toHaveBeenCalled();
134     });
135   });
136
137   describe('#jobsTable', () => {
138     const expectedJob1Row = { id: 'job1', typeId: 'type1', owner: 'owner1', targetUri: 'http://one' };
139     beforeEach(() => {
140       const jobs: EIJob[] = [ job1, job2 ];
141       jobDataSourceSpy.eiJobsSubject.and.returnValue(of(jobs));
142     });
143
144     it('should contain data after initialization', async () => {
145       component.ngOnInit();
146       const expectedJobRows = [
147         expectedJob1Row,
148         { id: 'job2', typeId: 'type2', owner: 'owner2', targetUri: 'http://two' }
149       ];
150       let jobsTable = await loader.getHarness(MatTableHarness.with({ selector: '#jobsTable' }));
151       let jobRows = await jobsTable.getRows();
152       expect(jobRows.length).toEqual(2);
153       jobRows.forEach(row => {
154         row.getCellTextByColumnName().then(values => {
155           expect(expectedJobRows).toContain(jasmine.objectContaining(values));
156         });
157       });
158     });
159
160     it('job defaults', async () => {
161       const jobMissingProperties = {
162         ei_job_identity: 'job1',
163         target_uri: 'http://one'
164       } as EIJob;
165       const jobs: EIJob[] = [jobMissingProperties];
166       jobDataSourceSpy.eiJobsSubject.and.returnValue(of(jobs));
167       component.ngOnInit();
168
169       const expectedJobRow = { id: 'job1', typeId: '< No type >', owner: '< No owner >', targetUri: 'http://one' };
170       let jobsTable = await loader.getHarness(MatTableHarness.with({ selector: '#jobsTable' }));
171       let jobRows = await jobsTable.getRows();
172       expect(await jobRows[0].getCellTextByColumnName()).toEqual(expectedJobRow);
173     });
174
175     it('filtering', async () => {
176       component.ngOnInit();
177       let jobsTable = await loader.getHarness(MatTableHarness.with({ selector: '#jobsTable' }));
178
179       let idFilterInput = await loader.getHarness(MatInputHarness.with({ selector: '#jobIdFilter' }));
180       await idFilterInput.setValue("1");
181       let jobRows = await jobsTable.getRows();
182       expect(jobRows.length).toEqual(1);
183       expect(await jobRows[0].getCellTextByColumnName()).toEqual(expectedJob1Row);
184
185       idFilterInput.setValue('');
186       let typeIdFilterInput = await loader.getHarness(MatInputHarness.with({ selector: '#jobTypeIdFilter' }));
187       await typeIdFilterInput.setValue("1");
188       jobRows = await jobsTable.getRows();
189       expect(jobRows.length).toEqual(1);
190
191       typeIdFilterInput.setValue('');
192       let ownerFilterInput = await loader.getHarness(MatInputHarness.with({ selector: '#jobOwnerFilter' }));
193       await ownerFilterInput.setValue("1");
194       jobRows = await jobsTable.getRows();
195       expect(jobRows.length).toEqual(1);
196       expect(await jobRows[0].getCellTextByColumnName()).toEqual(expectedJob1Row);
197
198       ownerFilterInput.setValue('');
199       let targetUriFilterInput = await loader.getHarness(MatInputHarness.with({ selector: '#jobTargetUriFilter' }));
200       await targetUriFilterInput.setValue("one");
201       jobRows = await jobsTable.getRows();
202       expect(jobRows.length).toEqual(1);
203       expect(await jobRows[0].getCellTextByColumnName()).toEqual(expectedJob1Row);
204     });
205   });
206 });