From b8dcbe08d55ef156435226651178411dfebbe0dc Mon Sep 17 00:00:00 2001
From: dangapay <divin.angapay@lam.fr>
Date: Wed, 5 Oct 2022 16:13:02 +0200
Subject: [PATCH] test for
 [admin][instance][dataset][components][attribute][criteria]

---
 .../generate-option-list.component.spec.ts    |  70 +++++++++++
 .../attribute/criteria/index.spec.ts          |  16 +++
 .../criteria/option-list.component.spec.ts    |  83 +++++++++++++
 .../criteria/table-criteria.component.spec.ts |  37 ++++++
 .../criteria/tr-criteria.component.spec.ts    | 109 ++++++++++++++++++
 5 files changed, 315 insertions(+)
 create mode 100644 client/src/app/admin/instance/dataset/components/attribute/criteria/generate-option-list.component.spec.ts
 create mode 100644 client/src/app/admin/instance/dataset/components/attribute/criteria/index.spec.ts
 create mode 100644 client/src/app/admin/instance/dataset/components/attribute/criteria/option-list.component.spec.ts
 create mode 100644 client/src/app/admin/instance/dataset/components/attribute/criteria/table-criteria.component.spec.ts
 create mode 100644 client/src/app/admin/instance/dataset/components/attribute/criteria/tr-criteria.component.spec.ts

diff --git a/client/src/app/admin/instance/dataset/components/attribute/criteria/generate-option-list.component.spec.ts b/client/src/app/admin/instance/dataset/components/attribute/criteria/generate-option-list.component.spec.ts
new file mode 100644
index 00000000..876e067b
--- /dev/null
+++ b/client/src/app/admin/instance/dataset/components/attribute/criteria/generate-option-list.component.spec.ts
@@ -0,0 +1,70 @@
+/**
+ * This file is part of Anis Client.
+ *
+ * @copyright Laboratoire d'Astrophysique de Marseille / CNRS
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+import { TemplateRef } from '@angular/core';
+import { ComponentFixture, TestBed } from '@angular/core/testing';
+import { ReactiveFormsModule } from '@angular/forms';
+import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
+import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal';
+import { GenerateOptionListComponent } from './generate-option-list.component';
+
+describe('[admin][instance][dataset][components][attribute][criteria] GenerateOptionListComponent', () => {
+    let component: GenerateOptionListComponent;
+    let fixture: ComponentFixture<GenerateOptionListComponent>;
+    const modalServiceStub = {
+        show: jest.fn().mockReturnThis(),
+        hide: jest.fn()
+    };
+
+    beforeEach(() => {
+        TestBed.configureTestingModule({
+            declarations: [
+                GenerateOptionListComponent,
+            ],
+            providers: [
+                BsModalRef,
+                { provide: BsModalService, useValue: modalServiceStub }
+            ],
+            imports: [
+                BrowserAnimationsModule,
+                ReactiveFormsModule
+            ],
+        });
+        fixture = TestBed.createComponent(GenerateOptionListComponent);
+        component = fixture.componentInstance;
+        fixture.detectChanges();
+    });
+
+    it('should create the component', () => {
+        expect(component).toBeTruthy();
+    });
+
+    it('should call modalRef.show(template) and  loadAttributeDistinctList.emit', () => {
+        let template: TemplateRef<any> = null;
+        let spyOnModal = jest.spyOn(modalServiceStub, 'show');
+        let spyOnloadAttributeDistinctList = jest.spyOn(component.loadAttributeDistinctList, 'emit');
+        component.openModal(template);
+        expect(spyOnModal).toHaveBeenCalledTimes(1);
+        expect(spyOnModal).toHaveBeenCalledWith(template);
+        expect(spyOnloadAttributeDistinctList).toHaveBeenCalledTimes(1);
+    });
+    it('should emit attributeDistinctList and call  modalRef.hide()', () => {
+        let template: TemplateRef<any> = null;
+        let spyOnModal = jest.spyOn(modalServiceStub, 'hide');
+        let spyOngenerateOptionList = jest.spyOn(component.generateOptionList, 'emit');
+        let attributeDistinctList: string[] = ['test'];
+        component.attributeDistinctList = attributeDistinctList;
+        component.openModal(template);
+        component.generate();
+        expect(spyOngenerateOptionList).toHaveBeenCalledTimes(1);
+        expect(spyOngenerateOptionList).toHaveBeenCalledWith(attributeDistinctList);
+        expect(spyOnModal).toHaveBeenCalledTimes(1);
+
+    });
+});
diff --git a/client/src/app/admin/instance/dataset/components/attribute/criteria/index.spec.ts b/client/src/app/admin/instance/dataset/components/attribute/criteria/index.spec.ts
new file mode 100644
index 00000000..aa433ed0
--- /dev/null
+++ b/client/src/app/admin/instance/dataset/components/attribute/criteria/index.spec.ts
@@ -0,0 +1,16 @@
+/**
+ * This file is part of Anis Client.
+ *
+ * @copyright Laboratoire d'Astrophysique de Marseille / CNRS
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+import { criteriaComponents } from './index';
+
+describe('[admin][instance][dataset][components][attribute][criteria] index', () => {
+    it('Test criteria index components', () => {
+        expect(criteriaComponents.length).toEqual(5);
+    });
+});
diff --git a/client/src/app/admin/instance/dataset/components/attribute/criteria/option-list.component.spec.ts b/client/src/app/admin/instance/dataset/components/attribute/criteria/option-list.component.spec.ts
new file mode 100644
index 00000000..91cfaffb
--- /dev/null
+++ b/client/src/app/admin/instance/dataset/components/attribute/criteria/option-list.component.spec.ts
@@ -0,0 +1,83 @@
+/**
+ * This file is part of Anis Client.
+ *
+ * @copyright Laboratoire d'Astrophysique de Marseille / CNRS
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+import { ComponentFixture, TestBed } from '@angular/core/testing';
+import { ReactiveFormsModule, UntypedFormArray, UntypedFormControl, UntypedFormGroup, Validators } from '@angular/forms';
+import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
+import { OptionListComponent } from './option-list.component';
+
+describe('[admin][instance][dataset][components][attribute][criteria] OptionListComponent', () => {
+    let component: OptionListComponent;
+    let fixture: ComponentFixture<OptionListComponent>;
+    let newOptionFormGroup: UntypedFormGroup = new UntypedFormGroup({
+        label: new UntypedFormControl('', [Validators.required]),
+        value: new UntypedFormControl('', [Validators.required]),
+        display: new UntypedFormControl('', [Validators.required]),
+    });
+
+    beforeEach(() => {
+        TestBed.configureTestingModule({
+            declarations: [
+                OptionListComponent
+            ],
+            imports: [
+                BrowserAnimationsModule,
+                ReactiveFormsModule
+            ],
+        });
+        fixture = TestBed.createComponent(OptionListComponent);
+        component = fixture.componentInstance;
+        component.optionList = [{ label: '', display: 10, value: null }];
+        component.form = new UntypedFormArray([]);
+        fixture.detectChanges();
+    });
+
+    it('should create the component', () => {
+        expect(component).toBeTruthy();
+    });
+    it('buildFormGroup() return an instance of UntypedFormGroup', () => {
+        let result = component.buildFormGroup();
+        expect(result).toBeInstanceOf(UntypedFormGroup);
+    });
+    it('addOption() should add newOptionFormGroup in form array , call buildFormGroup() and form.markAsDirty()', () => {
+        component.form = new UntypedFormArray([]);
+        component.newOptionFormGroup = newOptionFormGroup;
+        let spyOnBuildFormGroup = jest.spyOn(component, 'buildFormGroup')
+        let spyOnFormDirty = jest.spyOn(component.form, 'markAsDirty')
+        expect(component.form.controls.length).toEqual(0);
+        component.addOption();
+        expect(component.form.controls.length).toEqual(1);
+        expect(spyOnBuildFormGroup).toHaveBeenCalledTimes(1);
+        expect(spyOnFormDirty).toHaveBeenCalledTimes(1);
+
+    });
+    it('removeOption(index: number) should remove the element on the index in the form array and call form.markAsDirty()', () => {
+        component.form = new UntypedFormArray([newOptionFormGroup]);
+        let spyOnForm = jest.spyOn(component.form, 'markAsDirty')
+        expect(component.form.controls.length).toEqual(1);
+        component.removeOption(0);
+        expect(component.form.controls.length).toEqual(0);
+        expect(spyOnForm).toHaveBeenCalledTimes(1);
+    });
+    it('generateOptionList(attributeDistinctList: string[]) should call form.clear()', () => {
+        component.form = new UntypedFormArray([]);
+        let spyOnFormClear = jest.spyOn(component.form, 'clear');
+        let spyOnFormDirty = jest.spyOn(component.form, 'markAsDirty');
+        let attributeDistinctList: string[] = ['test1', 'test2', 'test3'];
+        expect(component.form.controls.length).toEqual(0);
+        component.generateOptionList(attributeDistinctList);
+        expect(spyOnFormClear).toHaveBeenCalledTimes(1);
+        expect(component.form.controls.length).toEqual(attributeDistinctList.length);
+        expect(spyOnFormDirty).toHaveBeenCalledTimes(1);
+
+    });
+
+
+});
+
diff --git a/client/src/app/admin/instance/dataset/components/attribute/criteria/table-criteria.component.spec.ts b/client/src/app/admin/instance/dataset/components/attribute/criteria/table-criteria.component.spec.ts
new file mode 100644
index 00000000..ed46ab84
--- /dev/null
+++ b/client/src/app/admin/instance/dataset/components/attribute/criteria/table-criteria.component.spec.ts
@@ -0,0 +1,37 @@
+/**
+ * This file is part of Anis Client.
+ *
+ * @copyright Laboratoire d'Astrophysique de Marseille / CNRS
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+import { ComponentFixture, TestBed } from '@angular/core/testing';
+import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
+import { TableCriteriaComponent } from './table-criteria.component';
+
+describe('[admin][instance][dataset][components][attribute][criteria] TableCriteriaComponent', () => {
+    let component: TableCriteriaComponent;
+    let fixture: ComponentFixture<TableCriteriaComponent>;
+
+    beforeEach(() => {
+        TestBed.configureTestingModule({
+            declarations: [
+                TableCriteriaComponent,
+            ],
+            providers: [],
+            imports: [
+                BrowserAnimationsModule,
+            ],
+        });
+        fixture = TestBed.createComponent(TableCriteriaComponent);
+        component = fixture.componentInstance;
+        fixture.detectChanges();
+    });
+
+    it('should create the component', () => {
+        expect(component).toBeTruthy();
+    });
+
+});
diff --git a/client/src/app/admin/instance/dataset/components/attribute/criteria/tr-criteria.component.spec.ts b/client/src/app/admin/instance/dataset/components/attribute/criteria/tr-criteria.component.spec.ts
new file mode 100644
index 00000000..32184216
--- /dev/null
+++ b/client/src/app/admin/instance/dataset/components/attribute/criteria/tr-criteria.component.spec.ts
@@ -0,0 +1,109 @@
+/**
+ * This file is part of Anis Client.
+ *
+ * @copyright Laboratoire d'Astrophysique de Marseille / CNRS
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+import { ComponentFixture, TestBed } from '@angular/core/testing';
+import { ReactiveFormsModule, UntypedFormArray, UntypedFormControl, UntypedFormGroup, Validators } from '@angular/forms';
+import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
+import { Attribute } from 'src/app/metamodel/models';
+import { TrCriteriaComponent } from './tr-criteria.component';
+
+describe('[admin][instance][dataset][components][attribute][criteria] TrCriteriaComponent', () => {
+    let component: TrCriteriaComponent;
+    let fixture: ComponentFixture<TrCriteriaComponent>;
+    let form;
+    let attribute: Attribute;
+
+    beforeEach(() => {
+        TestBed.configureTestingModule({
+            declarations: [
+                TrCriteriaComponent
+            ],
+            imports: [
+                BrowserAnimationsModule,
+                ReactiveFormsModule
+            ],
+        });
+        fixture = TestBed.createComponent(TrCriteriaComponent);
+        component = fixture.componentInstance;
+
+    });
+    beforeEach(() => {
+        form = new UntypedFormGroup({
+            name: new UntypedFormControl({ value: '', disabled: true }),
+            type: new UntypedFormControl({ value: '', disabled: true }),
+            id_criteria_family: new UntypedFormControl(''),
+            search_type: new UntypedFormControl(''),
+            operator: new UntypedFormControl(''),
+            dynamic_operator: new UntypedFormControl(),
+            min: new UntypedFormControl(''),
+            max: new UntypedFormControl(''),
+            options: new UntypedFormArray([]),
+            placeholder_min: new UntypedFormControl(''),
+            placeholder_max: new UntypedFormControl(''),
+            criteria_display: new UntypedFormControl('')
+        });
+        component.attribute = { ...attribute, name: 'test', id_criteria_family: 0, options: [] };
+        fixture.detectChanges();
+    })
+
+    it('should create the component', () => {
+        expect(component).toBeTruthy();
+    });
+    it('criteriaFamilyOnChange() should set id_criteria_family to null and call searchTypeOnChange()', () => {
+        let spy = jest.spyOn(component, 'searchTypeOnChange');
+        component.form = form;
+        expect(form.controls.id_criteria_family.value).toEqual('');
+        component.criteriaFamilyOnChange();
+        expect(form.controls.id_criteria_family.value).toEqual(null);
+        expect(spy).toHaveBeenCalledTimes(1);
+
+    });
+    it('searchTypeOnChange() should set search_type to null and call operatorOnChange()', () => {
+        let spy = jest.spyOn(component, 'operatorOnChange');
+        component.form = form;
+        expect(form.controls.search_type.value).toEqual('');
+        component.searchTypeOnChange();
+        expect(form.controls.search_type.value).toEqual(null);
+        expect(spy).toHaveBeenCalledTimes(1);
+
+    });
+    it('should set operator, min, max, palceholder_min, palceholder_max to null and call clear method on optionsFormArray', () => {
+        let spy = jest.spyOn(component.optionsFormArray, 'clear');
+        component.form = form;
+        expect(component.form.controls.operator.value).toEqual('');
+        component.operatorOnChange();
+        expect(component.form.controls.operator.value).toEqual(null);
+        expect(component.form.controls.min.value).toEqual(null);
+        expect(component.form.controls.max.value).toEqual(null);
+        expect(component.form.controls.placeholder_min.value).toEqual(null);
+        expect(component.form.controls.placeholder_max.value).toEqual(null);
+        expect(spy).toHaveBeenCalledTimes(1)
+
+    });
+    it('getMinValuePlaceholder(searchType: string) should return Default min value (optional)', () => {
+        let searchType: string = 'between-date';
+        let result = component.getMinValuePlaceholder(searchType);
+        expect(result).toEqual('Default min value (optional)')
+    });
+    it('getMinValuePlaceholder(searchType: string) should return Default value (optional)', () => {
+        let searchType: string = 'test';
+        let result = component.getMinValuePlaceholder(searchType);
+        expect(result).toEqual('Default value (optional)')
+    });
+    it('submit() should emit component.attribute and component.form.value', () => {
+        let spy = jest.spyOn(component.save, 'emit');
+        component.submit();
+        expect(spy).toHaveBeenCalledTimes(1);
+        expect(spy).toHaveBeenCalledWith({ ...component.attribute, ...component.form.value })
+    });
+
+
+
+});
+
-- 
GitLab