Skip to content

Commit

Permalink
feat(editor): do not remove all constraints when toggling off no know…
Browse files Browse the repository at this point in the history
…n/no applicable

Also addresses review comments
  • Loading branch information
jahow committed Nov 7, 2024
1 parent d415607 commit a2e96ae
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('constraints utils', () => {
})
).toBe(true)
})
it('returns false otherwide', () => {
it('returns false otherwise', () => {
expect(
matchesNoApplicableConstraint({
text: ' bonjour monde ',
Expand Down Expand Up @@ -88,7 +88,7 @@ describe('constraints utils', () => {
})
).toBe(true)
})
it('returns false otherwide', () => {
it('returns false otherwise', () => {
expect(
matchesNoKnownConstraint({
text: ' bonjour monde ',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
},
]
)
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { MatIconModule } from '@angular/material/icon'
import {
combineLatest,
distinctUntilChanged,
firstValueFrom,
map,
Observable,
Subject,
Expand Down Expand Up @@ -145,7 +146,7 @@ export class FormFieldConstraintsShortcutsComponent
this.editorFacade.setFieldVisibility({ model: 'otherConstraints' }, false)
}

onToggleChange(
async onToggleChange(
toggleName: 'noApplicableConstraint' | 'noKnownConstraint',
value: boolean
) {
Expand All @@ -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))
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
},
Expand Down Expand Up @@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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[]) {
Expand Down

0 comments on commit a2e96ae

Please sign in to comment.