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 { EIJob } from "@interfaces/ei.types";
45 import { EIService } from "@services/ei/ei.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 ei_job_identity: "job1",
56 ei_type_identity: "type1",
58 target_uri: "http://one",
61 ei_job_identity: "job2",
62 ei_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: EIService, 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 ei_job_identity: "job1",
243 jobparam2: "value2_job2",
244 jobparam3: "value3_job2",
245 jobparam1: "value1_job2",
247 target_uri: "http://one",
250 let eiServiceSpy = TestBed.inject(EIService) as jasmine.SpyObj<EIService>;
251 eiServiceSpy.getProducerIds.and.returnValue(of(["producer1"]));
252 eiServiceSpy.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 loader.getHarness(MatSortHarness).then((sort) => {
370 sort.getSortHeaders({ sortDirection: "" }).then((headers) => {
371 expect(headers.length).toBe(5);
373 headers[0].click().then((_) => {
374 headers[0].isActive().then((active) => {
375 expect(active).toBe(true);
377 headers[0].getSortDirection().then((direction) => {
378 expect(direction).toBe("asc");
381 headers[0].click().then((_) => {
382 headers[0].getSortDirection().then((direction) => {
383 expect(direction).toBe("desc");
388 discardPeriodicTasks();
391 it("should sort table asc and desc by first header", fakeAsync(() => {
395 loader.getHarness(MatSortHarness).then((sort) => {
397 .getHarness(MatTableHarness.with({ selector: "#jobsTable" }))
398 .then((loadTable) => {
399 sort.getSortHeaders().then((headers) => {
400 headers[0].click().then((_) => {
401 headers[0].getSortDirection().then((direction) => {
402 expect(direction).toBe("");
405 headers[0].click().then((_) => {
406 headers[0].getSortDirection().then((direction) => {
407 expect(direction).toBe("asc");
410 loadTable.getRows().then((jobRows) => {
411 jobRows[0].getCellTextByColumnName().then((value) => {
412 expect(expectedJob1Row).toContain(
413 jasmine.objectContaining(value)
417 headers[0].click().then((_) => {
418 headers[0].getSortDirection().then((direction) => {
419 expect(direction).toBe("desc");
422 loadTable.getRows().then((jobRows) => {
423 jobRows[jobRows.length - 1]
424 .getCellTextByColumnName()
426 expect(expectedJob1Row).toContain(
427 jasmine.objectContaining(value)
434 discardPeriodicTasks();
437 it("should not sort when clicking on filtering box", fakeAsync(() => {
438 const expectedJobRow = {
443 targetUri: "http://one",
447 component.ngOnInit();
451 .getHarness(MatTableHarness.with({ selector: "#jobsTable" }))
452 .then((loadTable) => {
454 .getHarness(MatInputHarness.with({ selector: "#jobIdFilter" }))
455 .then((idFilter) => {
457 idFilter.setValue("").then((_) => {
458 loadTable.getRows().then((jobRows) => {
459 expect(jobRows.length).toEqual(4);
460 jobRows[2].getCellTextByColumnName().then((value) => {
461 expect(expectedJobRow).toEqual(
462 jasmine.objectContaining(value)
470 MatInputHarness.with({ selector: "#jobTypeIdFilter" })
472 .then((typeIdFilter) => {
474 typeIdFilter.setValue("").then((_) => {
475 loadTable.getRows().then((jobRows) => {
476 expect(jobRows.length).toEqual(4);
477 jobRows[2].getCellTextByColumnName().then((value) => {
478 expect(expectedJobRow).toEqual(
479 jasmine.objectContaining(value)
486 .getHarness(MatInputHarness.with({ selector: "#jobOwnerFilter" }))
487 .then((ownerFilter) => {
489 ownerFilter.setValue("").then((_) => {
490 loadTable.getRows().then((jobRows) => {
491 expect(jobRows.length).toEqual(4);
492 jobRows[2].getCellTextByColumnName().then((value) => {
493 expect(expectedJobRow).toEqual(
494 jasmine.objectContaining(value)
502 MatInputHarness.with({ selector: "#jobTargetUriFilter" })
504 .then((targetUriFilter) => {
506 targetUriFilter.setValue("").then((_) => {
507 loadTable.getRows().then((jobRows) => {
508 expect(jobRows.length).toEqual(4);
509 jobRows[2].getCellTextByColumnName().then((value) => {
510 expect(expectedJobRow).toEqual(
511 jasmine.objectContaining(value)
518 discardPeriodicTasks();
522 describe("#paging", () => {
523 it("should work properly on the table", fakeAsync(() => {
524 let eiServiceSpy = TestBed.inject(
526 ) as jasmine.SpyObj<EIService>;
527 eiServiceSpy.getProducerIds.and.returnValue(
528 of(["producer1", "producer2"])
530 eiServiceSpy.getJobsForProducer.and.returnValue(
531 of([eijob1, eijob2, eijob1])
535 loader.getHarness(MatPaginatorHarness).then((paging) => {
536 paging.setPageSize(5);
539 .getHarness(MatTableHarness.with({ selector: "#jobsTable" }))
540 .then((loadTable) => {
541 loadTable.getRows().then((jobRows) => {
542 expect(jobRows.length).toEqual(5);
544 paging.goToNextPage();
545 loadTable.getRows().then((jobRows) => {
546 expect(jobRows.length).toEqual(1);
547 jobRows[0].getCellTextByColumnName().then((value) => {
548 const expectedRow = {
553 targetUri: "http://one",
555 expect(expectedRow).toContain(
556 jasmine.objectContaining(value)
562 discardPeriodicTasks();
568 function setServiceSpy() {
569 let eiServiceSpy = TestBed.inject(EIService) as jasmine.SpyObj<EIService>;
570 eiServiceSpy.getProducerIds.and.returnValue(of(["producer1", "producer2"]));
571 eiServiceSpy.getJobsForProducer.and.returnValue(of([eijob1, eijob2]));