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 { async, 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 { MatIconModule } from '@angular/material/icon';
26 import { MatInputHarness } from '@angular/material/input/testing'
27 import { MatTableModule } from '@angular/material/table';
28 import { MatTableHarness } from '@angular/material/table/testing';
29 import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
31 import { EICoordinatorComponent } from './ei-coordinator.component';
32 import { EIJobDataSource } from './ei-job.datasource';
33 import { EIProducerDataSource } from './ei-producer.datasource';
34 import { UiService } from '../services/ui/ui.service';
35 import { EIJob, EIProducer } from '../interfaces/ei.types';
37 describe('EICoordinatorComponent', () => {
38 let component: EICoordinatorComponent;
39 let fixture: ComponentFixture<EICoordinatorComponent>;
40 let loader: HarnessLoader;
41 let producerDataSourceSpy: jasmine.SpyObj<EIProducerDataSource>;
42 let jobDataSourceSpy: jasmine.SpyObj<EIJobDataSource>;
45 ei_producer_id: 'producer1',
46 ei_producer_types: [ 'type1', 'type2' ],
50 ei_producer_id: 'producer2',
51 ei_producer_types: [ 'type2', 'type3' ],
56 ei_job_identity: 'job1',
57 ei_type_identity: 'type1',
59 target_uri: 'http://one'
62 ei_job_identity: 'job2',
63 ei_type_identity: 'type2',
65 target_uri: 'http://two'
68 beforeEach(async () => {
69 producerDataSourceSpy = jasmine.createSpyObj('EIProducerDataSource', [ 'loadProducers', 'eiProducers' ]);
70 jobDataSourceSpy = jasmine.createSpyObj('EIJobDataSource', [ 'loadJobs', 'eiJobs' ]);
72 await TestBed.configureTestingModule({
76 BrowserAnimationsModule,
80 CUSTOM_ELEMENTS_SCHEMA
83 EICoordinatorComponent
86 { provide: EIJobDataSource, useValue: jobDataSourceSpy },
87 { provide: EIProducerDataSource, useValue: producerDataSourceSpy },
94 fixture = TestBed.createComponent(EICoordinatorComponent);
95 component = fixture.componentInstance;
96 fixture.detectChanges();
97 loader = TestbedHarnessEnvironment.loader(fixture);
100 it('should create', () => {
101 expect(component).toBeTruthy();
104 describe('#content', () => {
105 it('should contain refresh button with coorect icon', () => {
106 const button = fixture.debugElement.nativeElement.querySelector('#refreshButton');
107 expect(button).toBeTruthy();
108 expect(button.innerHTML).toContain('refresh');
111 it('should contain producers table with correct columns', async () => {
112 let producersTable = await loader.getHarness(MatTableHarness.with({selector: '#producersTable'}));
113 let headerRow = (await producersTable.getHeaderRows())[0];
114 let headers = await headerRow.getCellTextByColumnName();
116 expect(headers).toEqual({id: 'Producer ID', types: 'Producer types', status: 'Producer status'});
119 it('should contain jobs table with correct columns', async () => {
120 let producersTable = await loader.getHarness(MatTableHarness.with({selector: '#jobsTable'}));
121 let headerRow = (await producersTable.getHeaderRows())[0];
122 let headers = await headerRow.getCellTextByColumnName();
124 expect(headers).toEqual({id: 'Job ID', typeId: 'Type ID', owner: 'Owner', targetUri: 'Target URI'});
127 it('should set correct dark mode from UIService', () => {
128 const uiService: UiService = TestBed.inject(UiService);
129 expect(component.darkMode).toBeTruthy();
131 uiService.darkModeState.next(false);
132 fixture.detectChanges();
133 expect(component.darkMode).toBeFalsy();
138 describe('#producersTable', () => {
139 const expectedProducer1Row = { id: 'producer1', types: 'type1,type2', status: 'ENABLED' };
141 const producers: EIProducer[] =[ producer1, producer2 ];
142 producerDataSourceSpy.eiProducers.and.returnValue(producers);
145 it('should contain data after initialization', async () => {
146 component.ngOnInit();
147 const expectedProducerRows = [
148 expectedProducer1Row,
149 {id: 'producer2', types: 'type2,type3', status: 'DISABLED'}
151 let producersTable = await loader.getHarness(MatTableHarness.with({selector: '#producersTable'}));
152 let producerRows = await producersTable.getRows();
153 expect(producerRows.length).toEqual(2);
154 producerRows.forEach(row => {
155 row.getCellTextByColumnName().then(values => {
156 expect(expectedProducerRows).toContain(jasmine.objectContaining(values));
161 describe('should display default values for non required properties', () => {
162 it('producer defaults', async () => {
163 const producerMissingProperties = {
164 ei_producer_id: 'producer1'
166 const producers: EIProducer[] =[ producerMissingProperties ];
167 producerDataSourceSpy.eiProducers.and.returnValue(producers);
168 component.ngOnInit();
170 const expectedProducerRow = { id: 'producer1', types: '< No types >', status: '< No status >' };
171 let producersTable = await loader.getHarness(MatTableHarness.with({selector: '#producersTable'}));
172 let producerRows = await producersTable.getRows();
173 expect(await producerRows[0].getCellTextByColumnName()).toEqual(expectedProducerRow);
176 it('job defaults', async () => {
177 const jobMissingProperties = {
178 ei_job_identity: 'job1',
179 target_uri: 'http://one'
181 const jobs: EIJob[] =[ jobMissingProperties ];
182 jobDataSourceSpy.eiJobs.and.returnValue(jobs);
183 component.ngOnInit();
185 const expectedJobRow = { id: 'job1', typeId: '< No type >', owner: '< No owner >', targetUri: 'http://one' };
186 let jobsTable = await loader.getHarness(MatTableHarness.with({selector: '#jobsTable'}));
187 let jobRows = await jobsTable.getRows();
188 expect(await jobRows[0].getCellTextByColumnName()).toEqual(expectedJobRow);
192 it('filtering', async () => {
193 component.ngOnInit();
194 let producersTable = await loader.getHarness(MatTableHarness.with({selector: '#producersTable'}));
196 let idFilterInput = await loader.getHarness(MatInputHarness.with({selector: '#producerIdFilter'}));
197 await idFilterInput.setValue("1");
198 let producerRows = await producersTable.getRows();
199 expect(producerRows.length).toEqual(1);
200 expect(await producerRows[0].getCellTextByColumnName()).toEqual(expectedProducer1Row);
202 idFilterInput.setValue('');
203 let typesFilterInput = await loader.getHarness(MatInputHarness.with({selector: '#producerTypesFilter'}));
204 await typesFilterInput.setValue("1");
205 producerRows = await producersTable.getRows();
206 expect(producerRows.length).toEqual(1);
207 expect(await producerRows[0].getCellTextByColumnName()).toEqual(expectedProducer1Row);
208 await typesFilterInput.setValue("2");
209 producerRows = await producersTable.getRows();
210 expect(producerRows.length).toEqual(2);
212 typesFilterInput.setValue('');
213 let statusFilterInput = await loader.getHarness(MatInputHarness.with({selector: '#producerStatusFilter'}));
214 await statusFilterInput.setValue("enabled");
215 producerRows = await producersTable.getRows();
216 expect(producerRows.length).toEqual(1);
217 expect(await producerRows[0].getCellTextByColumnName()).toEqual(expectedProducer1Row);
221 describe('#jobsTable', () => {
222 const expectedJob1Row = { id: 'job1', typeId: 'type1', owner: 'owner1', targetUri: 'http://one' };
224 const jobs: EIJob[] =[ job1, job2 ];
225 jobDataSourceSpy.eiJobs.and.returnValue(jobs);
228 it('should contain data after initialization', async () => {
229 component.ngOnInit();
230 const expectedJobRows = [
232 { id: 'job2', typeId: 'type2', owner: 'owner2', targetUri: 'http://two' }
234 let jobsTable = await loader.getHarness(MatTableHarness.with({selector: '#jobsTable'}));
235 let jobRows = await jobsTable.getRows();
236 expect(jobRows.length).toEqual(2);
237 jobRows.forEach(row => {
238 row.getCellTextByColumnName().then(values => {
239 expect(expectedJobRows).toContain(jasmine.objectContaining(values));
244 it('filtering', async () => {
245 component.ngOnInit();
246 let jobsTable = await loader.getHarness(MatTableHarness.with({selector: '#jobsTable'}));
248 let idFilterInput = await loader.getHarness(MatInputHarness.with({selector: '#jobIdFilter'}));
249 await idFilterInput.setValue("1");
250 let jobRows = await jobsTable.getRows();
251 expect(jobRows.length).toEqual(1);
252 expect(await jobRows[0].getCellTextByColumnName()).toEqual(expectedJob1Row);
254 idFilterInput.setValue('');
255 let typeIdFilterInput = await loader.getHarness(MatInputHarness.with({selector: '#jobTypeIdFilter'}));
256 await typeIdFilterInput.setValue("1");
257 jobRows = await jobsTable.getRows();
258 expect(jobRows.length).toEqual(1);
260 typeIdFilterInput.setValue('');
261 let ownerFilterInput = await loader.getHarness(MatInputHarness.with({selector: '#jobOwnerFilter'}));
262 await ownerFilterInput.setValue("1");
263 jobRows = await jobsTable.getRows();
264 expect(jobRows.length).toEqual(1);
265 expect(await jobRows[0].getCellTextByColumnName()).toEqual(expectedJob1Row);
267 ownerFilterInput.setValue('');
268 let targetUriFilterInput = await loader.getHarness(MatInputHarness.with({selector: '#jobTargetUriFilter'}));
269 await targetUriFilterInput.setValue("one");
270 jobRows = await jobsTable.getRows();
271 expect(jobRows.length).toEqual(1);
272 expect(await jobRows[0].getCellTextByColumnName()).toEqual(expectedJob1Row);