2 * ========================LICENSE_START=================================
5 * Copyright (C) 2021 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 { 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';
33 import { JobsListComponent } from './jobs-list.component';
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>;
43 ei_job_identity: 'job1',
44 ei_type_identity: 'type1',
46 target_uri: 'http://one'
49 ei_job_identity: 'job2',
50 ei_type_identity: 'type2',
52 target_uri: 'http://two'
55 beforeEach(async(() => {
56 eiJobComponent = jasmine.createSpyObj('producersListSpy', ['refresh']);
57 jobDataSourceSpy = jasmine.createSpyObj('EIJobDataSource', ['loadJobs', 'eiJobs', 'eiJobsSubject']);
59 jobDataSourceSpy.eiJobsSubject.and.returnValue(of([job1, job2]));
61 TestBed.configureTestingModule({
67 CUSTOM_ELEMENTS_SCHEMA
69 declarations: [JobsListComponent],
71 { provide: EIJobDataSource, useValue: jobDataSourceSpy },
79 const expectedJob1Row = { id: 'job1', typeId: 'type1', owner: 'owner1', targetUri: 'http://one' };
82 fixture = TestBed.createComponent(JobsListComponent);
83 component = fixture.componentInstance;
84 fixture.detectChanges();
85 loader = TestbedHarnessEnvironment.loader(fixture);
88 it('should create', () => {
89 expect(component).toBeTruthy();
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();
97 expect(headers).toEqual({ id: 'Job ID', typeId: 'Type ID', owner: 'Owner', targetUri: 'Target URI' });
100 it('should contain data after initialization', async () => {
101 component.ngOnInit();
102 const expectedJobRows = [
104 { id: 'job2', typeId: 'type2', owner: 'owner2', targetUri: 'http://two' }
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));
116 it('should display default values for non required properties ', async () => {
117 const jobMissingProperties = {
118 ei_job_identity: 'job1',
119 target_uri: 'http://one'
121 const jobs: EIJob[] = [jobMissingProperties];
122 jobDataSourceSpy.eiJobsSubject.and.returnValue(of(jobs));
123 component.ngOnInit();
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);
131 it('filtering', async () => {
132 component.ngOnInit();
133 let jobsTable = await loader.getHarness(MatTableHarness.with({ selector: '#jobsTable' }));
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);
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);
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);
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);