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"],
78 targetUri: "http://one",
79 prodIds: ["producer1"],
83 describe("JobsListComponent", () => {
84 let loader: HarnessLoader;
86 beforeEach(async(() => {
87 const spy = jasmine.createSpyObj("EIService", [
93 TestBed.configureTestingModule({
100 BrowserAnimationsModule,
104 schemas: [CUSTOM_ELEMENTS_SCHEMA],
105 declarations: [JobsListComponent],
106 providers: [{ provide: ConsumerService, useValue: spy }, UiService],
110 fixture = TestBed.createComponent(JobsListComponent);
111 component = fixture.componentInstance;
112 loader = TestbedHarnessEnvironment.loader(fixture);
116 const expectedJob1Row = {
118 prodIds: "producer1",
121 targetUri: "http://one",
125 it("should create", () => {
126 expect(component).toBeTruthy();
129 describe("#content", () => {
130 it("should loadJobs", fakeAsync(() => {
132 const newSub: Subscription = component.dataSubscription();
135 const actualJobs: Job[] = component.jobs();
136 expect(actualJobs.length).toEqual(2);
137 expect(actualJobs).toEqual([job1, job2]);
138 newSub.unsubscribe();
141 it("should contain job table with correct columns", fakeAsync(() => {
143 component.ngOnInit();
147 .getHarness(MatTableHarness.with({ selector: "#jobsTable" }))
148 .then((loadTable) => {
149 loadTable.getHeaderRows().then((headerRow) => {
150 headerRow[0].getCellTextByColumnName().then((header) => {
151 expect(header).toEqual({
153 prodIds: "Producers",
156 targetUri: "Target URI",
163 discardPeriodicTasks();
166 it("should set correct dark mode from UIService", fakeAsync(() => {
168 component.ngOnInit();
171 const uiService: UiService = TestBed.inject(UiService);
172 expect(component.darkMode).toBeTruthy();
174 uiService.darkModeState.next(false);
175 fixture.detectChanges();
176 expect(component.darkMode).toBeFalsy();
177 discardPeriodicTasks();
181 describe("#jobsTable", () => {
182 it("should contain data after initialization", fakeAsync(() => {
184 component.ngOnInit();
187 const expectedJobRows = [
191 prodIds: "producer1",
194 targetUri: "http://one",
200 .getHarness(MatTableHarness.with({ selector: "#jobsTable" }))
201 .then((loadTable) => {
202 loadTable.getRows().then((jobRows) => {
203 expect(jobRows.length).toEqual(2);
204 jobRows.forEach((row) => {
205 row.getCellTextByColumnName().then((values) => {
206 expect(expectedJobRows).toContain(
207 jasmine.objectContaining(values)
213 discardPeriodicTasks();
216 it("should display default values for non required properties ", fakeAsync(() => {
217 const jobMissingProperties = {
219 jobparam2: "value2_job2",
220 jobparam3: "value3_job2",
221 jobparam1: "value1_job2",
223 job_result_uri: "http://one",
225 const jobMissingPropertiesStatus = {
226 info_job_status: "ENABLED",
227 producers: ["producer1"],
230 let consumerServiceSpy = TestBed.inject(
232 ) as jasmine.SpyObj<ConsumerService>;
233 consumerServiceSpy.getJobIds.and.returnValue(of(["job1"]));
234 consumerServiceSpy.getJobInfo.and.returnValue(of(jobMissingProperties));
235 consumerServiceSpy.getConsumerStatus.and.returnValue(
236 of(jobMissingPropertiesStatus)
239 component.ngOnInit();
241 const expectedJobRow = {
243 prodIds: "producer1",
244 typeId: "< No type >",
245 owner: "< No owner >",
246 targetUri: "http://one",
251 .getHarness(MatTableHarness.with({ selector: "#jobsTable" }))
252 .then((loadTable) => {
253 loadTable.getRows().then((jobRows) => {
254 jobRows[0].getCellTextByColumnName().then((value) => {
255 expect(expectedJobRow).toEqual(jasmine.objectContaining(value));
259 discardPeriodicTasks();
262 it("filtering", fakeAsync(() => {
264 component.ngOnInit();
268 .getHarness(MatTableHarness.with({ selector: "#jobsTable" }))
269 .then((loadTable) => {
271 .getHarness(MatInputHarness.with({ selector: "#jobIdFilter" }))
272 .then((idFilter) => {
274 idFilter.setValue("1").then((_) => {
276 loadTable.getRows().then((jobRows) => {
277 expect(jobRows.length).toEqual(1);
278 jobRows[0].getCellTextByColumnName().then((value) => {
279 expect(expectedJob1Row).toEqual(
280 jasmine.objectContaining(value)
282 idFilter.setValue("");
290 .getHarness(MatInputHarness.with({ selector: "#jobTypeIdFilter" }))
291 .then((typeIdFilter) => {
293 typeIdFilter.setValue("1").then((_) => {
294 loadTable.getRows().then((jobRows) => {
295 expect(jobRows.length).toEqual(2);
296 jobRows[0].getCellTextByColumnName().then((value) => {
297 expect(expectedJob1Row).toEqual(
298 jasmine.objectContaining(value)
300 typeIdFilter.setValue("");
308 .getHarness(MatInputHarness.with({ selector: "#jobOwnerFilter" }))
309 .then((ownerFilter) => {
311 ownerFilter.setValue("1").then((_) => {
312 loadTable.getRows().then((jobRows) => {
313 expect(jobRows.length).toEqual(2);
314 jobRows[0].getCellTextByColumnName().then((value) => {
315 expect(expectedJob1Row).toEqual(
316 jasmine.objectContaining(value)
318 ownerFilter.setValue("");
327 MatInputHarness.with({ selector: "#jobTargetUriFilter" })
329 .then((targetUriFilter) => {
331 targetUriFilter.setValue("one").then((_) => {
332 loadTable.getRows().then((jobRows) => {
333 expect(jobRows.length).toEqual(2);
334 jobRows[0].getCellTextByColumnName().then((value) => {
335 expect(expectedJob1Row).toEqual(
336 jasmine.objectContaining(value)
338 targetUriFilter.setValue("");
347 MatInputHarness.with({ selector: "#jobStatusFilter" })
349 .then((statusFilter) => {
351 statusFilter.setValue("ENA").then((_) => {
352 loadTable.getRows().then((jobRows) => {
353 expect(jobRows.length).toEqual(2);
354 jobRows[0].getCellTextByColumnName().then((value) => {
355 expect(expectedJob1Row).toEqual(
356 jasmine.objectContaining(value)
358 statusFilter.setValue("");
365 discardPeriodicTasks();
368 describe("#sorting", () => {
369 it("should verify sort functionality on the table", fakeAsync(() => {
373 let sort = loader.getHarness(MatSortHarness);
376 sort.then((value) => {
377 value.getSortHeaders({ sortDirection: "" }).then((headers) => {
378 expect(headers.length).toBe(6);
382 headers[0].isActive().then((value) => {
383 expect(value).toBe(true);
385 headers[0].getSortDirection().then((value) => {
386 expect(value).toBe("asc");
391 headers[0].getSortDirection().then((value) => {
392 expect(value).toBe("desc");
396 discardPeriodicTasks();
399 it("should sort table asc and desc by first header", fakeAsync(() => {
403 let sort = loader.getHarness(MatSortHarness);
406 sort.then((value) => {
408 .getHarness(MatTableHarness.with({ selector: "#jobsTable" }))
409 .then((loadTable) => {
411 value.getSortHeaders().then((headers) => {
414 headers[0].getSortDirection().then((direction) => {
415 expect(direction).toBe("asc");
417 loadTable.getRows().then((jobRows) => {
418 jobRows[0].getCellTextByColumnName().then((value) => {
419 expect(expectedJob1Row).toEqual(
420 jasmine.objectContaining(value)
427 headers[0].getSortDirection().then((direction) => {
428 expect(direction).toBe("desc");
430 loadTable.getRows().then((jobRows) => {
431 jobRows[jobRows.length - 1]
432 .getCellTextByColumnName()
434 expect(expectedJob1Row).toEqual(
435 jasmine.objectContaining(value)
442 discardPeriodicTasks();
445 it("should not sort when clicking on filtering box", fakeAsync(() => {
446 const expectedJobRow = {
448 prodIds: "producer1",
451 targetUri: "http://one",
456 component.ngOnInit();
460 .getHarness(MatTableHarness.with({ selector: "#jobsTable" }))
461 .then((loadTable) => {
463 .getHarness(MatInputHarness.with({ selector: "#jobIdFilter" }))
464 .then((idFilter) => {
466 idFilter.setValue("").then((_) => {
467 loadTable.getRows().then((jobRows) => {
468 expect(jobRows.length).toEqual(2);
469 jobRows[1].getCellTextByColumnName().then((value) => {
470 expect(expectedJobRow).toEqual(
471 jasmine.objectContaining(value)
479 MatInputHarness.with({ selector: "#jobTypeIdFilter" })
481 .then((typeIdFilter) => {
483 typeIdFilter.setValue("").then((_) => {
484 loadTable.getRows().then((jobRows) => {
485 expect(jobRows.length).toEqual(2);
486 jobRows[1].getCellTextByColumnName().then((value) => {
487 expect(expectedJobRow).toEqual(
488 jasmine.objectContaining(value)
495 .getHarness(MatInputHarness.with({ selector: "#jobOwnerFilter" }))
496 .then((ownerFilter) => {
498 ownerFilter.setValue("").then((_) => {
499 loadTable.getRows().then((jobRows) => {
500 expect(jobRows.length).toEqual(2);
501 jobRows[1].getCellTextByColumnName().then((value) => {
502 expect(expectedJobRow).toEqual(
503 jasmine.objectContaining(value)
511 MatInputHarness.with({ selector: "#jobTargetUriFilter" })
513 .then((targetUriFilter) => {
515 targetUriFilter.setValue("").then((_) => {
516 loadTable.getRows().then((jobRows) => {
517 expect(jobRows.length).toEqual(2);
518 jobRows[1].getCellTextByColumnName().then((value) => {
519 expect(expectedJobRow).toEqual(
520 jasmine.objectContaining(value)
528 MatInputHarness.with({ selector: "#jobStatusFilter" })
530 .then((statusFilter) => {
532 statusFilter.setValue("").then((_) => {
533 loadTable.getRows().then((jobRows) => {
534 expect(jobRows.length).toEqual(2);
535 jobRows[1].getCellTextByColumnName().then((value) => {
536 expect(expectedJobRow).toEqual(
537 jasmine.objectContaining(value)
544 discardPeriodicTasks();
548 describe("#paging", () => {
549 it("should work properly on the table", fakeAsync(() => {
550 let consumerServiceSpy = TestBed.inject(
552 ) as jasmine.SpyObj<ConsumerService>;
553 consumerServiceSpy.getJobIds.and.returnValue(
554 of(["job1", "job2", "job3", "job4", "job5", "job6", "job7"])
556 consumerServiceSpy.getJobInfo.and.returnValue(of(jobInfo1));
557 consumerServiceSpy.getConsumerStatus.and.returnValue(of(jobStatus1));
558 component.ngOnInit();
561 loader.getHarness(MatPaginatorHarness).then((paging) => {
562 paging.setPageSize(5);
565 .getHarness(MatTableHarness.with({ selector: "#jobsTable" }))
566 .then((loadTable) => {
567 loadTable.getRows().then((jobRows) => {
568 expect(jobRows.length).toEqual(5);
571 paging.goToNextPage();
574 loadTable.getRows().then((jobRows) => {
575 expect(jobRows.length).toEqual(2);
576 jobRows[0].getCellTextByColumnName().then((value) => {
577 const expectedRow = {
579 prodIds: "producer1",
582 targetUri: "http://one",
585 discardPeriodicTasks();
586 expect(expectedRow).toEqual(jasmine.objectContaining(value));
596 function setServiceSpy() {
597 let consumerServiceSpy = TestBed.inject(
599 ) as jasmine.SpyObj<ConsumerService>;
600 consumerServiceSpy.getJobIds.and.returnValue(of(["job1", "job2"]));
601 consumerServiceSpy.getJobInfo.and.returnValue(of(jobInfo1));
602 consumerServiceSpy.getConsumerStatus.and.returnValue(of(jobStatus1));