From 1ea5c8f619afca077ca7265f1281576f71f03f9c Mon Sep 17 00:00:00 2001
From: Jeremie BRISON <ext.sopra.jbrison@grandlyon.com>
Date: Mon, 2 Nov 2020 17:35:31 +0100
Subject: [PATCH] fix(search) : Add unit test

---
 debug.log                                     |  1 +
 .../modal-filter/modal-filter.component.html  |  2 +-
 .../modal-filter.component.spec.ts            | 62 ++++++++++++++++++-
 .../modal-filter/modal-filter.component.ts    | 10 +--
 .../components/search/search.component.ts     |  6 +-
 5 files changed, 70 insertions(+), 11 deletions(-)

diff --git a/debug.log b/debug.log
index a9f90adc5..301edff39 100644
--- a/debug.log
+++ b/debug.log
@@ -1 +1,2 @@
 [1102/160447.168:ERROR:directory_reader_win.cc(43)] FindFirstFile: Le chemin d’accès spécifié est introuvable. (0x3)
+[1102/165734.564:ERROR:directory_reader_win.cc(43)] FindFirstFile: Le chemin d’accès spécifié est introuvable. (0x3)
diff --git a/src/app/structure-list/components/modal-filter/modal-filter.component.html b/src/app/structure-list/components/modal-filter/modal-filter.component.html
index 12b002559..ec2d69fb0 100644
--- a/src/app/structure-list/components/modal-filter/modal-filter.component.html
+++ b/src/app/structure-list/components/modal-filter/modal-filter.component.html
@@ -27,7 +27,7 @@
     </div>
     <div class="footer" fxLayout="row" fxLayoutAlign="end center" fxLayoutGap="3vw">
       <a (click)="clearFilters()">Effacer</a>
-      <button type="button" (click)="emitFilter()">Appliquer</button>
+      <button type="button" (click)="emitModules()">Appliquer</button>
     </div>
   </div>
 </div>
diff --git a/src/app/structure-list/components/modal-filter/modal-filter.component.spec.ts b/src/app/structure-list/components/modal-filter/modal-filter.component.spec.ts
index bf696da42..9c1ad3d14 100644
--- a/src/app/structure-list/components/modal-filter/modal-filter.component.spec.ts
+++ b/src/app/structure-list/components/modal-filter/modal-filter.component.spec.ts
@@ -1,6 +1,8 @@
 import { ComponentFixture, TestBed } from '@angular/core/testing';
 import { ReactiveFormsModule } from '@angular/forms';
+import { Category } from '../../models/category.model';
 import { Filter } from '../../models/filter.model';
+import { Module } from '../../models/module.model';
 
 import { ModalFilterComponent } from './modal-filter.component';
 
@@ -25,9 +27,65 @@ describe('ModalFilterComponent', () => {
     expect(component).toBeTruthy();
   });
 
-  it('should emit filters', () => {
+  it('should emit modules', () => {
+    const modules: Module[] = [
+      { id: 176, text: 'training', count: 3 },
+      { id: 173, text: 'training', count: 2 },
+      { id: 172, text: 'training', count: 2 },
+    ];
+    component.checkedModules = modules;
     spyOn(component.searchEvent, 'emit');
-    component.emitFilter();
+    component.emitModules();
     expect(component.searchEvent.emit).toHaveBeenCalled();
+    expect(component.searchEvent.emit).toHaveBeenCalledWith(modules);
+  });
+  it('should return an index or -1', () => {
+    const modules: Module[] = [
+      { id: 176, text: 'training', count: 0 },
+      { id: 173, text: 'training', count: 0 },
+      { id: 172, text: 'training', count: 0 },
+    ];
+    component.checkedModules = modules;
+    const foundItem = component.getIndex(173, 'training');
+    const notFoundItem = component.getIndex(189, 'training');
+    expect(foundItem).toEqual(1);
+    expect(notFoundItem).toEqual(-1);
+  });
+  it('should add a module to checkedModule array', () => {
+    const modules: Module[] = [
+      { id: 176, text: 'training', count: 0 },
+      { id: 173, text: 'training', count: 0 },
+      { id: 172, text: 'training', count: 0 },
+    ];
+    component.checkedModules = modules;
+    const evt = { target: { checked: true, value: 175 } };
+    component.onCheckboxChange(evt, 'training');
+    expect(component.checkedModules.length).toEqual(4);
+  });
+  it('should remove a module to checkedModule array', () => {
+    const modules: Module[] = [
+      { id: 176, text: 'training', count: 0 },
+      { id: 173, text: 'training', count: 0 },
+      { id: 172, text: 'training', count: 0 },
+    ];
+    component.checkedModules = modules;
+    const evt = { target: { checked: false, value: 173 } };
+    component.onCheckboxChange(evt, 'training');
+    expect(component.checkedModules.length).toEqual(2);
+  });
+  it('should remove all modules checked from same modal, here morefilters', () => {
+    const modules: Module[] = [
+      { id: 176, text: 'morefilters', count: 0 },
+      { id: 173, text: 'morefilters', count: 0 },
+      { id: 172, text: 'morefilters', count: 0 },
+      { id: 179, text: 'training', count: 0 },
+      { id: 190, text: 'training', count: 0 },
+      { id: 167, text: 'training', count: 0 },
+    ];
+    component.checkedModules = modules;
+    const category: Category = { name: 'morefilters', modules: [modules[0], modules[1], modules[2]] };
+    component.categories = [category];
+    component.clearFilters();
+    expect(component.checkedModules.length).toEqual(3);
   });
 });
diff --git a/src/app/structure-list/components/modal-filter/modal-filter.component.ts b/src/app/structure-list/components/modal-filter/modal-filter.component.ts
index ff6050e60..2f46075fa 100644
--- a/src/app/structure-list/components/modal-filter/modal-filter.component.ts
+++ b/src/app/structure-list/components/modal-filter/modal-filter.component.ts
@@ -29,12 +29,12 @@ export class ModalFilterComponent implements OnInit {
   }
 
   // Return index of a specific module in array modules
-  private getIndex(id: number, categ: string): number {
+  public getIndex(id: number, categ: string): number {
     return this.checkedModules.findIndex((m: Module) => m.id === id && m.text === categ);
   }
 
   // Management of the checkbox event (Check / Uncheck)
-  private onCheckboxChange(event, categ: string): void {
+  public onCheckboxChange(event, categ: string): void {
     const checkValue: number = parseInt(event.target.value, 10);
     if (event.target.checked) {
       this.checkedModules.push(new Module(checkValue, categ));
@@ -47,7 +47,7 @@ export class ModalFilterComponent implements OnInit {
   }
 
   // Clear only filters in the current modal
-  private clearFilters(): void {
+  public clearFilters(): void {
     this.categories.forEach((categ: Category) => {
       categ.modules.forEach((module: Module) => {
         if (this.getIndex(module.id, categ.name) > -1) {
@@ -57,8 +57,8 @@ export class ModalFilterComponent implements OnInit {
     });
   }
 
-  // Sends an array containing all filters
-  public emitFilter(): void {
+  // Sends an array containing all modules
+  public emitModules(): void {
     this.searchEvent.emit(this.checkedModules);
   }
 }
diff --git a/src/app/structure-list/components/search/search.component.ts b/src/app/structure-list/components/search/search.component.ts
index 6db165c6c..5ad6e6a1f 100644
--- a/src/app/structure-list/components/search/search.component.ts
+++ b/src/app/structure-list/components/search/search.component.ts
@@ -23,12 +23,12 @@ export class SearchComponent implements OnInit {
   @Output() searchEvent = new EventEmitter();
 
   // Form search input
-  private searchForm: FormGroup;
+  public searchForm: FormGroup;
   // Modal variable
   public categories: Category[];
-  private modalTypeOpened: string;
+  public modalTypeOpened: string;
   // Checkbox variable
-  private checkedModulesFilter: Module[];
+  public checkedModulesFilter: Module[];
 
   ngOnInit(): void {
     // Will store the different categories
-- 
GitLab