Fixing auto-refresh for jobs

Issue-ID: NONRTRIC-475
Signed-off-by: ychacon <yennifer.chacon@est.tech>
Change-Id: Ifaf7e28a26e6b16ee62ed4000bc49aa38e421b9d
diff --git a/webapp-frontend/src/app/ei-coordinator/jobs-list/jobs-list.component.spec.ts b/webapp-frontend/src/app/ei-coordinator/jobs-list/jobs-list.component.spec.ts
index 7ccb80d..44fcb6a 100644
--- a/webapp-frontend/src/app/ei-coordinator/jobs-list/jobs-list.component.spec.ts
+++ b/webapp-frontend/src/app/ei-coordinator/jobs-list/jobs-list.component.spec.ts
@@ -27,6 +27,7 @@
   fakeAsync,
   TestBed,
   tick,
+  flushMicrotasks,
 } from "@angular/core/testing";
 import { FormsModule, ReactiveFormsModule } from "@angular/forms";
 import { MatFormFieldModule } from "@angular/material/form-field";
@@ -267,7 +268,7 @@
         .then((loadTable) => {
           loadTable.getRows().then((jobRows) => {
             jobRows[0].getCellTextByColumnName().then((value) => {
-              expect(expectedJobRow).toContain(jasmine.objectContaining(value));
+              expect(expectedJobRow).toEqual(jasmine.objectContaining(value));
             });
           });
         });
@@ -285,63 +286,78 @@
           loader
             .getHarness(MatInputHarness.with({ selector: "#jobIdFilter" }))
             .then((idFilter) => {
-              idFilter.setValue("1");
-              loadTable.getRows().then((jobRows) => {
-                expect(jobRows.length).toEqual(2);
-                jobRows[0].getCellTextByColumnName().then((value) => {
-                  expect(expectedJob1Row).toContain(
-                    jasmine.objectContaining(value)
-                  );
+              tick(10);
+              idFilter.setValue("1").then((_) => {
+                tick(10);
+                loadTable.getRows().then((jobRows) => {
+                  expect(jobRows.length).toEqual(2);
+                  jobRows[0].getCellTextByColumnName().then((value) => {
+                    expect(expectedJob1Row).toEqual(
+                      jasmine.objectContaining(value)
+                    );
+                    idFilter.setValue("");
+                    flushMicrotasks();
+                  });
                 });
               });
-              idFilter.setValue("");
             });
+
           loader
             .getHarness(MatInputHarness.with({ selector: "#jobTypeIdFilter" }))
             .then((typeIdFilter) => {
-              typeIdFilter.setValue("1");
-              loadTable.getRows().then((jobRows) => {
-                expect(jobRows.length).toEqual(2);
-                jobRows[0].getCellTextByColumnName().then((value) => {
-                  expect(expectedJob1Row).toContain(
-                    jasmine.objectContaining(value)
-                  );
+              tick(10);
+              typeIdFilter.setValue("1").then((_) => {
+                loadTable.getRows().then((jobRows) => {
+                  expect(jobRows.length).toEqual(2);
+                  jobRows[0].getCellTextByColumnName().then((value) => {
+                    expect(expectedJob1Row).toEqual(
+                      jasmine.objectContaining(value)
+                    );
+                    typeIdFilter.setValue("");
+                    flushMicrotasks();
+                  });
                 });
               });
-              typeIdFilter.setValue("");
             });
+
           loader
             .getHarness(MatInputHarness.with({ selector: "#jobOwnerFilter" }))
             .then((ownerFilter) => {
-              ownerFilter.setValue("1");
-              loadTable.getRows().then((jobRows) => {
-                expect(jobRows.length).toEqual(2);
-                jobRows[0].getCellTextByColumnName().then((value) => {
-                  expect(expectedJob1Row).toContain(
-                    jasmine.objectContaining(value)
-                  );
+              tick(10);
+              ownerFilter.setValue("1").then((_) => {
+                loadTable.getRows().then((jobRows) => {
+                  expect(jobRows.length).toEqual(2);
+                  jobRows[0].getCellTextByColumnName().then((value) => {
+                    expect(expectedJob1Row).toEqual(
+                      jasmine.objectContaining(value)
+                    );
+                    ownerFilter.setValue("");
+                    flushMicrotasks();
+                  });
                 });
               });
-              ownerFilter.setValue("");
             });
+
           loader
             .getHarness(
               MatInputHarness.with({ selector: "#jobTargetUriFilter" })
             )
             .then((targetUriFilter) => {
-              targetUriFilter.setValue("1");
-              loadTable.getRows().then((jobRows) => {
-                expect(jobRows.length).toEqual(2);
-                jobRows[0].getCellTextByColumnName().then((value) => {
-                  expect(expectedJob1Row).toContain(
-                    jasmine.objectContaining(value)
-                  );
+              tick(10);
+              targetUriFilter.setValue("one").then((_) => {
+                loadTable.getRows().then((jobRows) => {
+                  expect(jobRows.length).toEqual(2);
+                  jobRows[0].getCellTextByColumnName().then((value) => {
+                    expect(expectedJob1Row).toEqual(
+                      jasmine.objectContaining(value)
+                    );
+                    targetUriFilter.setValue("");
+                    flushMicrotasks();
+                  });
                 });
               });
-              targetUriFilter.setValue("");
             });
         });
-
       discardPeriodicTasks();
     }));
 
@@ -419,6 +435,14 @@
       }));
 
       it("should not sort when clicking on filtering box", fakeAsync(() => {
+        const expectedJobRow = {
+          jobId: "job1",
+          prodId: "producer2",
+          typeId: "type1",
+          owner: "owner1",
+          targetUri: "http://one",
+        };
+
         setServiceSpy();
         component.ngOnInit();
         tick(0);
@@ -426,67 +450,72 @@
         loader
           .getHarness(MatTableHarness.with({ selector: "#jobsTable" }))
           .then((loadTable) => {
-            loadTable.getRows().then((jobRows) => {
-              let jobIds, jobTypeIds, jobOwner, jobTargetUri: String[];
-              for (let i = 0; i < jobRows.length; i++) {
-                jobRows[i].getCellTextByColumnName().then((value) => {
-                  jobIds.push(value[0]);
-                  jobTypeIds.push(value[2]);
-                  jobOwner.push(value[3]);
-                  jobTargetUri.push(value[4]);
-                });
-              };
-              loader
-                .getHarness(MatInputHarness.with({ selector: "#jobIdFilter"}))
-                .then((idFilter) => {
-                  let unfilteredJobIds: String[];
-                  idFilter.setValue("");
-                  for (let i = 0; i < jobRows.length; i++) {
-                    jobRows[i].getCellTextByColumnName().then((value) => {
-                      unfilteredJobIds.push(value[0]);
+            loader
+              .getHarness(MatInputHarness.with({ selector: "#jobIdFilter" }))
+              .then((idFilter) => {
+                tick(10);
+                idFilter.setValue("").then((_) => {
+                  loadTable.getRows().then((jobRows) => {
+                    expect(jobRows.length).toEqual(4);
+                    jobRows[2].getCellTextByColumnName().then((value) => {
+                      expect(expectedJobRow).toEqual(
+                        jasmine.objectContaining(value)
+                      );
                     });
-                  };
-                  expect(unfilteredJobIds).toBe(jobIds);
+                  });
                 });
-              loader
-                .getHarness(MatInputHarness.with({ selector: "#jobTypeIdFilter"}))
-                .then((idFilter) => {
-                  let unfilteredJobTypeIds: String[];
-                  idFilter.setValue("");
-                  for (let i = 0; i < jobRows.length; i++) {
-                    jobRows[i].getCellTextByColumnName().then((value) => {
-                      unfilteredJobTypeIds.push(value[2]);
+              });
+            loader
+              .getHarness(
+                MatInputHarness.with({ selector: "#jobTypeIdFilter" })
+              )
+              .then((typeIdFilter) => {
+                tick(10);
+                typeIdFilter.setValue("").then((_) => {
+                  loadTable.getRows().then((jobRows) => {
+                    expect(jobRows.length).toEqual(4);
+                    jobRows[2].getCellTextByColumnName().then((value) => {
+                      expect(expectedJobRow).toEqual(
+                        jasmine.objectContaining(value)
+                      );
                     });
-                  };
-                  expect(unfilteredJobTypeIds).toBe(jobTypeIds);
+                  });
                 });
-              loader
-                .getHarness(MatInputHarness.with({ selector: "#jobOwnerFilter"}))
-                .then((idFilter) => {
-                  let unfilteredJobOwner: String[];
-                  idFilter.setValue("");
-                  for (let i = 0; i < jobRows.length; i++) {
-                    jobRows[i].getCellTextByColumnName().then((value) => {
-                      unfilteredJobOwner.push(value[3]);
+              });
+            loader
+              .getHarness(MatInputHarness.with({ selector: "#jobOwnerFilter" }))
+              .then((ownerFilter) => {
+                tick(10);
+                ownerFilter.setValue("").then((_) => {
+                  loadTable.getRows().then((jobRows) => {
+                    expect(jobRows.length).toEqual(4);
+                    jobRows[2].getCellTextByColumnName().then((value) => {
+                      expect(expectedJobRow).toEqual(
+                        jasmine.objectContaining(value)
+                      );
                     });
-                  };
-                  expect(unfilteredJobOwner).toBe(jobOwner);
+                  });
                 });
-              loader
-                .getHarness(MatInputHarness.with({ selector: "#jobTargetUriFilter"}))
-                .then((idFilter) => {
-                  let unfilteredJobTargetUri: String[];
-                  idFilter.setValue("");
-                  for (let i = 0; i < jobRows.length; i++) {
-                    jobRows[i].getCellTextByColumnName().then((value) => {
-                      unfilteredJobTargetUri.push(value[4]);
+              });
+            loader
+              .getHarness(
+                MatInputHarness.with({ selector: "#jobTargetUriFilter" })
+              )
+              .then((targetUriFilter) => {
+                tick(10);
+                targetUriFilter.setValue("").then((_) => {
+                  loadTable.getRows().then((jobRows) => {
+                    expect(jobRows.length).toEqual(4);
+                    jobRows[2].getCellTextByColumnName().then((value) => {
+                      expect(expectedJobRow).toEqual(
+                        jasmine.objectContaining(value)
+                      );
                     });
-                  };
-                  expect(unfilteredJobTargetUri).toBe(jobTargetUri);
+                  });
                 });
-            });
+              });
           });
-          discardPeriodicTasks();
+        discardPeriodicTasks();
       }));
     });
 
diff --git a/webapp-frontend/src/app/ei-coordinator/jobs-list/jobs-list.component.ts b/webapp-frontend/src/app/ei-coordinator/jobs-list/jobs-list.component.ts
index a85e577..178400e 100644
--- a/webapp-frontend/src/app/ei-coordinator/jobs-list/jobs-list.component.ts
+++ b/webapp-frontend/src/app/ei-coordinator/jobs-list/jobs-list.component.ts
@@ -22,20 +22,9 @@
 import { MatPaginator } from "@angular/material/paginator";
 import { Sort } from "@angular/material/sort";
 import { MatTableDataSource } from "@angular/material/table";
-import {
-  EMPTY,
-  forkJoin,
-  Subscription,
-  timer,
-} from "rxjs";
+import { EMPTY, forkJoin, Subscription, timer } from "rxjs";
 import { BehaviorSubject } from "rxjs/BehaviorSubject";
-import {
-  mergeMap,
-  finalize,
-  map,
-  tap,
-  switchMap
-} from "rxjs/operators";
+import { mergeMap, finalize, map, tap, switchMap } from "rxjs/operators";
 import { EIJob } from "@interfaces/ei.types";
 import { EIService } from "@services/ei/ei.service";
 import { UiService } from "@services/ui/ui.service";
@@ -63,10 +52,11 @@
   private jobsSubject$ = new BehaviorSubject<Job[]>([]);
   private refresh$ = new BehaviorSubject("");
   private loadingSubject$ = new BehaviorSubject<boolean>(false);
-  private polling$ = new BehaviorSubject<boolean>(true);
+  private polling$ = new BehaviorSubject(0);
   public loading$ = this.loadingSubject$.asObservable();
   subscription: Subscription;
   checked: boolean = false;
+  firstTime: boolean = true;
 
   constructor(private eiSvc: EIService, private ui: UiService) {
     this.jobForm = new FormGroup({
@@ -116,23 +106,26 @@
       finalize(() => this.loadingSubject$.next(false))
     );
 
-    const refreshedJobs$ = this.refresh$
-      .pipe(
-        switchMap((_) => timer(0, 10000).pipe(
+    const refreshedJobs$ = this.refresh$.pipe(
+      switchMap((_) =>
+        timer(0, 10000).pipe(
           tap((_) => {
             this.loadingSubject$.next(true);
           }),
           switchMap((_) => jobs$),
           map((response) => this.extractJobs(prodId, response))
         )
-        )
-      );
-    
-    return this.polling$.pipe(
-      switchMap(p => {
-        return p ? refreshedJobs$ : EMPTY;
-      })
-    ).subscribe();
+      )
+    );
+
+    return this.polling$
+      .pipe(
+        switchMap((value) => {
+          let pollCondition = value == 0 || this.checked;
+          return pollCondition ? refreshedJobs$ : EMPTY;
+        })
+      )
+      .subscribe();
   }
 
   ngOnDestroy() {
@@ -169,8 +162,9 @@
     this.jobsDataSource.data = data;
   }
 
-  stopPolling(checked){
-    this.polling$.next(checked);
+  stopPolling(checked) {
+    this.checked = checked;
+    this.polling$.next(this.jobs().length);
   }
 
   compare(a: any, b: any, isAsc: boolean) {
@@ -212,6 +206,10 @@
       jobList = jobList.concat(jobs.map((job) => this.createJob(element, job)));
     });
     this.jobsSubject$.next(jobList);
+    if (this.firstTime && jobList.length > 0) {
+      this.polling$.next(jobList.length);
+      this.firstTime = false;
+    }
     return jobList;
   }