From 2bf2d9755d695a375056c01277bcbde0c6749e92 Mon Sep 17 00:00:00 2001
From: Tifenn Guillas <tifenn.guillas@gmail.com>
Date: Mon, 4 Oct 2021 15:02:57 +0200
Subject: [PATCH] Tests on search result components => DONE

---
 .../result/datatable-tab.component.spec.ts    |  49 ++
 .../result/datatable-tab.component.ts         |   8 +-
 .../result/download.component.spec.ts         | 419 ++++++++++++++++++
 .../components/result/download.component.ts   |  37 +-
 4 files changed, 501 insertions(+), 12 deletions(-)
 create mode 100644 client/src/app/instance/search/components/result/datatable-tab.component.spec.ts
 create mode 100644 client/src/app/instance/search/components/result/download.component.spec.ts

diff --git a/client/src/app/instance/search/components/result/datatable-tab.component.spec.ts b/client/src/app/instance/search/components/result/datatable-tab.component.spec.ts
new file mode 100644
index 00000000..7705a42b
--- /dev/null
+++ b/client/src/app/instance/search/components/result/datatable-tab.component.spec.ts
@@ -0,0 +1,49 @@
+import { Component, Input } from '@angular/core';
+import { ComponentFixture, TestBed } from '@angular/core/testing';
+import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
+
+import { AccordionModule } from 'ngx-bootstrap/accordion';
+
+import { DatatableTabComponent } from './datatable-tab.component';
+import { Attribute, Dataset, Instance } from '../../../../metamodel/models';
+import { SearchQueryParams } from '../../../store/models';
+import { DatasetByNamePipe } from '../../../../shared/pipes/dataset-by-name.pipe';
+
+describe('[Instance][Search][Component][Result] DatatableTabComponent', () => {
+    @Component({ selector: 'app-datatable', template: '' })
+    class DatatableStubComponent {
+        @Input() dataset: Dataset;
+        @Input() instance: Instance;
+        @Input() attributeList: Attribute[];
+        @Input() outputList: number[];
+        @Input() queryParams: SearchQueryParams;
+        @Input() dataLength: number;
+        @Input() data: any[];
+        @Input() dataIsLoading: boolean;
+        @Input() dataIsLoaded: boolean;
+        @Input() selectedData: any[] = [];
+    }
+
+    let component: DatatableTabComponent;
+    let fixture: ComponentFixture<DatatableTabComponent>;
+
+    beforeEach(() => {
+        TestBed.configureTestingModule({
+            declarations: [
+                DatatableTabComponent,
+                DatatableStubComponent,
+                DatasetByNamePipe
+            ],
+            imports: [
+                AccordionModule.forRoot(),
+                BrowserAnimationsModule
+            ]
+        });
+        fixture = TestBed.createComponent(DatatableTabComponent);
+        component = fixture.componentInstance;
+    });
+
+    it('should create the component', () => {
+        expect(component).toBeTruthy();
+    });
+});
diff --git a/client/src/app/instance/search/components/result/datatable-tab.component.ts b/client/src/app/instance/search/components/result/datatable-tab.component.ts
index a276dec4..ff703b86 100644
--- a/client/src/app/instance/search/components/result/datatable-tab.component.ts
+++ b/client/src/app/instance/search/components/result/datatable-tab.component.ts
@@ -12,15 +12,15 @@ import { ChangeDetectionStrategy, Component, EventEmitter, Input, Output } from
 import { Instance, Attribute, Dataset } from 'src/app/metamodel/models';
 import { Pagination, SearchQueryParams } from 'src/app/instance/store/models';
 
+/**
+ * @class
+ * @classdesc Search result datatable tab component.
+ */
 @Component({
     selector: 'app-datatable-tab',
     templateUrl: 'datatable-tab.component.html',
     changeDetection: ChangeDetectionStrategy.OnPush
 })
-/**
- * @class
- * @classdesc Search result datatable tab component.
- */
 export class DatatableTabComponent {
     @Input() datasetSelected: string;
     @Input() instance: Instance;
diff --git a/client/src/app/instance/search/components/result/download.component.spec.ts b/client/src/app/instance/search/components/result/download.component.spec.ts
new file mode 100644
index 00000000..840d91af
--- /dev/null
+++ b/client/src/app/instance/search/components/result/download.component.spec.ts
@@ -0,0 +1,419 @@
+import { ComponentFixture, TestBed } from '@angular/core/testing';
+import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
+import { HttpClientTestingModule } from '@angular/common/http/testing';
+
+import { AccordionModule } from 'ngx-bootstrap/accordion';
+
+import { DownloadComponent } from './download.component';
+import { AppConfigService } from '../../../../app-config.service';
+import { FieldCriterion } from '../../../store/models/criterion';
+
+describe('[Instance][Search][Component][Result] DownloadComponent', () => {
+    let component: DownloadComponent;
+    let fixture: ComponentFixture<DownloadComponent>;
+    let appConfigServiceStub = new AppConfigService();
+
+    beforeEach(() => {
+        TestBed.configureTestingModule({
+            declarations: [DownloadComponent],
+            imports: [
+                HttpClientTestingModule,
+                AccordionModule.forRoot(),
+                BrowserAnimationsModule
+            ],
+            providers: [{ provide: AppConfigService, useValue: appConfigServiceStub }]
+        });
+        fixture = TestBed.createComponent(DownloadComponent);
+        component = fixture.componentInstance;
+    });
+
+    it('should create the component', () => {
+        expect(component).toBeTruthy();
+    });
+
+    it('#isDownloadActivated() should return if download section has to be enabled or not', () => {
+        component.datasetList = [
+            {
+                name: 'myDataset',
+                table_ref: 'table',
+                label: 'my dataset',
+                description: 'This is my dataset',
+                display: 1,
+                data_path: '/path',
+                survey_name: 'mySurvey',
+                id_dataset_family: 1,
+                public: true,
+                full_data_path: '/data/path',
+                config: {
+                    images: ['image1'],
+                    survey: {
+                        survey_enabled: true,
+                        survey_label: 'More about this survey'
+                    },
+                    cone_search: {
+                        cone_search_enabled: true,
+                        cone_search_opened: true,
+                        cone_search_column_ra: 1,
+                        cone_search_column_dec: 2,
+                        cone_search_plot_enabled: true,
+                        cone_search_sdss_enabled: true,
+                        cone_search_sdss_display: 1,
+                        cone_search_background: [{ id: 1, enabled: true, display: 1 }]
+                    },
+                    download: {
+                        download_enabled: true,
+                        download_opened: true,
+                        download_csv: true,
+                        download_ascii: true,
+                        download_vo: true,
+                        download_archive: true
+                    },
+                    summary: {
+                        summary_enabled: false,
+                        summary_opened: false
+                    },
+                    server_link: {
+                        server_link_enabled: false,
+                        server_link_opened: true
+                    },
+                    samp: {
+                        samp_enabled: false,
+                        samp_opened: false
+                    },
+                    datatable: {
+                        datatable_enabled: true,
+                        datatable_opened: true,
+                        datatable_selectable_rows: true
+                    }
+                }
+            },
+            {
+                name: 'anotherDataset',
+                table_ref: 'table',
+                label: 'another dataset',
+                description: 'This is another dataset',
+                display: 1,
+                data_path: '/path',
+                survey_name: 'mySurvey',
+                id_dataset_family: 1,
+                public: true,
+                full_data_path: '/data/path',
+                config: {
+                    images: ['image1'],
+                    survey: {
+                        survey_enabled: true,
+                        survey_label: 'More about this survey'
+                    },
+                    cone_search: {
+                        cone_search_enabled: true,
+                        cone_search_opened: true,
+                        cone_search_column_ra: 1,
+                        cone_search_column_dec: 2,
+                        cone_search_plot_enabled: true,
+                        cone_search_sdss_enabled: true,
+                        cone_search_sdss_display: 1,
+                        cone_search_background: [{ id: 1, enabled: true, display: 1 }]
+                    },
+                    download: {
+                        download_enabled: false,
+                        download_opened: false,
+                        download_csv: false,
+                        download_ascii: false,
+                        download_vo: false,
+                        download_archive: false
+                    },
+                    summary: {
+                        summary_enabled: true,
+                        summary_opened: true
+                    },
+                    server_link: {
+                        server_link_enabled: true,
+                        server_link_opened: true
+                    },
+                    samp: {
+                        samp_enabled: true,
+                        samp_opened: true
+                    },
+                    datatable: {
+                        datatable_enabled: true,
+                        datatable_opened: true,
+                        datatable_selectable_rows: true
+                    }
+                }
+            }
+        ];
+        component.datasetSelected = 'myDataset';
+        expect(component.isDownloadActivated()).toBeTruthy();
+        component.datasetSelected = 'anotherDataset';
+        expect(component.isDownloadActivated()).toBeFalsy();
+    });
+
+    it('#isDownloadOpened() should return if download tab has to be opened or not', () => {
+        component.datasetList = [
+            {
+                name: 'myDataset',
+                table_ref: 'table',
+                label: 'my dataset',
+                description: 'This is my dataset',
+                display: 1,
+                data_path: '/path',
+                survey_name: 'mySurvey',
+                id_dataset_family: 1,
+                public: true,
+                full_data_path: '/data/path',
+                config: {
+                    images: ['image1'],
+                    survey: {
+                        survey_enabled: true,
+                        survey_label: 'More about this survey'
+                    },
+                    cone_search: {
+                        cone_search_enabled: true,
+                        cone_search_opened: true,
+                        cone_search_column_ra: 1,
+                        cone_search_column_dec: 2,
+                        cone_search_plot_enabled: true,
+                        cone_search_sdss_enabled: true,
+                        cone_search_sdss_display: 1,
+                        cone_search_background: [{ id: 1, enabled: true, display: 1 }]
+                    },
+                    download: {
+                        download_enabled: true,
+                        download_opened: true,
+                        download_csv: true,
+                        download_ascii: true,
+                        download_vo: true,
+                        download_archive: true
+                    },
+                    summary: {
+                        summary_enabled: false,
+                        summary_opened: false
+                    },
+                    server_link: {
+                        server_link_enabled: false,
+                        server_link_opened: false
+                    },
+                    samp: {
+                        samp_enabled: false,
+                        samp_opened: false
+                    },
+                    datatable: {
+                        datatable_enabled: true,
+                        datatable_opened: true,
+                        datatable_selectable_rows: true
+                    }
+                }
+            },
+            {
+                name: 'anotherDataset',
+                table_ref: 'table',
+                label: 'another dataset',
+                description: 'This is another dataset',
+                display: 1,
+                data_path: '/path',
+                survey_name: 'mySurvey',
+                id_dataset_family: 1,
+                public: true,
+                full_data_path: '/data/path',
+                config: {
+                    images: ['image1'],
+                    survey: {
+                        survey_enabled: true,
+                        survey_label: 'More about this survey'
+                    },
+                    cone_search: {
+                        cone_search_enabled: true,
+                        cone_search_opened: true,
+                        cone_search_column_ra: 1,
+                        cone_search_column_dec: 2,
+                        cone_search_plot_enabled: true,
+                        cone_search_sdss_enabled: true,
+                        cone_search_sdss_display: 1,
+                        cone_search_background: [{ id: 1, enabled: true, display: 1 }]
+                    },
+                    download: {
+                        download_enabled: false,
+                        download_opened: false,
+                        download_csv: false,
+                        download_ascii: false,
+                        download_vo: false,
+                        download_archive: false
+                    },
+                    summary: {
+                        summary_enabled: true,
+                        summary_opened: true
+                    },
+                    server_link: {
+                        server_link_enabled: true,
+                        server_link_opened: true
+                    },
+                    samp: {
+                        samp_enabled: true,
+                        samp_opened: true
+                    },
+                    datatable: {
+                        datatable_enabled: true,
+                        datatable_opened: true,
+                        datatable_selectable_rows: true
+                    }
+                }
+            }
+        ];
+        component.datasetSelected = 'myDataset';
+        expect(component.isDownloadOpened()).toBeTruthy();
+        component.datasetSelected = 'anotherDataset';
+        expect(component.isDownloadOpened()).toBeFalsy();
+    });
+
+    it('#getConfigDownloadResultFormat() should return if download button for the given format has to be displayed', () => {
+        component.datasetList = [
+            {
+                name: 'myDataset',
+                table_ref: 'table',
+                label: 'my dataset',
+                description: 'This is my dataset',
+                display: 1,
+                data_path: '/path',
+                survey_name: 'mySurvey',
+                id_dataset_family: 1,
+                public: true,
+                full_data_path: '/data/path',
+                config: {
+                    images: ['image1'],
+                    survey: {
+                        survey_enabled: true,
+                        survey_label: 'More about this survey'
+                    },
+                    cone_search: {
+                        cone_search_enabled: true,
+                        cone_search_opened: true,
+                        cone_search_column_ra: 1,
+                        cone_search_column_dec: 2,
+                        cone_search_plot_enabled: true,
+                        cone_search_sdss_enabled: true,
+                        cone_search_sdss_display: 1,
+                        cone_search_background: [{ id: 1, enabled: true, display: 1 }]
+                    },
+                    download: {
+                        download_enabled: true,
+                        download_opened: true,
+                        download_csv: true,
+                        download_ascii: false,
+                        download_vo: true,
+                        download_archive: true
+                    },
+                    summary: {
+                        summary_enabled: false,
+                        summary_opened: false
+                    },
+                    server_link: {
+                        server_link_enabled: false,
+                        server_link_opened: false
+                    },
+                    samp: {
+                        samp_enabled: false,
+                        samp_opened: false
+                    },
+                    datatable: {
+                        datatable_enabled: true,
+                        datatable_opened: true,
+                        datatable_selectable_rows: true
+                    }
+                }
+            },
+            {
+                name: 'anotherDataset',
+                table_ref: 'table',
+                label: 'another dataset',
+                description: 'This is another dataset',
+                display: 1,
+                data_path: '/path',
+                survey_name: 'mySurvey',
+                id_dataset_family: 1,
+                public: true,
+                full_data_path: '/data/path',
+                config: {
+                    images: ['image1'],
+                    survey: {
+                        survey_enabled: true,
+                        survey_label: 'More about this survey'
+                    },
+                    cone_search: {
+                        cone_search_enabled: true,
+                        cone_search_opened: true,
+                        cone_search_column_ra: 1,
+                        cone_search_column_dec: 2,
+                        cone_search_plot_enabled: true,
+                        cone_search_sdss_enabled: true,
+                        cone_search_sdss_display: 1,
+                        cone_search_background: [{ id: 1, enabled: true, display: 1 }]
+                    },
+                    download: {
+                        download_enabled: false,
+                        download_opened: false,
+                        download_csv: false,
+                        download_ascii: false,
+                        download_vo: false,
+                        download_archive: false
+                    },
+                    summary: {
+                        summary_enabled: true,
+                        summary_opened: true
+                    },
+                    server_link: {
+                        server_link_enabled: true,
+                        server_link_opened: true
+                    },
+                    samp: {
+                        samp_enabled: true,
+                        samp_opened: true
+                    },
+                    datatable: {
+                        datatable_enabled: true,
+                        datatable_opened: true,
+                        datatable_selectable_rows: true
+                    }
+                }
+            }
+        ];
+        component.datasetSelected = 'myDataset';
+        expect(component.getConfigDownloadResultFormat('download_csv')).toBeTruthy();
+        expect(component.getConfigDownloadResultFormat('download_ascii')).toBeFalsy();
+    });
+
+    it('#getUrl(format) should construct url with format parameter', () => {
+        appConfigServiceStub.apiUrl = 'http://test.com';
+        component.datasetSelected = 'myDataset';
+        component.outputList = [1, 2, 3];
+        component.criteriaList = [];
+        expect(component.getUrl('csv')).toBe('http://test.com/search/myDataset?a=1;2;3&f=csv');
+        component.criteriaList = [
+            {'id':1,'type':'field','operator':'eq','value':'one'} as FieldCriterion,
+            {'id':2,'type':'field','operator':'eq','value':'two'} as FieldCriterion
+        ];
+        component.coneSearch = { ra: 4, dec: 5, radius: 6 };
+        expect(component.getUrl('csv')).toBe('http://test.com/search/myDataset?a=1;2;3&c=1::eq::one;2::eq::two&cs=4:5:6&f=csv');
+    });
+
+    it('#getUrlArchive() should construct url to access to archive', () => {
+        appConfigServiceStub.apiUrl = 'http://test.com';
+        component.datasetSelected = 'myDataset';
+        component.outputList = [1, 2, 3];
+        component.criteriaList = [
+            {'id':1,'type':'field','operator':'eq','value':'one'} as FieldCriterion,
+            {'id':2,'type':'field','operator':'eq','value':'two'} as FieldCriterion
+        ];
+        component.coneSearch = { ra: 4, dec: 5, radius: 6 };
+        expect(component.getUrlArchive()).toBe('http://test.com/archive/myDataset?a=1;2;3&c=1::eq::one;2::eq::two&cs=4:5:6');
+    });
+
+    it('#broadcastVotable() should raise broadcast event when clicked', () => {
+        appConfigServiceStub.apiUrl = 'http://test.com';
+        component.datasetSelected = 'myDataset';
+        component.outputList = [1, 2, 3];
+        component.criteriaList = [];
+        component.broadcast.subscribe((event: string) => expect(event).toEqual('toto'));
+        component.broadcastVotable();
+
+    });
+});
diff --git a/client/src/app/instance/search/components/result/download.component.ts b/client/src/app/instance/search/components/result/download.component.ts
index b0498b15..88709792 100644
--- a/client/src/app/instance/search/components/result/download.component.ts
+++ b/client/src/app/instance/search/components/result/download.component.ts
@@ -15,16 +15,14 @@ import { Dataset } from 'src/app/metamodel/models';
 import { getHost as host } from 'src/app/shared/utils';
 import { AppConfigService } from 'src/app/app-config.service';
 
-@Component({
-    selector: 'app-download',
-    templateUrl: 'download.component.html'
-})
 /**
  * @class
  * @classdesc Search result download component.
- *
- * @implements OnInit
  */
+@Component({
+    selector: 'app-download',
+    templateUrl: 'download.component.html'
+})
 export class DownloadComponent {
     @Input() datasetSelected: string;
     @Input() datasetList: Dataset[];
@@ -37,11 +35,21 @@ export class DownloadComponent {
 
     constructor(private appConfig: AppConfigService, private http: HttpClient) { }
 
+    /**
+     * Checks if download tab has to be display.
+     *
+     * @return boolean
+     */
     isDownloadActivated(): boolean {
         const dataset = this.datasetList.find(dataset => dataset.name === this.datasetSelected);
         return dataset.config.download.download_enabled;
     }
 
+    /**
+     * Checks if download tab has to be open.
+     *
+     * @return boolean
+     */
     isDownloadOpened(): boolean {
         const dataset = this.datasetList.find(dataset => dataset.name === this.datasetSelected);
         return dataset.config.download.download_opened;
@@ -78,6 +86,11 @@ export class DownloadComponent {
         return query;
     }
 
+    /**
+     * Returns URL to download archive.
+     *
+     * @return boolean
+     */
     getUrlArchive(): string {
         let query: string = host(this.appConfig.apiUrl) + '/archive/' + this.datasetSelected + '?a=' + this.outputList.join(';');
         if (this.criteriaList.length > 0) {
@@ -88,12 +101,20 @@ export class DownloadComponent {
         }
         return query;
     }
-    
+
+    /**
+     * Emits event to  action to broadcast data.
+     *
+     * @fires EventEmitter<string>
+     */
     broadcastVotable(): void {
         this.broadcast.emit(this.getUrl('votable'));
     }
 
-    click(event, href, extension) {
+    /**
+     * Allows to download file.
+     */
+    click(event, href, extension): void {
         event.preventDefault();
 
         this.http.get(href, {responseType: "blob"}).subscribe(
-- 
GitLab