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

407 create dataset prefill #414

Merged
merged 16 commits into from
Jun 17, 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
19 changes: 17 additions & 2 deletions src/sections/create-dataset/DatasetForm.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MouseEvent, useMemo, useRef } from 'react'
import { MouseEvent, useEffect, useMemo, useRef } from 'react'
import { useNavigate } from 'react-router-dom'
import { FieldErrors, FormProvider, useForm } from 'react-hook-form'
import { useTranslation } from 'react-i18next'
Expand All @@ -12,6 +12,7 @@ import { SeparationLine } from '../shared/layout/SeparationLine/SeparationLine'
import { MetadataBlockFormFields } from './MetadataBlockFormFields'
import { Route } from '../Route.enum'
import styles from './DatasetForm.module.scss'
import { useSession } from '../session/SessionContext'

interface DatasetFormProps {
repository: DatasetRepository
Expand Down Expand Up @@ -39,14 +40,28 @@ export const DatasetForm = ({
collectionId,
onCreateDatasetError
)
const { user } = useSession()

const isErrorLoadingMetadataBlocks = Boolean(errorLoadingMetadataBlocks)

const form = useForm({
mode: 'onChange',
defaultValues: formDefaultValues
})

const { setValue } = form
useEffect(() => {
if (user) {
setValue('citation.author.0.authorName', user.displayName)
setValue('citation.datasetContact.0.datasetContactName', user.displayName)
setValue('citation.datasetContact.0.datasetContactEmail', user.email, {
shouldValidate: true
})
if (user.affiliation) {
setValue('citation.datasetContact.0.datasetContactAffiliation', user.affiliation)
setValue('citation.author.0.authorAffiliation', user.affiliation)
}
}
}, [setValue, user])
const handleCancel = (event: MouseEvent<HTMLButtonElement>) => {
event.preventDefault()
navigate(Route.HOME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const Primitive = ({
name={builtFieldName}
control={control}
rules={rulesToApply}
render={({ field: { onChange, ref }, fieldState: { invalid, error } }) => (
render={({ field: { onChange, ref, value }, fieldState: { invalid, error } }) => (
<Col sm={withinMultipleFieldsGroup ? 12 : 9}>
<Row>
<Col sm={withinMultipleFieldsGroup ? 12 : 9}>
Expand All @@ -67,6 +67,7 @@ export const Primitive = ({
) : (
<Form.Group.Input
type="text"
value={value as string}
onChange={onChange}
isInvalid={invalid}
placeholder={watermark}
Expand Down
2 changes: 1 addition & 1 deletion src/sections/layout/header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function Header() {
{t('navigation.newDataset')}
</Navbar.Dropdown.Item>
</Navbar.Dropdown>
<Navbar.Dropdown title={user.name} id="dropdown-user">
<Navbar.Dropdown title={user.displayName} id="dropdown-user">
<Navbar.Dropdown.Item href="#" onClick={onLogoutClick}>
{t('logOut')}
</Navbar.Dropdown.Item>
Expand Down
3 changes: 2 additions & 1 deletion src/stories/create-dataset/CreateDataset.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import { MetadataBlockInfoMockRepository } from './MetadataBlockInfoMockReposito
import { MetadataBlockInfoMockLoadingRepository } from './MetadataBlockInfoMockLoadingRepository'
import { MetadataBlockInfoMockErrorRepository } from './MetadataBlockInfoMockErrorRepository'
import { NotImplementedModalProvider } from '../../sections/not-implemented/NotImplementedModalProvider'
import { WithLoggedInUser } from '../WithLoggedInUser'

const meta: Meta<typeof CreateDataset> = {
title: 'Pages/Create Dataset',
component: CreateDataset,
decorators: [WithI18next, WithLayout],
decorators: [WithI18next, WithLayout, WithLoggedInUser],
parameters: {
// Sets the delay for all stories.
chromatic: { delay: 15000, pauseAnimationAtEnd: true }
Expand Down
6 changes: 5 additions & 1 deletion src/users/domain/models/User.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export interface User {
name: string
displayName: string
persistentId: string
firstName: string
lastName: string
email: string
affiliation?: string
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ export class UserJSDataverseRepository implements UserRepository {
.execute()
.then((authenticatedUser: AuthenticatedUser) => {
return {
name: authenticatedUser.displayName,
persistentId: authenticatedUser.persistentUserId
displayName: authenticatedUser.displayName,
persistentId: authenticatedUser.persistentUserId,
firstName: authenticatedUser.firstName,
lastName: authenticatedUser.lastName,
email: authenticatedUser.email,
affiliation: authenticatedUser.affiliation
}
})
.catch((error: ReadError) => {
Expand Down
38 changes: 38 additions & 0 deletions tests/component/sections/create-dataset/CreateDataset.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ import { MetadataBlockInfoRepository } from '../../../../src/metadata-block-info
import { MetadataBlockInfoMother } from '../../metadata-block-info/domain/models/MetadataBlockInfoMother'
import { TypeMetadataFieldOptions } from '../../../../src/metadata-block-info/domain/models/MetadataBlockInfo'
import { NotImplementedModalProvider } from '../../../../src/sections/not-implemented/NotImplementedModalProvider'
import { UserMother } from '../../users/domain/models/UserMother'
import { UserRepository } from '../../../../src/users/domain/repositories/UserRepository'

const datasetRepository: DatasetRepository = {} as DatasetRepository
const metadataBlockInfoRepository: MetadataBlockInfoRepository = {} as MetadataBlockInfoRepository
const userRepository: UserRepository = {} as UserRepository

const collectionMetadataBlocksInfo =
MetadataBlockInfoMother.getByCollectionIdDisplayedOnCreateTrue()

const wrongCollectionMetadataBlocksInfo =
MetadataBlockInfoMother.wrongCollectionMetadataBlocksInfo()
const testUser = UserMother.create()

const fillRequiredFields = () => {
cy.findByLabelText(/^Title/i).type('Test Dataset Title')
Expand Down Expand Up @@ -66,7 +70,9 @@ describe('Create Dataset', () => {
metadataBlockInfoRepository.getDisplayedOnCreateByCollectionId = cy
.stub()
.resolves(collectionMetadataBlocksInfo)
userRepository.getAuthenticated = cy.stub().resolves(testUser)
})

it('renders the Host Collection Form', () => {
cy.customMount(
<NotImplementedModalProvider>
Expand All @@ -86,6 +92,38 @@ describe('Create Dataset', () => {
cy.findByText('Not Implemented').should('exist')
})
})
it('pre-fills the form with user data', () => {
cy.mountAuthenticated(
<NotImplementedModalProvider>
<CreateDataset
repository={datasetRepository}
collectionId={'test-collectionId'}
metadataBlockInfoRepository={metadataBlockInfoRepository}
/>
</NotImplementedModalProvider>
)
cy.findByText('Author')
.closest('.row')
.within(() => {
cy.findByLabelText(/^Name/i).should('have.value', testUser.displayName)
})
cy.findByText('Author')
.closest('.row')
.within(() => {
cy.findByLabelText(/^Affiliation/i).should('have.value', testUser.affiliation)
})
cy.findByText('Point of Contact')
.closest('.row')
.within(() => {
cy.findByLabelText(/^Name/i).should('have.value', testUser.displayName)
})
cy.findByText('Point of Contact')
.closest('.row')
.within(() => {
cy.findByLabelText(/^Affiliation/i).should('have.value', testUser.affiliation)
})
cy.findByLabelText(/^E-mail/i).should('have.value', testUser.email)
})
it('renders the Create Dataset page and its metadata blocks sections', () => {
cy.customMount(
<CreateDataset
Expand Down
8 changes: 4 additions & 4 deletions tests/component/sections/layout/header/Header.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ describe('Header component', () => {
cy.wrap(userRepository.getAuthenticated).should('be.calledOnce')

cy.findByRole('button', { name: 'Toggle navigation' }).click()
cy.findByText(testUser.name).should('be.visible')
cy.findByText(testUser.name).click()
cy.findByText(testUser.displayName).should('be.visible')
cy.findByText(testUser.displayName).click()
cy.findByText('Log Out').should('be.visible')
})

Expand Down Expand Up @@ -97,13 +97,13 @@ describe('Header component', () => {

cy.findByRole('button', { name: 'Toggle navigation' }).click()

cy.findByText(testUser.name).click()
cy.findByText(testUser.displayName).click()

cy.findByText('Log Out').click()

cy.wrap(userRepository.removeAuthenticated).should('be.calledOnce')

cy.findByText(testUser.name).should('not.exist')
cy.findByText(testUser.displayName).should('not.exist')

cy.findByText('Log In').should('exist')
cy.findByText('Sign Up').should('exist')
Expand Down
10 changes: 5 additions & 5 deletions tests/component/sections/session/useSession.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('useSession', () => {
function TestComponent() {
const { user } = useSession()

return <div>{user ? <span>{user.name}</span> : <></>}</div>
return <div>{user ? <span>{user.displayName}</span> : <></>}</div>
}

cy.mount(
Expand All @@ -26,7 +26,7 @@ describe('useSession', () => {
)

cy.wrap(userRepository.getAuthenticated).should('be.calledOnce')
cy.findByText(testUser.name).should('exist')
cy.findByText(testUser.displayName).should('exist')
})

it('should unset user after calling logOut on repository', () => {
Expand All @@ -38,7 +38,7 @@ describe('useSession', () => {

return (
<div>
{user ? <span>{user.name}</span> : <></>}
{user ? <span>{user.displayName}</span> : <></>}
<Button onClick={onLogoutClick}>Log Out</Button>
</div>
)
Expand All @@ -52,12 +52,12 @@ describe('useSession', () => {

cy.wrap(userRepository.getAuthenticated).should('be.calledOnce')

cy.findByText(testUser.name).should('exist')
cy.findByText(testUser.displayName).should('exist')

cy.findByText('Log Out').click()

cy.wrap(userRepository.removeAuthenticated).should('be.calledOnce')

cy.findByText(testUser.name).should('not.exist')
cy.findByText(testUser.displayName).should('not.exist')
})
})
8 changes: 6 additions & 2 deletions tests/component/users/domain/models/UserMother.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ import { User } from '../../../../../src/users/domain/models/User'
export class UserMother {
static create(): User {
return {
name: 'James D. Potts',
persistentId: 'jamesPotts'
displayName: 'James D. Potts',
persistentId: 'jamesPotts',
firstName: 'James',
lastName: 'Potts',
email: 'jamesPotts@g.harvard.edu',
affiliation: 'Harvard University'
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,6 @@ describe('Create Dataset', () => {
cy.visit('/spa/datasets/create')

cy.findByLabelText(/^Title/i).type('Test Dataset Title', { force: true })
cy.findByText('Author')
.closest('.row')
.within(() => {
cy.findByLabelText(/^Name/i).type('Test author name', { force: true })
})

cy.findByText('Point of Contact')
.closest('.row')
.within(() => {
cy.findByLabelText(/^E-mail/i).type('test@test.com', { force: true })
})

cy.findByText('Description')
.closest('.row')
Expand All @@ -45,7 +34,6 @@ describe('Create Dataset', () => {

cy.findByLabelText('Agricultural Sciences').click()
})

cy.findByText(/Save Dataset/i).click()

cy.findByRole('heading', { name: 'Test Dataset Title' }).should('exist')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ const userRepository = new UserJSDataverseRepository()
describe('User JSDataverse Repository', () => {
before(() => TestsUtils.setup())
beforeEach(() => TestsUtils.login())

it('gets the authenticated user', async () => {
const expectedUser = { name: 'Dataverse Admin', persistentId: 'dataverseAdmin' }
const expectedUser = {
displayName: 'Dataverse Admin',
persistentId: 'dataverseAdmin',
firstName: 'Dataverse',
lastName: 'Admin',
email: 'dataverse@mailinator.com',
affiliation: 'Dataverse.org'
}
const user = await userRepository.getAuthenticated()

expect(user).to.deep.equal(expectedUser)
Expand Down
Loading