Skip to content

Commit

Permalink
Merge pull request #540 from IQSS/fix/528-use-fields-from-parent-form…
Browse files Browse the repository at this point in the history
…-behavior

Fix/528 Use fields from parent - Create Collection form behavior
  • Loading branch information
ofahimIQSS authored Nov 8, 2024
2 parents 0a25d00 + 355afad commit 9ec7a9d
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 20 deletions.
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { ChangeEvent, useId, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { Controller, UseControllerProps, useFormContext, useWatch } from 'react-hook-form'
import { Form } from '@iqss/dataverse-design-system'
import { MetadataBlockName } from '../../../../../metadata-block-info/domain/models/MetadataBlockInfo'
import { ConfirmResetModificationsModal } from './ConfirmResetModificationsModal'
import {
CollectionFormData,
Expand All @@ -11,15 +10,6 @@ import {
USE_FIELDS_FROM_PARENT
} from '../../CollectionForm'

const ALL_INPUT_LEVEL_FIELDS = [
MetadataBlockName.CITATION,
MetadataBlockName.GEOSPATIAL,
MetadataBlockName.SOCIAL_SCIENCE,
MetadataBlockName.ASTROPHYSICS,
MetadataBlockName.BIOMEDICAL,
MetadataBlockName.JOURNAL
]

interface FieldsFromParentCheckboxProps {
defaultValues: CollectionFormData
}
Expand All @@ -34,12 +24,11 @@ export const FieldsFromParentCheckbox = ({ defaultValues }: FieldsFromParentChec
const handleContinueWithReset = () => {
setValue(USE_FIELDS_FROM_PARENT, true)

// Reset metadata block names checboxes to the inital value
ALL_INPUT_LEVEL_FIELDS.forEach((blockName) => {
setValue(
`${METADATA_BLOCKS_NAMES_GROUPER}.${blockName}`,
defaultValues[METADATA_BLOCKS_NAMES_GROUPER][blockName]
)
const metadataBlockDefaultValues = Object.entries(defaultValues[METADATA_BLOCKS_NAMES_GROUPER])

// Reset metadata block names checkboxes to the inital value
metadataBlockDefaultValues.forEach(([blockName, blockInitialValue]) => {
setValue(`${METADATA_BLOCKS_NAMES_GROUPER}.${blockName}`, blockInitialValue)
})

// Reset input levels to the initial value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
CollectionFormData,
CollectionFormValuesOnSubmit,
INPUT_LEVELS_GROUPER,
METADATA_BLOCKS_NAMES_GROUPER
METADATA_BLOCKS_NAMES_GROUPER,
USE_FIELDS_FROM_PARENT
} from './CollectionForm'
import { RouteWithParams } from '../../Route.enum'
import { JSDataverseWriteErrorHandler } from '../../../shared/helpers/JSDataverseWriteErrorHandler'
Expand Down Expand Up @@ -65,15 +66,17 @@ export function useSubmitCollection(

const facetIdsDTO = formData.facetIds.map((facet) => facet.value)

const useFieldsFromParentChecked = formData[USE_FIELDS_FROM_PARENT]

const newCollection: CollectionDTO = {
name: formData.name,
alias: formData.alias,
type: formData.type,
affiliation: formData.affiliation,
description: formData.description,
contacts: contactsDTO,
metadataBlockNames: metadataBlockNamesDTO,
inputLevels: inputLevelsDTO,
metadataBlockNames: useFieldsFromParentChecked ? undefined : metadataBlockNamesDTO,
inputLevels: useFieldsFromParentChecked ? undefined : inputLevelsDTO,
facetIds: facetIdsDTO
}

Expand Down
70 changes: 70 additions & 0 deletions tests/component/sections/create-collection/CollectionForm.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { MetadataBlockInfoMother } from '../../metadata-block-info/domain/models
import { CollectionFormHelper } from '../../../../src/sections/create-collection/collection-form/CollectionFormHelper'
import { MetadataBlockName } from '../../../../src/metadata-block-info/domain/models/MetadataBlockInfo'
import { CollectionFacetMother } from '../../collection/domain/models/CollectionFacetMother'
import { CollectionDTO } from '@/collection/domain/useCases/DTOs/CollectionDTO'

const collectionRepository: CollectionRepository = {} as CollectionRepository

Expand Down Expand Up @@ -547,6 +548,75 @@ describe('CollectionForm', () => {

cy.findByLabelText('Subtitle').should('be.checked')
})

it('should send metadataBlockNames and inputLevels as undefined if use fields from parent is checked', () => {
const collectionRepository = {} as CollectionRepository
collectionRepository.create = cy.stub().as('createCollection').resolves()

cy.customMount(
<CollectionForm
collectionRepository={collectionRepository}
ownerCollectionId={OWNER_COLLECTION_ID}
defaultValues={formDefaultValues}
allMetadataBlocksInfo={allMetadataBlocksMock}
defaultCollectionFacets={defaultCollectionFacetsMock}
allFacetableMetadataFields={allFacetableMetadataFields}
/>
)
// Accept suggestion
cy.findByRole('button', { name: 'Apply suggestion' }).click()
// Select a Category option
cy.findByLabelText(/^Category/i).select(1)

cy.findByRole('button', { name: 'Create Collection' }).click()

cy.get('@createCollection').should((spy) => {
const createCollectionSpy = spy as unknown as Cypress.Agent<sinon.SinonSpy>
const collectionDTO = createCollectionSpy.getCall(0).args[0] as CollectionDTO

const inputLevels = collectionDTO.inputLevels
const metadataBlockNames = collectionDTO.metadataBlockNames

expect(inputLevels).to.be.undefined
expect(metadataBlockNames).to.be.undefined
})
})

it('should not send metadataBlockNames and inputLevels as undefined if use fields from parent is unchecked', () => {
const collectionRepository = {} as CollectionRepository
collectionRepository.create = cy.stub().as('createCollection').resolves()

cy.customMount(
<CollectionForm
collectionRepository={collectionRepository}
ownerCollectionId={OWNER_COLLECTION_ID}
defaultValues={formDefaultValues}
allMetadataBlocksInfo={allMetadataBlocksMock}
defaultCollectionFacets={defaultCollectionFacetsMock}
allFacetableMetadataFields={allFacetableMetadataFields}
/>
)

cy.get('@useFieldsFromParentCheckbox').uncheck({ force: true })

// Accept suggestion
cy.findByRole('button', { name: 'Apply suggestion' }).click()
// Select a Category option
cy.findByLabelText(/^Category/i).select(1)

cy.findByRole('button', { name: 'Create Collection' }).click()

cy.get('@createCollection').should((spy) => {
const createCollectionSpy = spy as unknown as Cypress.Agent<sinon.SinonSpy>
const collectionDTO = createCollectionSpy.getCall(0).args[0] as CollectionDTO

const inputLevels = collectionDTO.inputLevels
const metadataBlockNames = collectionDTO.metadataBlockNames

expect(inputLevels).to.not.be.undefined
expect(metadataBlockNames).to.not.be.undefined
})
})
})

describe('InputLevelFieldRow', () => {
Expand Down

0 comments on commit 9ec7a9d

Please sign in to comment.