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";
30 } from "@angular/core/testing";
31 import { FormsModule, ReactiveFormsModule } from "@angular/forms";
32 import { MatFormFieldModule } from "@angular/material/form-field";
33 import { MatInputModule } from "@angular/material/input";
34 import { MatInputHarness } from "@angular/material/input/testing";
35 import { MatPaginatorModule } from "@angular/material/paginator";
36 import { MatSortModule } from "@angular/material/sort";
37 import { MatSortHarness } from "@angular/material/sort/testing";
38 import { MatPaginatorHarness } from "@angular/material/paginator/testing";
39 import { MatTableModule } from "@angular/material/table";
40 import { MatTableHarness } from "@angular/material/table/testing";
41 import { BrowserAnimationsModule } from "@angular/platform-browser/animations";
42 import { of } from "rxjs/observable/of";
43 import { EIJob } from "@interfaces/ei.types";
44 import { EIService } from "@services/ei/ei.service";
45 import { UiService } from "@services/ui/ui.service";
47 import { Job, JobsListComponent } from "./jobs-list.component";
48 import { Subscription } from "rxjs";
50 let component: JobsListComponent;
51 let fixture: ComponentFixture<JobsListComponent>;
54 ei_job_identity: "job1",
55 ei_type_identity: "type1",
57 target_uri: "http://one",
60 ei_job_identity: "job2",
61 ei_type_identity: "type2",
63 target_uri: "http://two",
70 targetUri: "http://one",
77 targetUri: "http://two",
84 targetUri: "http://one",
91 targetUri: "http://two",
95 describe("JobsListComponent", () => {
96 let loader: HarnessLoader;
98 beforeEach(async(() => {
99 const spy = jasmine.createSpyObj("EIService", [
101 "getJobsForProducer",
104 TestBed.configureTestingModule({
111 BrowserAnimationsModule,
115 schemas: [CUSTOM_ELEMENTS_SCHEMA],
116 declarations: [JobsListComponent],
117 providers: [{ provide: EIService, useValue: spy }, UiService],
121 fixture = TestBed.createComponent(JobsListComponent);
122 component = fixture.componentInstance;
123 loader = TestbedHarnessEnvironment.loader(fixture);
127 const expectedJob1Row = {
132 targetUri: "http://one",
135 it("should create", () => {
136 expect(component).toBeTruthy();
139 describe("#content", () => {
140 it("should loadJobs", fakeAsync(() => {
142 const newSub: Subscription = component.dataSubscription();
145 const actualJobs: Job[] = component.jobs();
146 expect(actualJobs.length).toEqual(4);
147 expect(actualJobs).toEqual([job1, job2, job3, job4]);
148 newSub.unsubscribe();
151 it("should contain job table with correct columns", fakeAsync(() => {
153 component.ngOnInit();
157 .getHarness(MatTableHarness.with({ selector: "#jobsTable" }))
158 .then((loadTable) => {
159 loadTable.getHeaderRows().then((headerRow) => {
160 headerRow[0].getCellTextByColumnName().then((header) => {
161 expect(header).toEqual({
163 prodId: "Producer ID",
166 targetUri: "Target URI",
172 discardPeriodicTasks();
175 it("should set correct dark mode from UIService", fakeAsync(() => {
177 component.ngOnInit();
180 const uiService: UiService = TestBed.inject(UiService);
181 expect(component.darkMode).toBeTruthy();
183 uiService.darkModeState.next(false);
184 fixture.detectChanges();
185 expect(component.darkMode).toBeFalsy();
186 discardPeriodicTasks();
190 describe("#jobsTable", () => {
191 it("should contain data after initialization", fakeAsync(() => {
193 component.ngOnInit();
196 const expectedJobRows = [
203 targetUri: "http://two",
210 targetUri: "http://one",
217 targetUri: "http://two",
222 .getHarness(MatTableHarness.with({ selector: "#jobsTable" }))
223 .then((loadTable) => {
224 loadTable.getRows().then((jobRows) => {
225 expect(jobRows.length).toEqual(4);
226 jobRows.forEach((row) => {
227 row.getCellTextByColumnName().then((values) => {
228 expect(expectedJobRows).toContain(
229 jasmine.objectContaining(values)
235 discardPeriodicTasks();
238 it("should display default values for non required properties ", fakeAsync(() => {
239 const jobMissingProperties = {
240 ei_job_identity: "job1",
242 jobparam2: "value2_job2",
243 jobparam3: "value3_job2",
244 jobparam1: "value1_job2",
246 target_uri: "http://one",
249 let eiServiceSpy = TestBed.inject(EIService) as jasmine.SpyObj<EIService>;
250 eiServiceSpy.getProducerIds.and.returnValue(of(["producer1"]));
251 eiServiceSpy.getJobsForProducer.and.returnValue(
252 of([jobMissingProperties])
255 component.ngOnInit();
257 const expectedJobRow = {
260 typeId: "< No type >",
261 owner: "< No owner >",
262 targetUri: "http://one",
266 .getHarness(MatTableHarness.with({ selector: "#jobsTable" }))
267 .then((loadTable) => {
268 loadTable.getRows().then((jobRows) => {
269 jobRows[0].getCellTextByColumnName().then((value) => {
270 expect(expectedJobRow).toContain(jasmine.objectContaining(value));
274 discardPeriodicTasks();
277 it("filtering", fakeAsync(() => {
279 component.ngOnInit();
283 .getHarness(MatTableHarness.with({ selector: "#jobsTable" }))
284 .then((loadTable) => {
286 .getHarness(MatInputHarness.with({ selector: "#jobIdFilter" }))
287 .then((idFilter) => {
288 idFilter.setValue("1");
289 loadTable.getRows().then((jobRows) => {
290 expect(jobRows.length).toEqual(2);
291 jobRows[0].getCellTextByColumnName().then((value) => {
292 expect(expectedJob1Row).toContain(
293 jasmine.objectContaining(value)
297 idFilter.setValue("");
300 .getHarness(MatInputHarness.with({ selector: "#jobTypeIdFilter" }))
301 .then((typeIdFilter) => {
302 typeIdFilter.setValue("1");
303 loadTable.getRows().then((jobRows) => {
304 expect(jobRows.length).toEqual(2);
305 jobRows[0].getCellTextByColumnName().then((value) => {
306 expect(expectedJob1Row).toContain(
307 jasmine.objectContaining(value)
311 typeIdFilter.setValue("");
314 .getHarness(MatInputHarness.with({ selector: "#jobOwnerFilter" }))
315 .then((ownerFilter) => {
316 ownerFilter.setValue("1");
317 loadTable.getRows().then((jobRows) => {
318 expect(jobRows.length).toEqual(2);
319 jobRows[0].getCellTextByColumnName().then((value) => {
320 expect(expectedJob1Row).toContain(
321 jasmine.objectContaining(value)
325 ownerFilter.setValue("");
329 MatInputHarness.with({ selector: "#jobTargetUriFilter" })
331 .then((targetUriFilter) => {
332 targetUriFilter.setValue("1");
333 loadTable.getRows().then((jobRows) => {
334 expect(jobRows.length).toEqual(2);
335 jobRows[0].getCellTextByColumnName().then((value) => {
336 expect(expectedJob1Row).toContain(
337 jasmine.objectContaining(value)
341 targetUriFilter.setValue("");
345 discardPeriodicTasks();
348 describe("#sorting", () => {
349 it("should verify sort functionality on the table", fakeAsync(() => {
353 loader.getHarness(MatSortHarness).then((sort) => {
354 sort.getSortHeaders({ sortDirection: "" }).then((headers) => {
355 expect(headers.length).toBe(5);
357 headers[0].click().then((_) => {
358 headers[0].isActive().then((active) => {
359 expect(active).toBe(true);
361 headers[0].getSortDirection().then((direction) => {
362 expect(direction).toBe("asc");
365 headers[0].click().then((_) => {
366 headers[0].getSortDirection().then((direction) => {
367 expect(direction).toBe("desc");
372 discardPeriodicTasks();
375 it("should sort table asc and desc by first header", fakeAsync(() => {
379 loader.getHarness(MatSortHarness).then((sort) => {
381 .getHarness(MatTableHarness.with({ selector: "#jobsTable" }))
382 .then((loadTable) => {
383 sort.getSortHeaders().then((headers) => {
384 headers[0].click().then((_) => {
385 headers[0].getSortDirection().then((direction) => {
386 expect(direction).toBe("");
389 headers[0].click().then((_) => {
390 headers[0].getSortDirection().then((direction) => {
391 expect(direction).toBe("asc");
394 loadTable.getRows().then((jobRows) => {
395 jobRows[0].getCellTextByColumnName().then((value) => {
396 expect(expectedJob1Row).toContain(
397 jasmine.objectContaining(value)
401 headers[0].click().then((_) => {
402 headers[0].getSortDirection().then((direction) => {
403 expect(direction).toBe("desc");
406 loadTable.getRows().then((jobRows) => {
407 jobRows[jobRows.length - 1]
408 .getCellTextByColumnName()
410 expect(expectedJob1Row).toContain(
411 jasmine.objectContaining(value)
418 discardPeriodicTasks();
422 describe("#paging", () => {
423 it("should work properly on the table", fakeAsync(() => {
424 let eiServiceSpy = TestBed.inject(
426 ) as jasmine.SpyObj<EIService>;
427 eiServiceSpy.getProducerIds.and.returnValue(
428 of(["producer1", "producer2"])
430 eiServiceSpy.getJobsForProducer.and.returnValue(
431 of([eijob1, eijob2, eijob1])
435 loader.getHarness(MatPaginatorHarness).then((paging) => {
436 paging.setPageSize(5);
439 .getHarness(MatTableHarness.with({ selector: "#jobsTable" }))
440 .then((loadTable) => {
441 loadTable.getRows().then((jobRows) => {
442 expect(jobRows.length).toEqual(5);
444 paging.goToNextPage();
445 loadTable.getRows().then((jobRows) => {
446 expect(jobRows.length).toEqual(1);
447 jobRows[0].getCellTextByColumnName().then((value) => {
448 const expectedRow = {
453 targetUri: "http://one",
455 expect(expectedRow).toContain(
456 jasmine.objectContaining(value)
463 discardPeriodicTasks();
469 function setServiceSpy() {
470 let eiServiceSpy = TestBed.inject(EIService) as jasmine.SpyObj<EIService>;
471 eiServiceSpy.getProducerIds.and.returnValue(of(["producer1", "producer2"]));
472 eiServiceSpy.getJobsForProducer.and.returnValue(of([eijob1, eijob2]));