From a2e96aea037aff7df9026d01ba88397a138c44e6 Mon Sep 17 00:00:00 2001 From: Olivia Guyot Date: Thu, 7 Nov 2024 14:01:49 +0100 Subject: [PATCH] feat(editor): do not remove all constraints when toggling off no known/no applicable Also addresses review comments --- .../constraints.utils.spec.ts | 4 +- ...ld-constraints-shortcuts.component.spec.ts | 47 +++++++++++++++++-- ...m-field-constraints-shortcuts.component.ts | 15 ++++-- .../form-field-constraints.component.spec.ts | 11 +++-- .../form-field-constraints.component.ts | 6 +-- 5 files changed, 68 insertions(+), 15 deletions(-) diff --git a/libs/feature/editor/src/lib/components/record-form/form-field/form-field-constraints-shortcuts/constraints.utils.spec.ts b/libs/feature/editor/src/lib/components/record-form/form-field/form-field-constraints-shortcuts/constraints.utils.spec.ts index f3f862d29..b43b5f794 100644 --- a/libs/feature/editor/src/lib/components/record-form/form-field/form-field-constraints-shortcuts/constraints.utils.spec.ts +++ b/libs/feature/editor/src/lib/components/record-form/form-field/form-field-constraints-shortcuts/constraints.utils.spec.ts @@ -44,7 +44,7 @@ describe('constraints utils', () => { }) ).toBe(true) }) - it('returns false otherwide', () => { + it('returns false otherwise', () => { expect( matchesNoApplicableConstraint({ text: ' bonjour monde ', @@ -88,7 +88,7 @@ describe('constraints utils', () => { }) ).toBe(true) }) - it('returns false otherwide', () => { + it('returns false otherwise', () => { expect( matchesNoKnownConstraint({ text: ' bonjour monde ', diff --git a/libs/feature/editor/src/lib/components/record-form/form-field/form-field-constraints-shortcuts/form-field-constraints-shortcuts.component.spec.ts b/libs/feature/editor/src/lib/components/record-form/form-field/form-field-constraints-shortcuts/form-field-constraints-shortcuts.component.spec.ts index 9d146cfcf..b2f16b48d 100644 --- a/libs/feature/editor/src/lib/components/record-form/form-field/form-field-constraints-shortcuts/form-field-constraints-shortcuts.component.spec.ts +++ b/libs/feature/editor/src/lib/components/record-form/form-field/form-field-constraints-shortcuts/form-field-constraints-shortcuts.component.spec.ts @@ -84,6 +84,24 @@ describe('FormFieldConstraintsShortcutsComponent', () => { }) describe('onToggleChange', () => { + beforeEach(() => { + sampleRecord$.next({ + ...sampleRecord, + legalConstraints: [ + { + text: 'no known', + url: NOT_KNOWN_CONSTRAINT.url, + }, + { + text: 'another constraint', + }, + { + text: 'no applicable', + url: NOT_APPLICABLE_CONSTRAINT.url, + }, + ], + }) + }) it('should update legal constraints and hide all sections when noApplicableConstraint toggled on', () => { component.onToggleChange('noApplicableConstraint', true) expect(editorFacade.updateRecordField).toHaveBeenCalledWith( @@ -123,11 +141,34 @@ describe('FormFieldConstraintsShortcutsComponent', () => { false ) }) - it('should remove all legal constraints when toggled off', () => { - component.onToggleChange('noApplicableConstraint', false) + it('should remove all legal constraints matching "no applicable" when toggled off', async () => { + await component.onToggleChange('noApplicableConstraint', false) + expect(editorFacade.updateRecordField).toHaveBeenCalledWith( + 'legalConstraints', + [ + { + text: 'no known', + url: NOT_KNOWN_CONSTRAINT.url, + }, + { + text: 'another constraint', + }, + ] + ) + }) + it('should remove all legal constraints matching "no known" when toggled off', async () => { + await component.onToggleChange('noKnownConstraint', false) expect(editorFacade.updateRecordField).toHaveBeenCalledWith( 'legalConstraints', - [] + [ + { + text: 'another constraint', + }, + { + text: 'no applicable', + url: NOT_APPLICABLE_CONSTRAINT.url, + }, + ] ) }) }) diff --git a/libs/feature/editor/src/lib/components/record-form/form-field/form-field-constraints-shortcuts/form-field-constraints-shortcuts.component.ts b/libs/feature/editor/src/lib/components/record-form/form-field/form-field-constraints-shortcuts/form-field-constraints-shortcuts.component.ts index 7e9232ffe..0dcbeb638 100644 --- a/libs/feature/editor/src/lib/components/record-form/form-field/form-field-constraints-shortcuts/form-field-constraints-shortcuts.component.ts +++ b/libs/feature/editor/src/lib/components/record-form/form-field/form-field-constraints-shortcuts/form-field-constraints-shortcuts.component.ts @@ -12,6 +12,7 @@ import { MatIconModule } from '@angular/material/icon' import { combineLatest, distinctUntilChanged, + firstValueFrom, map, Observable, Subject, @@ -145,7 +146,7 @@ export class FormFieldConstraintsShortcutsComponent this.editorFacade.setFieldVisibility({ model: 'otherConstraints' }, false) } - onToggleChange( + async onToggleChange( toggleName: 'noApplicableConstraint' | 'noKnownConstraint', value: boolean ) { @@ -159,8 +160,16 @@ export class FormFieldConstraintsShortcutsComponent ]) this.hideAllConstraintSections() } else { - // if the toggle is turned off, remove the constraint - this.editorFacade.updateRecordField('legalConstraints', []) //remove all legal constraints + const matcher = + toggleName === 'noApplicableConstraint' + ? matchesNoApplicableConstraint + : matchesNoKnownConstraint + // if the toggle is turned off, remove all matching constraints + const constraints = await firstValueFrom(this.legalConstraints$) + this.editorFacade.updateRecordField( + 'legalConstraints', + constraints.filter((c) => !matcher(c)) + ) } } diff --git a/libs/feature/editor/src/lib/components/record-form/form-field/form-field-constraints/form-field-constraints.component.spec.ts b/libs/feature/editor/src/lib/components/record-form/form-field/form-field-constraints/form-field-constraints.component.spec.ts index d12302804..a92a9096e 100644 --- a/libs/feature/editor/src/lib/components/record-form/form-field/form-field-constraints/form-field-constraints.component.spec.ts +++ b/libs/feature/editor/src/lib/components/record-form/form-field/form-field-constraints/form-field-constraints.component.spec.ts @@ -8,9 +8,11 @@ import { datasetRecordsFixture } from '@geonetwork-ui/common/fixtures' import { importProvidersFrom } from '@angular/core' import { TranslateModule } from '@ngx-translate/core' +const mockLegalConstraints = [...datasetRecordsFixture()[0].legalConstraints] + const mockConstraints = new BehaviorSubject([ { - legalConstraints: [...datasetRecordsFixture()[0].legalConstraints], + legalConstraints: mockLegalConstraints, securityConstraints: [], otherConstraints: [], }, @@ -65,9 +67,10 @@ describe('FormFieldConstraintsComponent', () => { jest.spyOn(component.valueChange, 'emit') const newConstraint = { text: 'aaa', url: new URL('http://example.com') } component.handleConstraintChange(newConstraint, 0) - - expect(component.value[0]).toBe(newConstraint) - expect(component.valueChange.emit).toHaveBeenCalledWith(component.value) + expect(component.valueChange.emit).toHaveBeenCalledWith([ + newConstraint, + mockLegalConstraints[1], + ]) }) it('#handleConstraintsOrderChange should emit the new value', () => { diff --git a/libs/feature/editor/src/lib/components/record-form/form-field/form-field-constraints/form-field-constraints.component.ts b/libs/feature/editor/src/lib/components/record-form/form-field/form-field-constraints/form-field-constraints.component.ts index cda68ccaa..7b1d8dd7f 100644 --- a/libs/feature/editor/src/lib/components/record-form/form-field/form-field-constraints/form-field-constraints.component.ts +++ b/libs/feature/editor/src/lib/components/record-form/form-field/form-field-constraints/form-field-constraints.component.ts @@ -56,9 +56,9 @@ export class FormFieldConstraintsComponent implements OnInit { } handleConstraintChange(constraint: Constraint, index: number) { - this.value = [...this.value] - this.value[index] = constraint - this.valueChange.emit(this.value) + const newValue = [...this.value] + newValue[index] = constraint + this.valueChange.emit(newValue) } handleConstraintsOrderChange(constraints: Constraint[]) {