From ee745038c47b0ca75ad518f7e714bf147608e458 Mon Sep 17 00:00:00 2001 From: Abdelmalek Date: Tue, 21 Jun 2022 11:32:27 +0200 Subject: [PATCH 1/5] fix(links): add excel extension and pdf mimeType --- libs/util/shared/src/lib/links/link-utils.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/libs/util/shared/src/lib/links/link-utils.ts b/libs/util/shared/src/lib/links/link-utils.ts index c81abbbe51..b03b14de38 100644 --- a/libs/util/shared/src/lib/links/link-utils.ts +++ b/libs/util/shared/src/lib/links/link-utils.ts @@ -11,7 +11,13 @@ export const FORMATS = { mimeTypes: ['text/csv', 'application/csv'], }, excel: { - extensions: ['xls', 'xlsx', 'ms-excel', 'openxmlformats-officedocument'], + extensions: [ + 'excel', + 'xls', + 'xlsx', + 'ms-excel', + 'openxmlformats-officedocument', + ], priority: 2, color: '#0f4395', mimeTypes: [ @@ -62,7 +68,7 @@ export const FORMATS = { extensions: ['pdf'], priority: 8, color: '#db544a', - mimeTypes: ['application/vnd.ms-excel'], + mimeTypes: ['application/pdf'], }, jpg: { extensions: ['jpg', 'jpeg', 'jfif', 'pjpeg', 'pjp'], From a95b512bb825036134a5ecf013c473c30b40fc1e Mon Sep 17 00:00:00 2001 From: Abdelmalek Date: Tue, 21 Jun 2022 20:00:41 +0200 Subject: [PATCH 2/5] test(links): add excel format to sort test --- .../data-downloads.component.spec.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/libs/feature/record/src/lib/data-downloads/data-downloads.component.spec.ts b/libs/feature/record/src/lib/data-downloads/data-downloads.component.spec.ts index 6b2dee46b1..dd8b358f44 100644 --- a/libs/feature/record/src/lib/data-downloads/data-downloads.component.spec.ts +++ b/libs/feature/record/src/lib/data-downloads/data-downloads.component.spec.ts @@ -297,6 +297,13 @@ describe('DataDownloadsComponent', () => { protocol: 'WWW:DOWNLOAD', url: 'https://www.ifremer.fr/surval_parametre_polygone.geojson', }, + { + description: 'excel data', + name: 'data.xls', + format: 'Excel', + protocol: 'WWW:DOWNLOAD', + url: 'https://www.ifremer.fr/data.excel', + }, ]) fixture.detectChanges() }) @@ -314,6 +321,13 @@ describe('DataDownloadsComponent', () => { protocol: 'WWW:DOWNLOAD', url: 'https://www.ifremer.fr/surval_parametre_point.csv', }, + { + description: 'excel data', + name: 'data.xls', + format: 'Excel', + protocol: 'WWW:DOWNLOAD', + url: 'https://www.ifremer.fr/data.excel', + }, { description: 'Lieu de surveillance (polygone)', name: 'surval_parametre_polygone.geojson', From 51c207ed9b4ab7ad8e80b6484e703096710284c1 Mon Sep 17 00:00:00 2001 From: Abdelmalek Date: Tue, 21 Jun 2022 20:21:08 +0200 Subject: [PATCH 3/5] fix(links): display link when format is unknown --- .../src/lib/downloads-list/downloads-list.component.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libs/ui/elements/src/lib/downloads-list/downloads-list.component.ts b/libs/ui/elements/src/lib/downloads-list/downloads-list.component.ts index 9c5985f7b4..8a183721be 100644 --- a/libs/ui/elements/src/lib/downloads-list/downloads-list.component.ts +++ b/libs/ui/elements/src/lib/downloads-list/downloads-list.component.ts @@ -54,6 +54,7 @@ export class DownloadsListComponent implements OnInit { ngOnInit(): void { this.processedLinks = this.formatLinks(this.links) + console.log(this.processedLinks) this.processedLinks = this.assignColor(this.processedLinks) this.processedLinks = this.isGeneratedFromWfs(this.processedLinks) this.filteredLinks = this.filterLinks(this.processedLinks) @@ -70,7 +71,7 @@ export class DownloadsListComponent implements OnInit { return links.map((link) => { return { ...link, - format: link.format.split(':').at(-1), + format: link.format ? link.format.split(':').at(-1) : '', } }) } From c16b90bbc8d5adaae810169480e430438382c746 Mon Sep 17 00:00:00 2001 From: Florent gravin Date: Wed, 22 Jun 2022 11:41:31 +0200 Subject: [PATCH 4/5] test(downloads-list): add unknown format link --- .../downloads-list.component.spec.ts | 53 ++++++++++++++++--- .../downloads-list.component.ts | 1 - .../shared/src/lib/links/link.fixtures.ts | 7 +++ 3 files changed, 54 insertions(+), 7 deletions(-) diff --git a/libs/ui/elements/src/lib/downloads-list/downloads-list.component.spec.ts b/libs/ui/elements/src/lib/downloads-list/downloads-list.component.spec.ts index 74bfde2f2a..47fa2fe3f7 100644 --- a/libs/ui/elements/src/lib/downloads-list/downloads-list.component.spec.ts +++ b/libs/ui/elements/src/lib/downloads-list/downloads-list.component.spec.ts @@ -1,19 +1,43 @@ -import { NO_ERRORS_SCHEMA } from '@angular/core' +import { + ChangeDetectionStrategy, + Component, + DebugElement, + Input, + NO_ERRORS_SCHEMA, +} from '@angular/core' import { ComponentFixture, TestBed } from '@angular/core/testing' -import { LinkHelperService } from '@geonetwork-ui/util/shared' +import { By } from '@angular/platform-browser' +import { + LinkHelperService, + MetadataLinkValid, +} from '@geonetwork-ui/util/shared' import { TranslateModule } from '@ngx-translate/core' +import { LINK_FIXTURES } from '../../../../../util/shared/src/lib/links/link.fixtures' import { DownloadsListComponent } from './downloads-list.component' -const linkHelperServiceMock = {} +const linkHelperServiceMock = { + isWfsLink: jest.fn(() => true), +} + +@Component({ + selector: 'gn-ui-download-item', + template: ``, +}) +export class DownloadItemComponentMock { + @Input() link: MetadataLinkValid + @Input() color: string +} + describe('DownloadsListComponent', () => { let component: DownloadsListComponent let fixture: ComponentFixture + let de beforeEach(async () => { await TestBed.configureTestingModule({ imports: [TranslateModule.forRoot()], - declarations: [DownloadsListComponent], + declarations: [DownloadsListComponent, DownloadItemComponentMock], schemas: [NO_ERRORS_SCHEMA], providers: [ { @@ -21,17 +45,34 @@ describe('DownloadsListComponent', () => { useValue: linkHelperServiceMock, }, ], - }).compileComponents() + }) + .overrideComponent(DownloadsListComponent, { + set: { changeDetection: ChangeDetectionStrategy.Default }, + }) + .compileComponents() }) beforeEach(() => { fixture = TestBed.createComponent(DownloadsListComponent) component = fixture.componentInstance component.links = [] - fixture.detectChanges() + de = fixture.debugElement }) it('should create', () => { + fixture.detectChanges() expect(component).toBeTruthy() }) + describe('when link format is unknown', () => { + let items: DebugElement[] + + beforeEach(() => { + component.links = [LINK_FIXTURES.unknownFormat] + fixture.detectChanges() + items = de.queryAll(By.directive(DownloadItemComponentMock)) + }) + it('contains one link', () => { + expect(items.length).toBe(1) + }) + }) }) diff --git a/libs/ui/elements/src/lib/downloads-list/downloads-list.component.ts b/libs/ui/elements/src/lib/downloads-list/downloads-list.component.ts index 8a183721be..89cb504f32 100644 --- a/libs/ui/elements/src/lib/downloads-list/downloads-list.component.ts +++ b/libs/ui/elements/src/lib/downloads-list/downloads-list.component.ts @@ -54,7 +54,6 @@ export class DownloadsListComponent implements OnInit { ngOnInit(): void { this.processedLinks = this.formatLinks(this.links) - console.log(this.processedLinks) this.processedLinks = this.assignColor(this.processedLinks) this.processedLinks = this.isGeneratedFromWfs(this.processedLinks) this.filteredLinks = this.filterLinks(this.processedLinks) diff --git a/libs/util/shared/src/lib/links/link.fixtures.ts b/libs/util/shared/src/lib/links/link.fixtures.ts index a4f84a7dba..9846a94839 100644 --- a/libs/util/shared/src/lib/links/link.fixtures.ts +++ b/libs/util/shared/src/lib/links/link.fixtures.ts @@ -156,4 +156,11 @@ export const LINK_FIXTURES = { label: 'landingpage link', url: 'https://landing.page', }, + unknownFormat: { + protocol: 'WWW:DOWNLOAD-1.0-http--download', + format: undefined, + name: 'Vue HTML des métadonnées sur internet', + label: 'Vue HTML des métadonnées sur internet', + url: 'http://catalogue.geo-ide.developpement-durable.gouv.fr/catalogue/srv/fre/catalog.search#/metadata/fr-120066022-jdd-199fd14c-2abb-4c14-b0f8-6c8d92e7b480', + }, } From 1146c3a0c517fc3998d84ac13661ef563489c289 Mon Sep 17 00:00:00 2001 From: Florent gravin Date: Wed, 22 Jun 2022 13:58:58 +0200 Subject: [PATCH 5/5] refactor(downloads-list): improve components, move fixtures add tests for filtering --- .../downloads-list.component.spec.ts | 57 +++++++++++++++++-- .../downloads-list.component.ts | 29 ++++------ libs/ui/elements/tsconfig.lib.json | 2 +- libs/util/shared/src/lib/fixtures/index.ts | 1 + .../lib/{links => fixtures}/link.fixtures.ts | 0 .../lib/links/link-classifier.service.spec.ts | 2 +- .../src/lib/links/link-helper.service.spec.ts | 2 +- .../shared/src/lib/links/link-utils.spec.ts | 2 +- 8 files changed, 70 insertions(+), 25 deletions(-) rename libs/util/shared/src/lib/{links => fixtures}/link.fixtures.ts (100%) diff --git a/libs/ui/elements/src/lib/downloads-list/downloads-list.component.spec.ts b/libs/ui/elements/src/lib/downloads-list/downloads-list.component.spec.ts index 47fa2fe3f7..b425f5e581 100644 --- a/libs/ui/elements/src/lib/downloads-list/downloads-list.component.spec.ts +++ b/libs/ui/elements/src/lib/downloads-list/downloads-list.component.spec.ts @@ -8,11 +8,11 @@ import { import { ComponentFixture, TestBed } from '@angular/core/testing' import { By } from '@angular/platform-browser' import { + LINK_FIXTURES, LinkHelperService, MetadataLinkValid, } from '@geonetwork-ui/util/shared' import { TranslateModule } from '@ngx-translate/core' -import { LINK_FIXTURES } from '../../../../../util/shared/src/lib/links/link.fixtures' import { DownloadsListComponent } from './downloads-list.component' @@ -24,7 +24,7 @@ const linkHelperServiceMock = { selector: 'gn-ui-download-item', template: ``, }) -export class DownloadItemComponentMock { +class MockDownloadItemComponent { @Input() link: MetadataLinkValid @Input() color: string } @@ -37,7 +37,7 @@ describe('DownloadsListComponent', () => { beforeEach(async () => { await TestBed.configureTestingModule({ imports: [TranslateModule.forRoot()], - declarations: [DownloadsListComponent, DownloadItemComponentMock], + declarations: [DownloadsListComponent, MockDownloadItemComponent], schemas: [NO_ERRORS_SCHEMA], providers: [ { @@ -63,16 +63,65 @@ describe('DownloadsListComponent', () => { fixture.detectChanges() expect(component).toBeTruthy() }) + + describe('when a list of downloads', () => { + let items: DebugElement[] + + beforeEach(() => { + component.links = [ + LINK_FIXTURES.dataCsv, + LINK_FIXTURES.dataPdf, + LINK_FIXTURES.dataPdf, + ] + fixture.detectChanges() + items = de.queryAll(By.directive(MockDownloadItemComponent)) + }) + it('contains one link', () => { + expect(items.length).toBe(3) + }) + }) describe('when link format is unknown', () => { let items: DebugElement[] beforeEach(() => { component.links = [LINK_FIXTURES.unknownFormat] fixture.detectChanges() - items = de.queryAll(By.directive(DownloadItemComponentMock)) + items = de.queryAll(By.directive(MockDownloadItemComponent)) }) it('contains one link', () => { expect(items.length).toBe(1) }) }) + describe('hydrates link with color and format', () => { + let items: DebugElement[] + + beforeEach(() => { + component.links = [LINK_FIXTURES.geodataShpWithMimeType] + fixture.detectChanges() + items = de.queryAll(By.directive(MockDownloadItemComponent)) + }) + it('contains color, isWfs & format', () => { + expect(items.length).toBe(1) + expect(items[0].componentInstance.link).toEqual({ + ...LINK_FIXTURES.geodataShpWithMimeType, + color: 'var(--color-gray-700)', + format: '', + isWfs: true, + }) + }) + }) + describe('filtering', () => { + let items: DebugElement[] + + beforeEach(() => { + component.links = [{ ...LINK_FIXTURES.dataCsv, format: 'csv' }] + component.activeFilterFormats = ['csv', 'json'] + fixture.detectChanges() + }) + it('csv link is displayed', () => { + expect(component.filteredLinks.length).toBe(1) + component.toggleFilterFormat('csv') + expect(component.filteredLinks.length).toBe(0) + }) + }) }) diff --git a/libs/ui/elements/src/lib/downloads-list/downloads-list.component.ts b/libs/ui/elements/src/lib/downloads-list/downloads-list.component.ts index 89cb504f32..163b14c7df 100644 --- a/libs/ui/elements/src/lib/downloads-list/downloads-list.component.ts +++ b/libs/ui/elements/src/lib/downloads-list/downloads-list.component.ts @@ -53,9 +53,8 @@ export class DownloadsListComponent implements OnInit { } ngOnInit(): void { - this.processedLinks = this.formatLinks(this.links) - this.processedLinks = this.assignColor(this.processedLinks) - this.processedLinks = this.isGeneratedFromWfs(this.processedLinks) + this.processedLinks = this.assignColor(this.links) + this.processedLinks = this.formatWfs(this.processedLinks) this.filteredLinks = this.filterLinks(this.processedLinks) this.filterButtons = this.filterFormats.map((format) => { @@ -66,15 +65,6 @@ export class DownloadsListComponent implements OnInit { }) } - formatLinks(links) { - return links.map((link) => { - return { - ...link, - format: link.format ? link.format.split(':').at(-1) : '', - } - }) - } - filterLinks(links: MetadataLinkValid[]) { if ( this.activeFilterFormats.length === 0 || @@ -107,13 +97,18 @@ export class DownloadsListComponent implements OnInit { }) } - isGeneratedFromWfs(links: MetadataLinkValid[]) { + formatWfs(links: MetadataLinkValid[]) { return links.map((link) => { - if (!this.linkHelper.isWfsLink(link)) return link - return { - ...link, - isWfs: true, + if (this.linkHelper.isWfsLink(link)) { + const { format = '' } = link + const tokens = format.split(':') + link = { + ...link, + isWfs: true, + format: tokens[tokens.length - 1], + } } + return link }) } } diff --git a/libs/ui/elements/tsconfig.lib.json b/libs/ui/elements/tsconfig.lib.json index 382013681c..177dff5d17 100644 --- a/libs/ui/elements/tsconfig.lib.json +++ b/libs/ui/elements/tsconfig.lib.json @@ -7,7 +7,7 @@ "declarationMap": true, "inlineSources": true, "types": [], - "lib": ["dom", "es2018"] + "lib": ["dom", "es2022"] }, "exclude": [ "src/test-setup.ts", diff --git a/libs/util/shared/src/lib/fixtures/index.ts b/libs/util/shared/src/lib/fixtures/index.ts index cc6e0c18ee..67a7d0a9f7 100644 --- a/libs/util/shared/src/lib/fixtures/index.ts +++ b/libs/util/shared/src/lib/fixtures/index.ts @@ -1,4 +1,5 @@ export * from './geojson.fixtures' export * from './ol-feature.fixture' export * from './record-link.fixtures' +export * from './link.fixtures' export * from './records' diff --git a/libs/util/shared/src/lib/links/link.fixtures.ts b/libs/util/shared/src/lib/fixtures/link.fixtures.ts similarity index 100% rename from libs/util/shared/src/lib/links/link.fixtures.ts rename to libs/util/shared/src/lib/fixtures/link.fixtures.ts diff --git a/libs/util/shared/src/lib/links/link-classifier.service.spec.ts b/libs/util/shared/src/lib/links/link-classifier.service.spec.ts index 6317922de2..ca0d693151 100644 --- a/libs/util/shared/src/lib/links/link-classifier.service.spec.ts +++ b/libs/util/shared/src/lib/links/link-classifier.service.spec.ts @@ -1,5 +1,5 @@ import { LinkClassifierService, LinkUsage } from './link-classifier.service' -import { LINK_FIXTURES } from './link.fixtures' +import { LINK_FIXTURES } from '../fixtures/link.fixtures' describe('LinkClassifierService', () => { let service: LinkClassifierService diff --git a/libs/util/shared/src/lib/links/link-helper.service.spec.ts b/libs/util/shared/src/lib/links/link-helper.service.spec.ts index 0c2bed2eb6..e43e2d5b0d 100644 --- a/libs/util/shared/src/lib/links/link-helper.service.spec.ts +++ b/libs/util/shared/src/lib/links/link-helper.service.spec.ts @@ -5,7 +5,7 @@ import { MetadataLink, MetadataRecord } from '../models' import { LinkClassifierService, LinkUsage } from './link-classifier.service' import { LinkHelperService } from './link-helper.service' -import { LINK_FIXTURES } from './link.fixtures' +import { LINK_FIXTURES } from '../fixtures/link.fixtures' let linkUsage: LinkUsage[] const linkClassifierMock = { diff --git a/libs/util/shared/src/lib/links/link-utils.spec.ts b/libs/util/shared/src/lib/links/link-utils.spec.ts index 93e7a61889..02c78e1e9a 100644 --- a/libs/util/shared/src/lib/links/link-utils.spec.ts +++ b/libs/util/shared/src/lib/links/link-utils.spec.ts @@ -7,7 +7,7 @@ import { sortPriority, FORMATS, } from './link-utils' -import { LINK_FIXTURES } from './link.fixtures' +import { LINK_FIXTURES } from '../fixtures/link.fixtures' jest.mock('@camptocamp/ogc-client', () => ({ WfsEndpoint: class {