Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/528 Use fields from parent - Create Collection form behavior #540

Merged
merged 5 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading