From 517fa40b23402cdf72e8297b13d0ef4dd9c1509e Mon Sep 17 00:00:00 2001
From: dangapay <divin.angapay@lam.fr>
Date: Fri, 21 Oct 2022 13:43:08 +0200
Subject: [PATCH] add test for admin/instance/dataset/components/file

---
 .../file/add-file.component.spec.ts           | 56 ++++++++++++++
 .../file/edit-file.component.spec.ts          | 55 ++++++++++++++
 .../file/file-form.component.spec.ts          | 74 +++++++++++++++++++
 .../file/file-list.component.spec.ts          | 38 ++++++++++
 .../dataset/components/file/index.spec.ts     | 19 +++++
 5 files changed, 242 insertions(+)
 create mode 100644 client/src/app/admin/instance/dataset/components/file/add-file.component.spec.ts
 create mode 100644 client/src/app/admin/instance/dataset/components/file/edit-file.component.spec.ts
 create mode 100644 client/src/app/admin/instance/dataset/components/file/file-form.component.spec.ts
 create mode 100644 client/src/app/admin/instance/dataset/components/file/file-list.component.spec.ts
 create mode 100644 client/src/app/admin/instance/dataset/components/file/index.spec.ts

diff --git a/client/src/app/admin/instance/dataset/components/file/add-file.component.spec.ts b/client/src/app/admin/instance/dataset/components/file/add-file.component.spec.ts
new file mode 100644
index 00000000..86e3eb4d
--- /dev/null
+++ b/client/src/app/admin/instance/dataset/components/file/add-file.component.spec.ts
@@ -0,0 +1,56 @@
+/**
+ * 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 { AddFileComponent } from './add-file.component';
+
+describe('[admin][instance][dataset][components][file] AddFileComponent', () => {
+    let component: AddFileComponent;
+    let fixture: ComponentFixture<AddFileComponent>;
+    const modalServiceStub = {
+        show: jest.fn(),
+    };
+
+    beforeEach(() => {
+        TestBed.configureTestingModule({
+            declarations: [
+                AddFileComponent,
+            ],
+            providers: [
+                BsModalRef,
+                { provide: BsModalService, useValue: modalServiceStub }
+            ],
+            imports: [
+                BrowserAnimationsModule,
+                ReactiveFormsModule
+            ],
+        });
+        fixture = TestBed.createComponent(AddFileComponent);
+        component = fixture.componentInstance;
+        fixture.detectChanges();
+    });
+
+    it('should create the component', () => {
+        expect(component).toBeTruthy();
+    });
+
+    it('should call modalRef.show(template)', () => {
+        let template: TemplateRef<any> = null;
+        let spy = jest.spyOn(modalServiceStub, 'show');
+        component.openModal(template);
+        expect(spy).toHaveBeenCalledTimes(1);
+        expect(spy).toHaveBeenCalledWith(template);
+    });
+
+});
+
diff --git a/client/src/app/admin/instance/dataset/components/file/edit-file.component.spec.ts b/client/src/app/admin/instance/dataset/components/file/edit-file.component.spec.ts
new file mode 100644
index 00000000..f88d9729
--- /dev/null
+++ b/client/src/app/admin/instance/dataset/components/file/edit-file.component.spec.ts
@@ -0,0 +1,55 @@
+/**
+ * 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 { EditFileComponent } from './edit-file.component';
+describe('[admin][instance][dataset][components][file] EditFileComponent', () => {
+    let component: EditFileComponent;
+    let fixture: ComponentFixture<EditFileComponent>;
+    const modalServiceStub = {
+        show: jest.fn(),
+    };
+
+    beforeEach(() => {
+        TestBed.configureTestingModule({
+            declarations: [
+                EditFileComponent,
+            ],
+            providers: [
+                BsModalRef,
+                { provide: BsModalService, useValue: modalServiceStub }
+            ],
+            imports: [
+                BrowserAnimationsModule,
+                ReactiveFormsModule
+            ],
+        });
+        fixture = TestBed.createComponent(EditFileComponent);
+        component = fixture.componentInstance;
+        fixture.detectChanges();
+    });
+
+    it('should create the component', () => {
+        expect(component).toBeTruthy();
+    });
+
+    it('should call modalRef.show(template)', () => {
+        let template: TemplateRef<any> = null;
+        let spy = jest.spyOn(modalServiceStub, 'show');
+        component.openModal(template);
+        expect(spy).toHaveBeenCalledTimes(1);
+        expect(spy).toHaveBeenCalledWith(template);
+    });
+
+});
+
diff --git a/client/src/app/admin/instance/dataset/components/file/file-form.component.spec.ts b/client/src/app/admin/instance/dataset/components/file/file-form.component.spec.ts
new file mode 100644
index 00000000..b9a86948
--- /dev/null
+++ b/client/src/app/admin/instance/dataset/components/file/file-form.component.spec.ts
@@ -0,0 +1,74 @@
+/**
+ * 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 } from '@angular/forms';
+import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
+import { FileInfo } from 'src/app/admin/store/models';
+import { Dataset, Instance } from 'src/app/metamodel/models';
+import { FileFormComponent } from './file-form.component';
+
+describe('[admin][instance][dataset][components][file] FileFormComponent', () => {
+    let component: FileFormComponent;
+    let fixture: ComponentFixture<FileFormComponent>;
+
+    beforeEach(() => {
+        TestBed.configureTestingModule({
+            declarations: [
+                FileFormComponent,
+            ],
+            imports: [
+                BrowserAnimationsModule,
+                ReactiveFormsModule
+            ],
+        });
+        fixture = TestBed.createComponent(FileFormComponent);
+        component = fixture.componentInstance;
+        component.file = { id: 1, file_path: 'test', file_size: 1000, label: 'test', type: 'test' };
+        let instance: Instance;
+        let dataset: Dataset;
+        component.instance = { ...instance, data_path: 'test3' };
+        component.dataset = { ...dataset, data_path: 'test_dataset' }
+        fixture.detectChanges();
+    });
+
+    it('should create the component', () => {
+        expect(component).toBeTruthy();
+    });
+    it('onChangeFileSelect(path: string) should emit instance.data_path, dataset.data_path and path', () => {
+        let spy = jest.spyOn(component.loadRootDirectory, 'emit');
+        let path: string = 'test1';
+        component.onChangeFileSelect(path);
+        expect(spy).toHaveBeenCalledTimes(1);
+        expect(spy).toHaveBeenCalledWith(component.instance.data_path + component.dataset.data_path + path);
+    });
+    it(' onFileSelect(fileInfo: FileInfo) should set form.file_size to 2000', () => {
+
+        expect(component.form.controls.file_size.value).toEqual(1000);
+        let fileInfo: FileInfo = {mimetype: 'test',name: 'test',size : 2000, type: 'test'};
+        component.onFileSelect(fileInfo)
+        expect(component.form.controls.file_size.value).toEqual(2000);
+    });
+
+    it('submit() should emit file and form.getRawValue()', () => {
+        let spy = jest.spyOn(component.onSubmit, 'emit');
+        component.submit();
+        expect(spy).toHaveBeenCalledTimes(1);
+        expect(spy).toHaveBeenCalledWith({...component.file, ...component.form.getRawValue()});
+    } );
+    it('submit() should emit only form.getRawValue()', () => {
+        let spy = jest.spyOn(component.onSubmit, 'emit');
+        component.file = null;
+        component.submit();
+        expect(spy).toHaveBeenCalledTimes(1);
+        expect(spy).toHaveBeenCalledWith({...component.form.getRawValue()});
+    } )
+
+});
+
diff --git a/client/src/app/admin/instance/dataset/components/file/file-list.component.spec.ts b/client/src/app/admin/instance/dataset/components/file/file-list.component.spec.ts
new file mode 100644
index 00000000..3c2a5e87
--- /dev/null
+++ b/client/src/app/admin/instance/dataset/components/file/file-list.component.spec.ts
@@ -0,0 +1,38 @@
+/**
+ * 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 } from '@angular/forms';
+import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
+import { FileListComponent } from './file-list.component';
+
+describe('[admin][instance][dataset][components][file] FileListComponent', () => {
+    let component: FileListComponent;
+    let fixture: ComponentFixture<FileListComponent>;
+
+    beforeEach(() => {
+        TestBed.configureTestingModule({
+            declarations: [
+                FileListComponent
+            ],
+            imports: [
+                BrowserAnimationsModule,
+                ReactiveFormsModule
+            ],
+        });
+        fixture = TestBed.createComponent(FileListComponent);
+        component = fixture.componentInstance;
+    });
+
+    it('should create the component', () => {
+        expect(component).toBeTruthy();
+    });
+
+});
+
+
diff --git a/client/src/app/admin/instance/dataset/components/file/index.spec.ts b/client/src/app/admin/instance/dataset/components/file/index.spec.ts
new file mode 100644
index 00000000..dd63aec6
--- /dev/null
+++ b/client/src/app/admin/instance/dataset/components/file/index.spec.ts
@@ -0,0 +1,19 @@
+/**
+ * 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 { fileComponents } from './index';
+
+describe('[admin][instance][dataset][components][file] index', () => {
+    it('should test file index components', () => {
+        expect(fileComponents.length).toEqual(4);
+    });
+});
+
+
+
-- 
GitLab