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";
31 } from "@angular/core/testing";
32 import { FormsModule, ReactiveFormsModule } from "@angular/forms";
33 import { MatFormFieldModule } from "@angular/material/form-field";
34 import { MatInputModule } from "@angular/material/input";
35 import { MatInputHarness } from "@angular/material/input/testing";
36 import { MatPaginatorModule } from "@angular/material/paginator";
37 import { MatSortModule } from "@angular/material/sort";
38 import { MatSortHarness } from "@angular/material/sort/testing";
39 import { MatPaginatorHarness } from "@angular/material/paginator/testing";
40 import { MatTableModule } from "@angular/material/table";
41 import { MatTableHarness } from "@angular/material/table/testing";
42 import { BrowserAnimationsModule } from "@angular/platform-browser/animations";
43 import { of } from "rxjs/observable/of";
44 import { ConsumerStatus, JobInfo } from "@interfaces/consumer.types";
45 import { UiService } from "@services/ui/ui.service";
47 import { Job, JobsListComponent } from "./jobs-list.component";
48 import { Subscription } from "rxjs";
49 import { ConsumerService } from "@services/ei/consumer.service";
51 let component: JobsListComponent;
52 let fixture: ComponentFixture<JobsListComponent>;
55 info_type_id: "type1",
57 job_result_uri: "http://one",
62 info_job_status: "ENABLED",
63 producers: ["producer1"],
70 targetUri: "http://one",
71 prodIds: ["producer1"],
77 targetUri: "http://one",
78 prodIds: ["producer1"],
81 describe("JobsListComponent", () => {
82 let loader: HarnessLoader;
84 beforeEach(async(() => {
85 const spy = jasmine.createSpyObj("EIService", [
91 TestBed.configureTestingModule({
98 BrowserAnimationsModule,
102 schemas: [CUSTOM_ELEMENTS_SCHEMA],
103 declarations: [JobsListComponent],
104 providers: [{ provide: ConsumerService, useValue: spy }, UiService],
108 fixture = TestBed.createComponent(JobsListComponent);
109 component = fixture.componentInstance;
110 loader = TestbedHarnessEnvironment.loader(fixture);
114 const expectedJob1Row = {
116 prodIds: "producer1",
119 targetUri: "http://one",
122 it("should create", () => {
123 expect(component).toBeTruthy();
126 describe("#content", () => {
127 it("should loadJobs", fakeAsync(() => {
129 const newSub: Subscription = component.dataSubscription();
132 const actualJobs: Job[] = component.jobs();
133 expect(actualJobs.length).toEqual(2);
134 expect(actualJobs).toEqual([job1, job2]);
135 newSub.unsubscribe();
138 it("should contain job table with correct columns", fakeAsync(() => {
140 component.ngOnInit();
144 .getHarness(MatTableHarness.with({ selector: "#jobsTable" }))
145 .then((loadTable) => {
146 loadTable.getHeaderRows().then((headerRow) => {
147 headerRow[0].getCellTextByColumnName().then((header) => {
148 expect(header).toEqual({
150 prodIds: "Producers",
153 targetUri: "Target URI",
159 discardPeriodicTasks();
162 it("should set correct dark mode from UIService", fakeAsync(() => {
164 component.ngOnInit();
167 const uiService: UiService = TestBed.inject(UiService);
168 expect(component.darkMode).toBeTruthy();
170 uiService.darkModeState.next(false);
171 fixture.detectChanges();
172 expect(component.darkMode).toBeFalsy();
173 discardPeriodicTasks();
177 describe("#jobsTable", () => {
178 it("should contain data after initialization", fakeAsync(() => {
180 component.ngOnInit();
183 const expectedJobRows = [
187 prodIds: "producer1",
190 targetUri: "http://one",
195 .getHarness(MatTableHarness.with({ selector: "#jobsTable" }))
196 .then((loadTable) => {
197 loadTable.getRows().then((jobRows) => {
198 expect(jobRows.length).toEqual(2);
199 jobRows.forEach((row) => {
200 row.getCellTextByColumnName().then((values) => {
201 expect(expectedJobRows).toContain(
202 jasmine.objectContaining(values)
208 discardPeriodicTasks();
211 it("should display default values for non required properties ", fakeAsync(() => {
212 const jobMissingProperties = {
214 jobparam2: "value2_job2",
215 jobparam3: "value3_job2",
216 jobparam1: "value1_job2",
218 job_result_uri: "http://one",
220 const jobMissingPropertiesStatus = {
221 info_job_status: "ENABLED",
222 producers: ["producer1"],
225 let consumerServiceSpy = TestBed.inject(
227 ) as jasmine.SpyObj<ConsumerService>;
228 consumerServiceSpy.getJobIds.and.returnValue(of(["job1"]));
229 consumerServiceSpy.getJobInfo.and.returnValue(of(jobMissingProperties));
230 consumerServiceSpy.getConsumerStatus.and.returnValue(
231 of(jobMissingPropertiesStatus)
234 component.ngOnInit();
236 const expectedJobRow = {
238 prodIds: "producer1",
239 typeId: "< No type >",
240 owner: "< No owner >",
241 targetUri: "http://one",
245 .getHarness(MatTableHarness.with({ selector: "#jobsTable" }))
246 .then((loadTable) => {
247 loadTable.getRows().then((jobRows) => {
248 jobRows[0].getCellTextByColumnName().then((value) => {
249 expect(expectedJobRow).toEqual(jasmine.objectContaining(value));
253 discardPeriodicTasks();
256 it("filtering", fakeAsync(() => {
258 component.ngOnInit();
262 .getHarness(MatTableHarness.with({ selector: "#jobsTable" }))
263 .then((loadTable) => {
265 .getHarness(MatInputHarness.with({ selector: "#jobIdFilter" }))
266 .then((idFilter) => {
268 idFilter.setValue("1").then((_) => {
270 loadTable.getRows().then((jobRows) => {
271 expect(jobRows.length).toEqual(1);
272 jobRows[0].getCellTextByColumnName().then((value) => {
273 expect(expectedJob1Row).toEqual(
274 jasmine.objectContaining(value)
276 idFilter.setValue("");
284 .getHarness(MatInputHarness.with({ selector: "#jobTypeIdFilter" }))
285 .then((typeIdFilter) => {
287 typeIdFilter.setValue("1").then((_) => {
288 loadTable.getRows().then((jobRows) => {
289 expect(jobRows.length).toEqual(2);
290 jobRows[0].getCellTextByColumnName().then((value) => {
291 expect(expectedJob1Row).toEqual(
292 jasmine.objectContaining(value)
294 typeIdFilter.setValue("");
302 .getHarness(MatInputHarness.with({ selector: "#jobOwnerFilter" }))
303 .then((ownerFilter) => {
305 ownerFilter.setValue("1").then((_) => {
306 loadTable.getRows().then((jobRows) => {
307 expect(jobRows.length).toEqual(2);
308 jobRows[0].getCellTextByColumnName().then((value) => {
309 expect(expectedJob1Row).toEqual(
310 jasmine.objectContaining(value)
312 ownerFilter.setValue("");
321 MatInputHarness.with({ selector: "#jobTargetUriFilter" })
323 .then((targetUriFilter) => {
325 targetUriFilter.setValue("one").then((_) => {
326 loadTable.getRows().then((jobRows) => {
327 expect(jobRows.length).toEqual(2);
328 jobRows[0].getCellTextByColumnName().then((value) => {
329 expect(expectedJob1Row).toEqual(
330 jasmine.objectContaining(value)
332 targetUriFilter.setValue("");
339 discardPeriodicTasks();
342 describe("#sorting", () => {
343 it("should verify sort functionality on the table", fakeAsync(() => {
347 let sort = loader.getHarness(MatSortHarness);
350 sort.then((value) => {
351 value.getSortHeaders({ sortDirection: "" }).then((headers) => {
352 expect(headers.length).toBe(5);
356 headers[0].isActive().then((value) => {
357 expect(value).toBe(true);
359 headers[0].getSortDirection().then((value) => {
360 expect(value).toBe("asc");
365 headers[0].getSortDirection().then((value) => {
366 expect(value).toBe("desc");
370 discardPeriodicTasks();
373 it("should sort table asc and desc by first header", fakeAsync(() => {
377 let sort = loader.getHarness(MatSortHarness);
380 sort.then((value) => {
382 .getHarness(MatTableHarness.with({ selector: "#jobsTable" }))
383 .then((loadTable) => {
385 value.getSortHeaders().then((headers) => {
388 headers[0].getSortDirection().then((direction) => {
389 expect(direction).toBe("asc");
391 loadTable.getRows().then((jobRows) => {
392 jobRows[0].getCellTextByColumnName().then((value) => {
393 expect(expectedJob1Row).toEqual(
394 jasmine.objectContaining(value)
401 headers[0].getSortDirection().then((direction) => {
402 expect(direction).toBe("desc");
404 loadTable.getRows().then((jobRows) => {
405 jobRows[jobRows.length - 1]
406 .getCellTextByColumnName()
408 expect(expectedJob1Row).toEqual(
409 jasmine.objectContaining(value)
416 discardPeriodicTasks();
419 it("should not sort when clicking on filtering box", fakeAsync(() => {
420 const expectedJobRow = {
422 prodIds: "producer1",
425 targetUri: "http://one",
429 component.ngOnInit();
433 .getHarness(MatTableHarness.with({ selector: "#jobsTable" }))
434 .then((loadTable) => {
436 .getHarness(MatInputHarness.with({ selector: "#jobIdFilter" }))
437 .then((idFilter) => {
439 idFilter.setValue("").then((_) => {
440 loadTable.getRows().then((jobRows) => {
441 expect(jobRows.length).toEqual(2);
442 jobRows[1].getCellTextByColumnName().then((value) => {
443 expect(expectedJobRow).toEqual(
444 jasmine.objectContaining(value)
452 MatInputHarness.with({ selector: "#jobTypeIdFilter" })
454 .then((typeIdFilter) => {
456 typeIdFilter.setValue("").then((_) => {
457 loadTable.getRows().then((jobRows) => {
458 expect(jobRows.length).toEqual(2);
459 jobRows[1].getCellTextByColumnName().then((value) => {
460 expect(expectedJobRow).toEqual(
461 jasmine.objectContaining(value)
468 .getHarness(MatInputHarness.with({ selector: "#jobOwnerFilter" }))
469 .then((ownerFilter) => {
471 ownerFilter.setValue("").then((_) => {
472 loadTable.getRows().then((jobRows) => {
473 expect(jobRows.length).toEqual(2);
474 jobRows[1].getCellTextByColumnName().then((value) => {
475 expect(expectedJobRow).toEqual(
476 jasmine.objectContaining(value)
484 MatInputHarness.with({ selector: "#jobTargetUriFilter" })
486 .then((targetUriFilter) => {
488 targetUriFilter.setValue("").then((_) => {
489 loadTable.getRows().then((jobRows) => {
490 expect(jobRows.length).toEqual(2);
491 jobRows[1].getCellTextByColumnName().then((value) => {
492 expect(expectedJobRow).toEqual(
493 jasmine.objectContaining(value)
500 discardPeriodicTasks();
504 describe("#paging", () => {
505 it("should work properly on the table", fakeAsync(() => {
506 let consumerServiceSpy = TestBed.inject(
508 ) as jasmine.SpyObj<ConsumerService>;
509 consumerServiceSpy.getJobIds.and.returnValue(
510 of(["job1", "job2", "job3", "job4", "job5", "job6", "job7"])
512 consumerServiceSpy.getJobInfo.and.returnValue(of(jobInfo1));
513 consumerServiceSpy.getConsumerStatus.and.returnValue(of(jobStatus1));
514 component.ngOnInit();
517 loader.getHarness(MatPaginatorHarness).then((paging) => {
518 paging.setPageSize(5);
521 .getHarness(MatTableHarness.with({ selector: "#jobsTable" }))
522 .then((loadTable) => {
523 loadTable.getRows().then((jobRows) => {
524 expect(jobRows.length).toEqual(5);
527 paging.goToNextPage();
530 loadTable.getRows().then((jobRows) => {
531 expect(jobRows.length).toEqual(2);
532 jobRows[0].getCellTextByColumnName().then((value) => {
533 const expectedRow = {
535 prodIds: "producer1",
538 targetUri: "http://one",
540 discardPeriodicTasks();
541 expect(expectedRow).toEqual(jasmine.objectContaining(value));
551 function setServiceSpy() {
552 let consumerServiceSpy = TestBed.inject(
554 ) as jasmine.SpyObj<ConsumerService>;
555 consumerServiceSpy.getJobIds.and.returnValue(of(["job1", "job2"]));
556 consumerServiceSpy.getJobInfo.and.returnValue(of(jobInfo1));
557 consumerServiceSpy.getConsumerStatus.and.returnValue(of(jobStatus1));