From b6edbd674693e1828dd3ef1decefe6bb9fd86330 Mon Sep 17 00:00:00 2001 From: Angelika Kinas Date: Thu, 12 Dec 2024 14:13:30 +0100 Subject: [PATCH 1/5] feat(editor): create form-field-spatial-toggle component, add new field to config --- .../form-field-spatial-toggle.component.css | 0 .../form-field-spatial-toggle.component.html | 1 + ...orm-field-spatial-toggle.component.spec.ts | 21 +++++++++++++++++++ .../form-field-spatial-toggle.component.ts | 14 +++++++++++++ .../form-field/form-field.component.html | 3 +++ .../form-field/form-field.component.ts | 3 +++ libs/feature/editor/src/lib/fields.config.ts | 10 ++++++++- .../src/lib/models/editor-config.model.ts | 4 +++- 8 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 libs/feature/editor/src/lib/components/record-form/form-field/form-field-spatial-toggle/form-field-spatial-toggle.component.css create mode 100644 libs/feature/editor/src/lib/components/record-form/form-field/form-field-spatial-toggle/form-field-spatial-toggle.component.html create mode 100644 libs/feature/editor/src/lib/components/record-form/form-field/form-field-spatial-toggle/form-field-spatial-toggle.component.spec.ts create mode 100644 libs/feature/editor/src/lib/components/record-form/form-field/form-field-spatial-toggle/form-field-spatial-toggle.component.ts diff --git a/libs/feature/editor/src/lib/components/record-form/form-field/form-field-spatial-toggle/form-field-spatial-toggle.component.css b/libs/feature/editor/src/lib/components/record-form/form-field/form-field-spatial-toggle/form-field-spatial-toggle.component.css new file mode 100644 index 0000000000..e69de29bb2 diff --git a/libs/feature/editor/src/lib/components/record-form/form-field/form-field-spatial-toggle/form-field-spatial-toggle.component.html b/libs/feature/editor/src/lib/components/record-form/form-field/form-field-spatial-toggle/form-field-spatial-toggle.component.html new file mode 100644 index 0000000000..e59a6765aa --- /dev/null +++ b/libs/feature/editor/src/lib/components/record-form/form-field/form-field-spatial-toggle/form-field-spatial-toggle.component.html @@ -0,0 +1 @@ +

form-field-spatial-toggle works!

diff --git a/libs/feature/editor/src/lib/components/record-form/form-field/form-field-spatial-toggle/form-field-spatial-toggle.component.spec.ts b/libs/feature/editor/src/lib/components/record-form/form-field/form-field-spatial-toggle/form-field-spatial-toggle.component.spec.ts new file mode 100644 index 0000000000..05259e5e31 --- /dev/null +++ b/libs/feature/editor/src/lib/components/record-form/form-field/form-field-spatial-toggle/form-field-spatial-toggle.component.spec.ts @@ -0,0 +1,21 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { FormFieldSpatialToggleComponent } from './form-field-spatial-toggle.component'; + +describe('FormFieldSpatialToggleComponent', () => { + let component: FormFieldSpatialToggleComponent; + let fixture: ComponentFixture; + + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [FormFieldSpatialToggleComponent] + }); + fixture = TestBed.createComponent(FormFieldSpatialToggleComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/libs/feature/editor/src/lib/components/record-form/form-field/form-field-spatial-toggle/form-field-spatial-toggle.component.ts b/libs/feature/editor/src/lib/components/record-form/form-field/form-field-spatial-toggle/form-field-spatial-toggle.component.ts new file mode 100644 index 0000000000..aef7577e50 --- /dev/null +++ b/libs/feature/editor/src/lib/components/record-form/form-field/form-field-spatial-toggle/form-field-spatial-toggle.component.ts @@ -0,0 +1,14 @@ +import { ChangeDetectionStrategy, Component } from '@angular/core'; +import { CommonModule } from '@angular/common'; + +@Component({ + selector: 'gn-ui-form-field-spatial-toggle', + standalone: true, + imports: [CommonModule], + templateUrl: './form-field-spatial-toggle.component.html', + styleUrls: ['./form-field-spatial-toggle.component.css'], + changeDetection: ChangeDetectionStrategy.OnPush +}) +export class FormFieldSpatialToggleComponent { + +} diff --git a/libs/feature/editor/src/lib/components/record-form/form-field/form-field.component.html b/libs/feature/editor/src/lib/components/record-form/form-field/form-field.component.html index 5f10115ddc..5bb8ed772d 100644 --- a/libs/feature/editor/src/lib/components/record-form/form-field/form-field.component.html +++ b/libs/feature/editor/src/lib/components/record-form/form-field/form-field.component.html @@ -167,6 +167,9 @@ + + + diff --git a/libs/feature/editor/src/lib/components/record-form/form-field/form-field.component.ts b/libs/feature/editor/src/lib/components/record-form/form-field/form-field.component.ts index 35124632a2..2ed9834332 100644 --- a/libs/feature/editor/src/lib/components/record-form/form-field/form-field.component.ts +++ b/libs/feature/editor/src/lib/components/record-form/form-field/form-field.component.ts @@ -52,6 +52,7 @@ import { FormFieldUpdateFrequencyComponent } from './form-field-update-frequency import { FormFieldConstraintsShortcutsComponent } from './form-field-constraints-shortcuts/form-field-constraints-shortcuts.component' import { FormFieldConstraintsComponent } from './form-field-constraints/form-field-constraints.component' import { TextFieldModule } from '@angular/cdk/text-field' +import { FormFieldSpatialToggleComponent } from './form-field-spatial-toggle/form-field-spatial-toggle.component' @Component({ selector: 'gn-ui-form-field', @@ -84,6 +85,7 @@ import { TextFieldModule } from '@angular/cdk/text-field' FormFieldContactsComponent, FormFieldConstraintsComponent, FormFieldConstraintsShortcutsComponent, + FormFieldSpatialToggleComponent, TextFieldModule, ], }) @@ -116,6 +118,7 @@ export class FormFieldComponent { this.model === 'legalConstraints' || this.model === 'securityConstraints' || this.model === 'otherConstraints' || + this.model === 'spatialExtents' || this.componentName === 'form-field-constraints-shortcuts' ) } diff --git a/libs/feature/editor/src/lib/fields.config.ts b/libs/feature/editor/src/lib/fields.config.ts index 90f18c4003..5e46e2883e 100644 --- a/libs/feature/editor/src/lib/fields.config.ts +++ b/libs/feature/editor/src/lib/fields.config.ts @@ -148,6 +148,14 @@ export const RECORD_GRAPHICAL_OVERVIEW_FIELD: EditorField = { }, } +export const RECORD_SPATIAL_TOGGLE_FIELD: EditorField = { + // model: 'keywords', + componentName: 'form-field-spatial-toggle', + formFieldConfig: { + labelKey: marker('editor.record.form.field.spatialExtents'), + }, +} + export const RECORD_SPATIAL_EXTENTS_FIELD: EditorField = { model: 'spatialExtents', formFieldConfig: { @@ -203,7 +211,7 @@ export const ABOUT_SECTION: EditorSection = { export const GEOGRAPHICAL_COVERAGE_SECTION: EditorSection = { labelKey: marker('editor.record.form.section.geographicalCoverage.label'), hidden: false, - fields: [RECORD_SPATIAL_EXTENTS_FIELD], + fields: [RECORD_SPATIAL_TOGGLE_FIELD, RECORD_SPATIAL_EXTENTS_FIELD], } export const ASSOCIATED_RESOURCES_SECTION: EditorSection = { diff --git a/libs/feature/editor/src/lib/models/editor-config.model.ts b/libs/feature/editor/src/lib/models/editor-config.model.ts index d4fcc83afe..b675bad2de 100644 --- a/libs/feature/editor/src/lib/models/editor-config.model.ts +++ b/libs/feature/editor/src/lib/models/editor-config.model.ts @@ -23,7 +23,9 @@ export type FieldModelSpecifier = | OnlineLinkResourceSpecifier | DatasetDistributionsSpecifier -export type FormFieldComponentName = 'form-field-constraints-shortcuts' +export type FormFieldComponentName = + | 'form-field-constraints-shortcuts' + | 'form-field-spatial-toggle' export interface EditorFieldIdentification { // name of the target field in the record; will not change the record directly if not defined From efeed3ae0b1098ea08a68c35d92cf47950927c87 Mon Sep 17 00:00:00 2001 From: Angelika Kinas Date: Thu, 12 Dec 2024 14:37:10 +0100 Subject: [PATCH 2/5] feat: extract national/regional toggle from spatial extent field and hide it on the form --- .../form-field-spatial-extent.component.html | 5 -- .../form-field-spatial-extent.component.ts | 42 +-------------- .../form-field-spatial-toggle.component.html | 6 ++- .../form-field-spatial-toggle.component.ts | 54 +++++++++++++++++-- .../form-field/form-field.component.ts | 1 - libs/feature/editor/src/lib/fields.config.ts | 6 +-- 6 files changed, 58 insertions(+), 56 deletions(-) diff --git a/libs/feature/editor/src/lib/components/record-form/form-field/form-field-spatial-extent/form-field-spatial-extent.component.html b/libs/feature/editor/src/lib/components/record-form/form-field/form-field-spatial-extent/form-field-spatial-extent.component.html index 7ae739977e..f290b66c9b 100644 --- a/libs/feature/editor/src/lib/components/record-form/form-field/form-field-spatial-extent/form-field-spatial-extent.component.html +++ b/libs/feature/editor/src/lib/components/record-form/form-field/form-field-spatial-extent/form-field-spatial-extent.component.html @@ -1,9 +1,4 @@
- record?.keywords) ) - switchToggleOptions$: Observable = - this.allKeywords$.pipe( - map((keywords) => - SPATIAL_SCOPES.map((scope) => { - const isChecked = keywords.some( - (keyword) => keyword.label === scope.label - ) - return { - label: scope.label, - checked: isChecked, - } - }) - ) - ) - shownKeywords$ = this.editorFacade.record$.pipe( map((record) => record?.keywords.filter((k) => k.type === 'place')), // look for full keywords in the thesauri @@ -198,24 +178,4 @@ export class FormFieldSpatialExtentComponent { this.editorFacade.updateRecordField('keywords', allKeywords) this.editorFacade.updateRecordField('spatialExtents', spatialExtents) } - - async onSpatialScopeChange(selectedOption: SwitchToggleOption) { - // remove all existing spatial scope keywords - const allKeywords = await firstValueFrom(this.allKeywords$) - const filteredKeywords = allKeywords.filter((keyword) => { - const spatialScopeLabels = SPATIAL_SCOPES.map((scope) => scope.label) - return !spatialScopeLabels.includes(keyword.label) - }) - - const selectedOptionLabel = selectedOption.label - const selectedKeyword = SPATIAL_SCOPES.find( - (scopes) => scopes.label === selectedOptionLabel - ) - - // add the selected spatial scope keyword - this.editorFacade.updateRecordField('keywords', [ - ...filteredKeywords, - { ...selectedKeyword }, - ]) - } } diff --git a/libs/feature/editor/src/lib/components/record-form/form-field/form-field-spatial-toggle/form-field-spatial-toggle.component.html b/libs/feature/editor/src/lib/components/record-form/form-field/form-field-spatial-toggle/form-field-spatial-toggle.component.html index e59a6765aa..ea2661e2c2 100644 --- a/libs/feature/editor/src/lib/components/record-form/form-field/form-field-spatial-toggle/form-field-spatial-toggle.component.html +++ b/libs/feature/editor/src/lib/components/record-form/form-field/form-field-spatial-toggle/form-field-spatial-toggle.component.html @@ -1 +1,5 @@ -

form-field-spatial-toggle works!

+ diff --git a/libs/feature/editor/src/lib/components/record-form/form-field/form-field-spatial-toggle/form-field-spatial-toggle.component.ts b/libs/feature/editor/src/lib/components/record-form/form-field/form-field-spatial-toggle/form-field-spatial-toggle.component.ts index aef7577e50..7687435a67 100644 --- a/libs/feature/editor/src/lib/components/record-form/form-field/form-field-spatial-toggle/form-field-spatial-toggle.component.ts +++ b/libs/feature/editor/src/lib/components/record-form/form-field/form-field-spatial-toggle/form-field-spatial-toggle.component.ts @@ -1,14 +1,60 @@ -import { ChangeDetectionStrategy, Component } from '@angular/core'; -import { CommonModule } from '@angular/common'; +import { ChangeDetectionStrategy, Component } from '@angular/core' +import { CommonModule } from '@angular/common' +import { + SwitchToggleComponent, + SwitchToggleOption, +} from '@geonetwork-ui/ui/inputs' +import { EditorFacade } from '../../../../+state/editor.facade' +import { firstValueFrom, map, Observable } from 'rxjs' +import { SPATIAL_SCOPES } from '../../../../fields.config' @Component({ selector: 'gn-ui-form-field-spatial-toggle', standalone: true, - imports: [CommonModule], + imports: [CommonModule, SwitchToggleComponent], templateUrl: './form-field-spatial-toggle.component.html', styleUrls: ['./form-field-spatial-toggle.component.css'], - changeDetection: ChangeDetectionStrategy.OnPush + changeDetection: ChangeDetectionStrategy.OnPush, }) export class FormFieldSpatialToggleComponent { + allKeywords$ = this.editorFacade.record$.pipe( + map((record) => record?.keywords) + ) + switchToggleOptions$: Observable = + this.allKeywords$.pipe( + map((keywords) => + SPATIAL_SCOPES.map((scope) => { + const isChecked = keywords.some( + (keyword) => keyword.label === scope.label + ) + return { + label: scope.label, + checked: isChecked, + } + }) + ) + ) + + constructor(private editorFacade: EditorFacade) {} + + async onSpatialScopeChange(selectedOption: SwitchToggleOption) { + // remove all existing spatial scope keywords + const allKeywords = await firstValueFrom(this.allKeywords$) + const filteredKeywords = allKeywords.filter((keyword) => { + const spatialScopeLabels = SPATIAL_SCOPES.map((scope) => scope.label) + return !spatialScopeLabels.includes(keyword.label) + }) + + const selectedOptionLabel = selectedOption.label + const selectedKeyword = SPATIAL_SCOPES.find( + (scopes) => scopes.label === selectedOptionLabel + ) + + // add the selected spatial scope keyword + this.editorFacade.updateRecordField('keywords', [ + ...filteredKeywords, + { ...selectedKeyword }, + ]) + } } diff --git a/libs/feature/editor/src/lib/components/record-form/form-field/form-field.component.ts b/libs/feature/editor/src/lib/components/record-form/form-field/form-field.component.ts index 2ed9834332..9d2d1b1311 100644 --- a/libs/feature/editor/src/lib/components/record-form/form-field/form-field.component.ts +++ b/libs/feature/editor/src/lib/components/record-form/form-field/form-field.component.ts @@ -118,7 +118,6 @@ export class FormFieldComponent { this.model === 'legalConstraints' || this.model === 'securityConstraints' || this.model === 'otherConstraints' || - this.model === 'spatialExtents' || this.componentName === 'form-field-constraints-shortcuts' ) } diff --git a/libs/feature/editor/src/lib/fields.config.ts b/libs/feature/editor/src/lib/fields.config.ts index 5e46e2883e..f5e1b338a8 100644 --- a/libs/feature/editor/src/lib/fields.config.ts +++ b/libs/feature/editor/src/lib/fields.config.ts @@ -149,11 +149,9 @@ export const RECORD_GRAPHICAL_OVERVIEW_FIELD: EditorField = { } export const RECORD_SPATIAL_TOGGLE_FIELD: EditorField = { - // model: 'keywords', componentName: 'form-field-spatial-toggle', - formFieldConfig: { - labelKey: marker('editor.record.form.field.spatialExtents'), - }, + formFieldConfig: {}, + hidden: true, } export const RECORD_SPATIAL_EXTENTS_FIELD: EditorField = { From 4e196fde2d694c2eac26cfe07fc23b07469157fe Mon Sep 17 00:00:00 2001 From: Angelika Kinas Date: Thu, 12 Dec 2024 14:50:49 +0100 Subject: [PATCH 3/5] chore(e2e): remove test for nation/regional toggle --- apps/metadata-editor-e2e/src/e2e/edit.cy.ts | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/apps/metadata-editor-e2e/src/e2e/edit.cy.ts b/apps/metadata-editor-e2e/src/e2e/edit.cy.ts index 3fd0fd98a8..208f5122da 100644 --- a/apps/metadata-editor-e2e/src/e2e/edit.cy.ts +++ b/apps/metadata-editor-e2e/src/e2e/edit.cy.ts @@ -441,27 +441,6 @@ describe('editor form', () => { .should('have.length', 1) }) }) - describe('spatial scope', () => { - it('toggle between national and regional spatial coverage', () => { - cy.get('gn-ui-switch-toggle').should('exist') - - cy.get('gn-ui-switch-toggle').find('mat-button-toggle').eq(0).click() - cy.get('mat-button-toggle') - .eq(0) - .should('have.class', 'mat-button-toggle-checked') - cy.get('mat-button-toggle') - .eq(1) - .should('not.have.class', 'mat-button-toggle-checked') - - cy.get('gn-ui-switch-toggle').find('mat-button-toggle').eq(1).click() - cy.get('mat-button-toggle') - .eq(1) - .should('have.class', 'mat-button-toggle-checked') - cy.get('mat-button-toggle') - .eq(0) - .should('not.have.class', 'mat-button-toggle-checked') - }) - }) }) describe('distribution resources', () => { beforeEach(() => { From 5684c88f0d334986d7ce02193e2f5d2309c514c6 Mon Sep 17 00:00:00 2001 From: Angelika Kinas Date: Thu, 12 Dec 2024 15:12:20 +0100 Subject: [PATCH 4/5] chore: fix unit test --- ...orm-field-spatial-toggle.component.spec.ts | 33 +++++++++++-------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/libs/feature/editor/src/lib/components/record-form/form-field/form-field-spatial-toggle/form-field-spatial-toggle.component.spec.ts b/libs/feature/editor/src/lib/components/record-form/form-field/form-field-spatial-toggle/form-field-spatial-toggle.component.spec.ts index 05259e5e31..05063dfb46 100644 --- a/libs/feature/editor/src/lib/components/record-form/form-field/form-field-spatial-toggle/form-field-spatial-toggle.component.spec.ts +++ b/libs/feature/editor/src/lib/components/record-form/form-field/form-field-spatial-toggle/form-field-spatial-toggle.component.spec.ts @@ -1,21 +1,28 @@ -import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { ComponentFixture, TestBed } from '@angular/core/testing' -import { FormFieldSpatialToggleComponent } from './form-field-spatial-toggle.component'; +import { FormFieldSpatialToggleComponent } from './form-field-spatial-toggle.component' +import { MockProvider } from 'ng-mocks' +import { EditorFacade } from '../../../../+state/editor.facade' +import { BehaviorSubject } from 'rxjs' +class EditorFacadeMock { + record$ = new BehaviorSubject([]) +} describe('FormFieldSpatialToggleComponent', () => { - let component: FormFieldSpatialToggleComponent; - let fixture: ComponentFixture; + let component: FormFieldSpatialToggleComponent + let fixture: ComponentFixture beforeEach(() => { TestBed.configureTestingModule({ - imports: [FormFieldSpatialToggleComponent] - }); - fixture = TestBed.createComponent(FormFieldSpatialToggleComponent); - component = fixture.componentInstance; - fixture.detectChanges(); - }); + imports: [FormFieldSpatialToggleComponent], + providers: [MockProvider(EditorFacade, EditorFacadeMock, 'useClass')], + }) + fixture = TestBed.createComponent(FormFieldSpatialToggleComponent) + component = fixture.componentInstance + fixture.detectChanges() + }) it('should create', () => { - expect(component).toBeTruthy(); - }); -}); + expect(component).toBeTruthy() + }) +}) From 87cff9b18d4236d5e29b38fbc38529b078d6d5e7 Mon Sep 17 00:00:00 2001 From: Angelika Kinas Date: Thu, 12 Dec 2024 15:35:50 +0100 Subject: [PATCH 5/5] chore: move unit tests to spatial toggle comp --- .../fixtures/src/lib/records.fixtures.ts | 101 ++++++++++ ...orm-field-spatial-extent.component.spec.ts | 174 +----------------- ...orm-field-spatial-toggle.component.spec.ts | 87 ++++++++- 3 files changed, 191 insertions(+), 171 deletions(-) diff --git a/libs/common/fixtures/src/lib/records.fixtures.ts b/libs/common/fixtures/src/lib/records.fixtures.ts index b5eee466aa..f4a59b1d73 100644 --- a/libs/common/fixtures/src/lib/records.fixtures.ts +++ b/libs/common/fixtures/src/lib/records.fixtures.ts @@ -1,6 +1,8 @@ import { CatalogRecord, DatasetRecord, + DatasetSpatialExtent, + Keyword, } from '@geonetwork-ui/common/domain/model/record' export const datasetRecordsFixture: () => CatalogRecord[] = () => [ @@ -464,3 +466,102 @@ export const simpleDatasetRecordAsXmlFixture = ` + +export const NATIONAL_KEYWORD = { + key: 'http://inspire.ec.europa.eu/metadata-codelist/SpatialScope/national', + label: 'National', + description: '', + type: 'theme', +} + +export const SAMPLE_PLACE_KEYWORDS: Keyword[] = [ + // these keywords come from a thesaurus available locally + { + key: 'uri1', + label: 'Berlin', + thesaurus: { + id: '1', + name: 'places', + }, + type: 'place', + bbox: [13.27, 52.63, 52.5, 13.14], + }, + { + key: 'uri2', + label: 'Hamburg', + thesaurus: { + id: '1', + name: 'places', + }, + type: 'place', + bbox: [10.5, 53.66, 53.53, 10], + }, + // this keyword is available locally but has no extent linked to it + { + key: 'uri3', + label: 'Munich', + thesaurus: { + id: '1', + name: 'places', + }, + type: 'place', + bbox: [11.64, 48.65, 48.51, 11.5], + }, + // this keyword comes from a thesaurus not available locally + { + label: 'Europe', + thesaurus: { + id: '2', + name: 'otherPlaces', + }, + type: 'place', + }, + // this keyword has no thesaurus + { + label: 'Narnia', + type: 'place', + }, +] + +// records coming from XML do not have a key or a bbox in them +export const SAMPLE_PLACE_KEYWORDS_FROM_XML = SAMPLE_PLACE_KEYWORDS.map( + ({ label, thesaurus, type }) => ({ + label, + type, + ...(thesaurus && { thesaurus }), + }) +) + +export const SAMPLE_SPATIAL_EXTENTS: DatasetSpatialExtent[] = [ + // these extents are linked to keywords known locally + { + description: 'uri1', + bbox: [13.5, 52.5, 14.5, 53.5], + }, + { + description: 'uri2', + bbox: [10, 53.5, 11, 53.4], + }, + { + description: 'uri4', + bbox: [11.5, 48.5, 11.5, 48.3], + }, + // this extent is linked to a keyword not available locally + { + description: 'URI-Paris', + bbox: [1, 2, 3, 4], + }, + // this extent is not linked to any keyword + { + bbox: [5, 6, 7, 8], + }, +] + +export const SAMPLE_RECORD = { + ...datasetRecordsFixture()[0], + spatialExtents: SAMPLE_SPATIAL_EXTENTS, + keywords: [ + ...datasetRecordsFixture()[0].keywords, + ...SAMPLE_PLACE_KEYWORDS_FROM_XML, + ], +} diff --git a/libs/feature/editor/src/lib/components/record-form/form-field/form-field-spatial-extent/form-field-spatial-extent.component.spec.ts b/libs/feature/editor/src/lib/components/record-form/form-field/form-field-spatial-extent/form-field-spatial-extent.component.spec.ts index 359d036bf1..0308a9fc75 100644 --- a/libs/feature/editor/src/lib/components/record-form/form-field/form-field-spatial-extent/form-field-spatial-extent.component.spec.ts +++ b/libs/feature/editor/src/lib/components/record-form/form-field/form-field-spatial-extent/form-field-spatial-extent.component.spec.ts @@ -1,116 +1,23 @@ import { ComponentFixture, TestBed } from '@angular/core/testing' import { FormFieldSpatialExtentComponent } from './form-field-spatial-extent.component' -import { BehaviorSubject, firstValueFrom, from, of } from 'rxjs' +import { BehaviorSubject, from, of } from 'rxjs' import { PlatformServiceInterface } from '@geonetwork-ui/common/domain/platform.service.interface' import { CatalogRecord, - DatasetSpatialExtent, Keyword, } from '@geonetwork-ui/common/domain/model/record' import { MockBuilder, MockProvider } from 'ng-mocks' import { EditorFacade } from '../../../../+state/editor.facade' -import { datasetRecordsFixture } from '@geonetwork-ui/common/fixtures' +import { + datasetRecordsFixture, + NATIONAL_KEYWORD, + SAMPLE_PLACE_KEYWORDS, + SAMPLE_PLACE_KEYWORDS_FROM_XML, + SAMPLE_RECORD, + SAMPLE_SPATIAL_EXTENTS, +} from '@geonetwork-ui/common/fixtures' import { TranslateModule } from '@ngx-translate/core' -const NATIONAL_KEYWORD = { - key: 'http://inspire.ec.europa.eu/metadata-codelist/SpatialScope/national', - label: 'National', - description: '', - type: 'theme', -} - -const SAMPLE_PLACE_KEYWORDS: Keyword[] = [ - // these keywords come from a thesaurus available locally - { - key: 'uri1', - label: 'Berlin', - thesaurus: { - id: '1', - name: 'places', - }, - type: 'place', - bbox: [13.27, 52.63, 52.5, 13.14], - }, - { - key: 'uri2', - label: 'Hamburg', - thesaurus: { - id: '1', - name: 'places', - }, - type: 'place', - bbox: [10.5, 53.66, 53.53, 10], - }, - // this keyword is available locally but has no extent linked to it - { - key: 'uri3', - label: 'Munich', - thesaurus: { - id: '1', - name: 'places', - }, - type: 'place', - bbox: [11.64, 48.65, 48.51, 11.5], - }, - // this keyword comes from a thesaurus not available locally - { - label: 'Europe', - thesaurus: { - id: '2', - name: 'otherPlaces', - }, - type: 'place', - }, - // this keyword has no thesaurus - { - label: 'Narnia', - type: 'place', - }, -] - -// records coming from XML do not have a key or a bbox in them -const SAMPLE_PLACE_KEYWORDS_FROM_XML = SAMPLE_PLACE_KEYWORDS.map( - ({ label, thesaurus, type }) => ({ - label, - type, - ...(thesaurus && { thesaurus }), - }) -) - -const SAMPLE_SPATIAL_EXTENTS: DatasetSpatialExtent[] = [ - // these extents are linked to keywords known locally - { - description: 'uri1', - bbox: [13.5, 52.5, 14.5, 53.5], - }, - { - description: 'uri2', - bbox: [10, 53.5, 11, 53.4], - }, - { - description: 'uri4', - bbox: [11.5, 48.5, 11.5, 48.3], - }, - // this extent is linked to a keyword not available locally - { - description: 'URI-Paris', - bbox: [1, 2, 3, 4], - }, - // this extent is not linked to any keyword - { - bbox: [5, 6, 7, 8], - }, -] - -const SAMPLE_RECORD = { - ...datasetRecordsFixture()[0], - spatialExtents: SAMPLE_SPATIAL_EXTENTS, - keywords: [ - ...datasetRecordsFixture()[0].keywords, - ...SAMPLE_PLACE_KEYWORDS_FROM_XML, - ], -} - class PlatformServiceInterfaceMock { // this simulates a search of a complete keyword with bbox, key... // only thesaurus 1 is known @@ -354,69 +261,6 @@ describe('FormFieldSpatialExtentComponent', () => { }) }) describe('spatial coverage', () => { - describe('switch toggle option is based on the keywords present in the record', () => { - it('should return true if the record has a national keyword', async () => { - const keywords = [ - ...SAMPLE_PLACE_KEYWORDS, - NATIONAL_KEYWORD, - ] as Keyword[] - editorFacade = TestBed.inject(EditorFacade) - editorFacade.record$ = from([ - { ...SAMPLE_RECORD, keywords } as CatalogRecord, - ]) - fixture = TestBed.createComponent(FormFieldSpatialExtentComponent) - component = fixture.componentInstance - fixture.detectChanges() - - const results = await firstValueFrom(component.switchToggleOptions$) - const nationalOption = results.filter( - (result) => result.label === 'National' - )[0] - - expect(nationalOption.checked).toBe(true) - }) - it('should return false if the record does not have a national keyword', async () => { - const keywords2 = [...SAMPLE_PLACE_KEYWORDS] as Keyword[] - editorFacade = TestBed.inject(EditorFacade) - editorFacade.record$ = from([ - { ...SAMPLE_RECORD, keywords: keywords2 } as CatalogRecord, - ]) - fixture = TestBed.createComponent(FormFieldSpatialExtentComponent) - component = fixture.componentInstance - fixture.detectChanges() - - const results = await firstValueFrom(component.switchToggleOptions$) - const nationalOption = results.filter( - (result) => result.label === 'National' - )[0] - - expect(nationalOption.checked).toBe(false) - }) - }) - describe('#onSpatialScopeChange', () => { - it('removes all existing spatial scope keywords and add the selected one', async () => { - const spatialScopes = [{ label: 'National' }, { label: 'Regional' }] - - const allKeywords = await firstValueFrom(component.allKeywords$) - const filteredKeywords = allKeywords.filter((keyword) => { - const spatialScopeLabels = spatialScopes.map((scope) => scope.label) - return !spatialScopeLabels.includes(keyword.label) - }) - - const selectedOption = { - label: 'National', - value: NATIONAL_KEYWORD, - checked: true, - } - await component.onSpatialScopeChange(selectedOption) - - expect(editorFacade.updateRecordField).toHaveBeenCalledWith( - 'keywords', - [...filteredKeywords, NATIONAL_KEYWORD] - ) - }) - }) - describe('#emitChanges', () => { const allKeywords = [ ...datasetRecordsFixture()[0].keywords, diff --git a/libs/feature/editor/src/lib/components/record-form/form-field/form-field-spatial-toggle/form-field-spatial-toggle.component.spec.ts b/libs/feature/editor/src/lib/components/record-form/form-field/form-field-spatial-toggle/form-field-spatial-toggle.component.spec.ts index 05063dfb46..04068df92a 100644 --- a/libs/feature/editor/src/lib/components/record-form/form-field/form-field-spatial-toggle/form-field-spatial-toggle.component.spec.ts +++ b/libs/feature/editor/src/lib/components/record-form/form-field/form-field-spatial-toggle/form-field-spatial-toggle.component.spec.ts @@ -3,20 +3,35 @@ import { ComponentFixture, TestBed } from '@angular/core/testing' import { FormFieldSpatialToggleComponent } from './form-field-spatial-toggle.component' import { MockProvider } from 'ng-mocks' import { EditorFacade } from '../../../../+state/editor.facade' -import { BehaviorSubject } from 'rxjs' +import { BehaviorSubject, firstValueFrom, from } from 'rxjs' +import { + datasetRecordsFixture, + NATIONAL_KEYWORD, + SAMPLE_PLACE_KEYWORDS, + SAMPLE_RECORD, +} from '@geonetwork-ui/common/fixtures' +import { + CatalogRecord, + Keyword, +} from '@geonetwork-ui/common/domain/model/record' +import { TranslateModule } from '@ngx-translate/core' -class EditorFacadeMock { - record$ = new BehaviorSubject([]) -} describe('FormFieldSpatialToggleComponent', () => { let component: FormFieldSpatialToggleComponent let fixture: ComponentFixture + let editorFacade: EditorFacade beforeEach(() => { TestBed.configureTestingModule({ - imports: [FormFieldSpatialToggleComponent], - providers: [MockProvider(EditorFacade, EditorFacadeMock, 'useClass')], + imports: [FormFieldSpatialToggleComponent, TranslateModule.forRoot()], + providers: [ + MockProvider(EditorFacade, { + record$: new BehaviorSubject(SAMPLE_RECORD), + updateRecordField: jest.fn(), + }), + ], }) + editorFacade = TestBed.inject(EditorFacade) fixture = TestBed.createComponent(FormFieldSpatialToggleComponent) component = fixture.componentInstance fixture.detectChanges() @@ -25,4 +40,64 @@ describe('FormFieldSpatialToggleComponent', () => { it('should create', () => { expect(component).toBeTruthy() }) + + describe('switch toggle option is based on the keywords present in the record', () => { + it('should return true if the record has a national keyword', async () => { + const keywords = [...SAMPLE_PLACE_KEYWORDS, NATIONAL_KEYWORD] as Keyword[] + editorFacade = TestBed.inject(EditorFacade) + editorFacade.record$ = from([ + { ...SAMPLE_RECORD, keywords } as CatalogRecord, + ]) + fixture = TestBed.createComponent(FormFieldSpatialToggleComponent) + component = fixture.componentInstance + fixture.detectChanges() + + const results = await firstValueFrom(component.switchToggleOptions$) + const nationalOption = results.filter( + (result) => result.label === 'National' + )[0] + + expect(nationalOption.checked).toBe(true) + }) + it('should return false if the record does not have a national keyword', async () => { + const keywords2 = [...SAMPLE_PLACE_KEYWORDS] as Keyword[] + editorFacade = TestBed.inject(EditorFacade) + editorFacade.record$ = from([ + { ...SAMPLE_RECORD, keywords: keywords2 } as CatalogRecord, + ]) + fixture = TestBed.createComponent(FormFieldSpatialToggleComponent) + component = fixture.componentInstance + fixture.detectChanges() + + const results = await firstValueFrom(component.switchToggleOptions$) + const nationalOption = results.filter( + (result) => result.label === 'National' + )[0] + + expect(nationalOption.checked).toBe(false) + }) + }) + describe('#onSpatialScopeChange', () => { + it('removes all existing spatial scope keywords and add the selected one', async () => { + const spatialScopes = [{ label: 'National' }, { label: 'Regional' }] + + const allKeywords = await firstValueFrom(component.allKeywords$) + const filteredKeywords = allKeywords.filter((keyword) => { + const spatialScopeLabels = spatialScopes.map((scope) => scope.label) + return !spatialScopeLabels.includes(keyword.label) + }) + + const selectedOption = { + label: 'National', + value: NATIONAL_KEYWORD, + checked: true, + } + await component.onSpatialScopeChange(selectedOption) + + expect(editorFacade.updateRecordField).toHaveBeenCalledWith('keywords', [ + ...filteredKeywords, + NATIONAL_KEYWORD, + ]) + }) + }) })