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 { JobInfo } from "@interfaces/producer.types";
45 import { ProducerService } from "@services/ei/producer.service";
46 import { UiService } from "@services/ui/ui.service";
48 import { Job, JobsListComponent } from "./jobs-list.component";
49 import { Subscription } from "rxjs";
51 let component: JobsListComponent;
52 let fixture: ComponentFixture<JobsListComponent>;
55 info_job_identity: "job1",
56 info_type_identity: "type1",
58 target_uri: "http://one",
61 info_job_identity: "job2",
62 info_type_identity: "type2",
64 target_uri: "http://two",
71 targetUri: "http://one",
78 targetUri: "http://two",
85 targetUri: "http://one",
92 targetUri: "http://two",
96 describe("JobsListComponent", () => {
97 let loader: HarnessLoader;
99 beforeEach(async(() => {
100 const spy = jasmine.createSpyObj("EIService", [
102 "getJobsForProducer",
105 TestBed.configureTestingModule({
112 BrowserAnimationsModule,
116 schemas: [CUSTOM_ELEMENTS_SCHEMA],
117 declarations: [JobsListComponent],
118 providers: [{ provide: ProducerService, useValue: spy }, UiService],
122 fixture = TestBed.createComponent(JobsListComponent);
123 component = fixture.componentInstance;
124 loader = TestbedHarnessEnvironment.loader(fixture);
128 const expectedJob1Row = {
133 targetUri: "http://one",
136 it("should create", () => {
137 expect(component).toBeTruthy();
140 describe("#content", () => {
141 it("should loadJobs", fakeAsync(() => {
143 const newSub: Subscription = component.dataSubscription();
146 const actualJobs: Job[] = component.jobs();
147 expect(actualJobs.length).toEqual(4);
148 expect(actualJobs).toEqual([job1, job2, job3, job4]);
149 newSub.unsubscribe();
152 it("should contain job table with correct columns", fakeAsync(() => {
154 component.ngOnInit();
158 .getHarness(MatTableHarness.with({ selector: "#jobsTable" }))
159 .then((loadTable) => {
160 loadTable.getHeaderRows().then((headerRow) => {
161 headerRow[0].getCellTextByColumnName().then((header) => {
162 expect(header).toEqual({
164 prodId: "Producer ID",
167 targetUri: "Target URI",
173 discardPeriodicTasks();
176 it("should set correct dark mode from UIService", fakeAsync(() => {
178 component.ngOnInit();
181 const uiService: UiService = TestBed.inject(UiService);
182 expect(component.darkMode).toBeTruthy();
184 uiService.darkModeState.next(false);
185 fixture.detectChanges();
186 expect(component.darkMode).toBeFalsy();
187 discardPeriodicTasks();
191 describe("#jobsTable", () => {
192 it("should contain data after initialization", fakeAsync(() => {
194 component.ngOnInit();
197 const expectedJobRows = [
204 targetUri: "http://two",
211 targetUri: "http://one",
218 targetUri: "http://two",
223 .getHarness(MatTableHarness.with({ selector: "#jobsTable" }))
224 .then((loadTable) => {
225 loadTable.getRows().then((jobRows) => {
226 expect(jobRows.length).toEqual(4);
227 jobRows.forEach((row) => {
228 row.getCellTextByColumnName().then((values) => {
229 expect(expectedJobRows).toContain(
230 jasmine.objectContaining(values)
236 discardPeriodicTasks();
239 it("should display default values for non required properties ", fakeAsync(() => {
240 const jobMissingProperties = {
241 info_job_identity: "job1",
243 jobparam2: "value2_job2",
244 jobparam3: "value3_job2",
245 jobparam1: "value1_job2",
247 target_uri: "http://one",
250 let producerServiceSpy = TestBed.inject(ProducerService) as jasmine.SpyObj<ProducerService>;
251 producerServiceSpy.getProducerIds.and.returnValue(of(["producer1"]));
252 producerServiceSpy.getJobsForProducer.and.returnValue(
253 of([jobMissingProperties])
256 component.ngOnInit();
258 const expectedJobRow = {
261 typeId: "< No type >",
262 owner: "< No owner >",
263 targetUri: "http://one",
267 .getHarness(MatTableHarness.with({ selector: "#jobsTable" }))
268 .then((loadTable) => {
269 loadTable.getRows().then((jobRows) => {
270 jobRows[0].getCellTextByColumnName().then((value) => {
271 expect(expectedJobRow).toEqual(jasmine.objectContaining(value));
275 discardPeriodicTasks();
278 it("filtering", fakeAsync(() => {
280 component.ngOnInit();
284 .getHarness(MatTableHarness.with({ selector: "#jobsTable" }))
285 .then((loadTable) => {
287 .getHarness(MatInputHarness.with({ selector: "#jobIdFilter" }))
288 .then((idFilter) => {
290 idFilter.setValue("1").then((_) => {
292 loadTable.getRows().then((jobRows) => {
293 expect(jobRows.length).toEqual(2);
294 jobRows[0].getCellTextByColumnName().then((value) => {
295 expect(expectedJob1Row).toEqual(
296 jasmine.objectContaining(value)
298 idFilter.setValue("");
306 .getHarness(MatInputHarness.with({ selector: "#jobTypeIdFilter" }))
307 .then((typeIdFilter) => {
309 typeIdFilter.setValue("1").then((_) => {
310 loadTable.getRows().then((jobRows) => {
311 expect(jobRows.length).toEqual(2);
312 jobRows[0].getCellTextByColumnName().then((value) => {
313 expect(expectedJob1Row).toEqual(
314 jasmine.objectContaining(value)
316 typeIdFilter.setValue("");
324 .getHarness(MatInputHarness.with({ selector: "#jobOwnerFilter" }))
325 .then((ownerFilter) => {
327 ownerFilter.setValue("1").then((_) => {
328 loadTable.getRows().then((jobRows) => {
329 expect(jobRows.length).toEqual(2);
330 jobRows[0].getCellTextByColumnName().then((value) => {
331 expect(expectedJob1Row).toEqual(
332 jasmine.objectContaining(value)
334 ownerFilter.setValue("");
343 MatInputHarness.with({ selector: "#jobTargetUriFilter" })
345 .then((targetUriFilter) => {
347 targetUriFilter.setValue("one").then((_) => {
348 loadTable.getRows().then((jobRows) => {
349 expect(jobRows.length).toEqual(2);
350 jobRows[0].getCellTextByColumnName().then((value) => {
351 expect(expectedJob1Row).toEqual(
352 jasmine.objectContaining(value)
354 targetUriFilter.setValue("");
361 discardPeriodicTasks();
364 describe("#sorting", () => {
365 it("should verify sort functionality on the table", fakeAsync(() => {
369 let sort = loader.getHarness(MatSortHarness);
372 sort.then((value) => {
373 value.getSortHeaders({ sortDirection: "" }).then((headers) => {
374 expect(headers.length).toBe(5);
378 headers[0].isActive().then((value) => {
379 expect(value).toBe(true);
381 headers[0].getSortDirection().then((value) => {
382 expect(value).toBe("asc");
387 headers[0].getSortDirection().then((value) => {
388 expect(value).toBe("desc");
392 discardPeriodicTasks();
395 it("should sort table asc and desc by first header", fakeAsync(() => {
399 let sort = loader.getHarness(MatSortHarness);
402 sort.then((value) => {
404 .getHarness(MatTableHarness.with({ selector: "#jobsTable" }))
405 .then((loadTable) => {
407 value.getSortHeaders().then((headers) => {
410 headers[0].getSortDirection().then((direction) => {
411 expect(direction).toBe("asc");
413 loadTable.getRows().then((jobRows) => {
414 jobRows[0].getCellTextByColumnName().then((value) => {
415 expect(expectedJob1Row).toEqual(
416 jasmine.objectContaining(value)
423 headers[0].getSortDirection().then((direction) => {
424 expect(direction).toBe("desc");
426 loadTable.getRows().then((jobRows) => {
427 jobRows[jobRows.length - 1]
428 .getCellTextByColumnName()
430 expect(expectedJob1Row).toEqual(
431 jasmine.objectContaining(value)
438 discardPeriodicTasks();
441 it("should not sort when clicking on filtering box", fakeAsync(() => {
442 const expectedJobRow = {
447 targetUri: "http://one",
451 component.ngOnInit();
455 .getHarness(MatTableHarness.with({ selector: "#jobsTable" }))
456 .then((loadTable) => {
458 .getHarness(MatInputHarness.with({ selector: "#jobIdFilter" }))
459 .then((idFilter) => {
461 idFilter.setValue("").then((_) => {
462 loadTable.getRows().then((jobRows) => {
463 expect(jobRows.length).toEqual(4);
464 jobRows[2].getCellTextByColumnName().then((value) => {
465 expect(expectedJobRow).toEqual(
466 jasmine.objectContaining(value)
474 MatInputHarness.with({ selector: "#jobTypeIdFilter" })
476 .then((typeIdFilter) => {
478 typeIdFilter.setValue("").then((_) => {
479 loadTable.getRows().then((jobRows) => {
480 expect(jobRows.length).toEqual(4);
481 jobRows[2].getCellTextByColumnName().then((value) => {
482 expect(expectedJobRow).toEqual(
483 jasmine.objectContaining(value)
490 .getHarness(MatInputHarness.with({ selector: "#jobOwnerFilter" }))
491 .then((ownerFilter) => {
493 ownerFilter.setValue("").then((_) => {
494 loadTable.getRows().then((jobRows) => {
495 expect(jobRows.length).toEqual(4);
496 jobRows[2].getCellTextByColumnName().then((value) => {
497 expect(expectedJobRow).toEqual(
498 jasmine.objectContaining(value)
506 MatInputHarness.with({ selector: "#jobTargetUriFilter" })
508 .then((targetUriFilter) => {
510 targetUriFilter.setValue("").then((_) => {
511 loadTable.getRows().then((jobRows) => {
512 expect(jobRows.length).toEqual(4);
513 jobRows[2].getCellTextByColumnName().then((value) => {
514 expect(expectedJobRow).toEqual(
515 jasmine.objectContaining(value)
522 discardPeriodicTasks();
526 describe("#paging", () => {
527 it("should work properly on the table", fakeAsync(() => {
528 let producerServiceSpy = TestBed.inject(
530 ) as jasmine.SpyObj<ProducerService>;
531 producerServiceSpy.getProducerIds.and.returnValue(
532 of(["producer1", "producer2"])
534 producerServiceSpy.getJobsForProducer.and.returnValue(
535 of([jobInfo1, jobInfo2, jobInfo1])
539 loader.getHarness(MatPaginatorHarness).then((paging) => {
540 paging.setPageSize(5);
543 .getHarness(MatTableHarness.with({ selector: "#jobsTable" }))
544 .then((loadTable) => {
545 loadTable.getRows().then((jobRows) => {
546 expect(jobRows.length).toEqual(5);
548 paging.goToNextPage();
549 loadTable.getRows().then((jobRows) => {
550 expect(jobRows.length).toEqual(1);
551 jobRows[0].getCellTextByColumnName().then((value) => {
552 const expectedRow = {
557 targetUri: "http://one",
559 expect(expectedRow).toContain(
560 jasmine.objectContaining(value)
566 discardPeriodicTasks();
572 function setServiceSpy() {
573 let producerServiceSpy = TestBed.inject(ProducerService) as jasmine.SpyObj<ProducerService>;
574 producerServiceSpy.getProducerIds.and.returnValue(of(["producer1", "producer2"]));
575 producerServiceSpy.getJobsForProducer.and.returnValue(of([jobInfo1, jobInfo2]));