Fix filtering in jobs and producers table
[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 { Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
23 import { HarnessLoader } from '@angular/cdk/testing';
24 import { MatButtonModule } from '@angular/material/button';
25 import { MatButtonHarness } from '@angular/material/button/testing';
26 import { MatIconModule } from '@angular/material/icon';
27 import { MatTableModule } from '@angular/material/table';
28 import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
29
30 import { EICoordinatorComponent } from './ei-coordinator.component';
31 import { UiService } from '../services/ui/ui.service';
32 import { ProducersListComponent } from './producers-list/producers-list.component';
33 import { JobsListComponent } from './jobs-list/jobs-list.component';
34
35 describe('EICoordinatorComponent', () => {
36   let component: EICoordinatorComponent;
37   let fixture: ComponentFixture<EICoordinatorComponent>;
38   let loader: HarnessLoader;
39
40   beforeEach(async () => {
41
42     await TestBed.configureTestingModule({
43       imports: [
44         MatButtonModule,
45         MatIconModule,
46         MatTableModule,
47         BrowserAnimationsModule
48       ],
49       schemas: [
50         CUSTOM_ELEMENTS_SCHEMA
51       ],
52       declarations: [
53         EICoordinatorComponent,
54         JobsListStubComponent,
55         ProducerListStubComponent,
56       ],
57       providers: [
58         UiService
59       ]
60     })
61       .compileComponents();
62
63     fixture = TestBed.createComponent(EICoordinatorComponent);
64     component = fixture.componentInstance;
65
66     fixture.detectChanges();
67     loader = TestbedHarnessEnvironment.loader(fixture);
68   });
69
70   it('should create', () => {
71     expect(component).toBeTruthy();
72   });
73
74   describe('#content', () => {
75     it('should contain refresh button with correct icon', async () => {
76       let refreshButton = await loader.getHarness(MatButtonHarness.with({ selector: '#refreshButton' }));
77       expect(refreshButton).toBeTruthy();
78       expect(await refreshButton.getText()).toEqual('refresh');
79     });
80
81     it('should contain producers table', async () => {
82       const producersTableComponent = fixture.debugElement.nativeElement.querySelector('nrcp-producers-list');
83       expect(producersTableComponent).toBeTruthy();
84     });
85
86     it('should contain jobs table', async () => {
87       const jobsComponent = fixture.debugElement.nativeElement.querySelector('nrcp-jobs-list');
88       expect(jobsComponent).toBeTruthy();
89     });
90
91     it('should set correct dark mode from UIService', () => {
92       const uiService: UiService = TestBed.inject(UiService);
93       expect(component.darkMode).toBeTruthy();
94
95       uiService.darkModeState.next(false);
96       fixture.detectChanges();
97       expect(component.darkMode).toBeFalsy();
98
99     });
100
101     it('should refresh tables', async () => {
102       let refreshButton = await loader.getHarness(MatButtonHarness.with({ selector: '#refreshButton' }));
103       spyOn(component.producersList, 'loadProducers');
104       spyOn(component.producersList, 'clearFilter');
105       spyOn(component.jobComponent, 'loadJobs');
106       spyOn(component.jobComponent, 'clearFilter');
107       await refreshButton.click();
108
109       expect(component.jobComponent.loadJobs).toHaveBeenCalled();
110       expect(component.jobComponent.clearFilter).toHaveBeenCalled();
111       expect(component.producersList.loadProducers).toHaveBeenCalled();
112       expect(component.producersList.clearFilter).toHaveBeenCalled();
113     });
114   });
115
116   @Component({
117     selector: 'nrcp-jobs-list',
118     template: '',
119     providers: [
120       {
121         provide: JobsListComponent,
122         useClass: JobsListStubComponent
123       }
124     ]
125   })
126   class JobsListStubComponent {
127     loadJobs() { }
128     clearFilter() { }
129   }
130
131   @Component({
132     selector: 'nrcp-producers-list',
133     template: '',
134     providers: [
135       {
136         provide: ProducersListComponent,
137         useClass: ProducerListStubComponent
138       }
139     ]
140   })
141   class ProducerListStubComponent {
142     loadProducers() { }
143     clearFilter() { }
144   }
145
146 });