2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 Nordix Foundation
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
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';
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';
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>;
48 ei_job_identity: 'job1',
49 ei_type_identity: 'type1',
51 target_uri: 'http://one'
54 ei_job_identity: 'job2',
55 ei_type_identity: 'type2',
57 target_uri: 'http://two'
60 beforeEach(async () => {
61 producersListSpy = jasmine.createSpyObj('producersListSpy', ['refresh']);
62 jobDataSourceSpy = jasmine.createSpyObj('EIJobDataSource', [ 'loadJobs', 'eiJobs', 'eiJobsSubject' ]);
64 jobDataSourceSpy.eiJobsSubject.and.returnValue(of([ job1, job2 ]));
66 await TestBed.configureTestingModule({
71 BrowserAnimationsModule,
75 CUSTOM_ELEMENTS_SCHEMA
78 EICoordinatorComponent
81 { provide: ProducersListComponent, useValue: producersListSpy },
82 { provide: EIJobDataSource, useValue: jobDataSourceSpy },
89 fixture = TestBed.createComponent(EICoordinatorComponent);
90 component = fixture.componentInstance;
91 fixture.detectChanges();
92 loader = TestbedHarnessEnvironment.loader(fixture);
95 it('should create', () => {
96 expect(component).toBeTruthy();
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');
106 it('should contain producers table', async () => {
107 const producersTableComponent = fixture.debugElement.nativeElement.querySelector('nrcp-producers-list');
108 expect(producersTableComponent).toBeTruthy();
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();
116 expect(headers).toEqual({ id: 'Job ID', typeId: 'Type ID', owner: 'Owner', targetUri: 'Target URI' });
119 it('should set correct dark mode from UIService', () => {
120 const uiService: UiService = TestBed.inject(UiService);
121 expect(component.darkMode).toBeTruthy();
123 uiService.darkModeState.next(false);
124 fixture.detectChanges();
125 expect(component.darkMode).toBeFalsy();
129 it('should refresh tables', async () => {
130 let refreshButton = await loader.getHarness(MatButtonHarness.with({ selector: '#refreshButton' }));
131 await refreshButton.click();
133 expect(producersListSpy.refresh).toHaveBeenCalled();
137 describe('#jobsTable', () => {
138 const expectedJob1Row = { id: 'job1', typeId: 'type1', owner: 'owner1', targetUri: 'http://one' };
140 const jobs: EIJob[] = [ job1, job2 ];
141 jobDataSourceSpy.eiJobsSubject.and.returnValue(of(jobs));
144 it('should contain data after initialization', async () => {
145 component.ngOnInit();
146 const expectedJobRows = [
148 { id: 'job2', typeId: 'type2', owner: 'owner2', targetUri: 'http://two' }
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));
160 it('job defaults', async () => {
161 const jobMissingProperties = {
162 ei_job_identity: 'job1',
163 target_uri: 'http://one'
165 const jobs: EIJob[] = [jobMissingProperties];
166 jobDataSourceSpy.eiJobsSubject.and.returnValue(of(jobs));
167 component.ngOnInit();
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);
175 it('filtering', async () => {
176 component.ngOnInit();
177 let jobsTable = await loader.getHarness(MatTableHarness.with({ selector: '#jobsTable' }));
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);
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);
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);
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);