Skip to content

Commit

Permalink
Added basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Mangan committed Dec 5, 2023
1 parent 69327f6 commit f402382
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/views/create-dataset/CreateDataset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export function CreateDataset() {
const { isLoading } = useDataset()
const { t } = useTranslation('createDataset')
const [submitting, setSubmitting] = useState(false)
const [submitStatus, setSubmitStatus] = useState(false)

useEffect(() => {
setIsLoading(isLoading)
}, [isLoading])
Expand All @@ -42,17 +44,20 @@ export function CreateDataset() {
setState({ ...state, [e.target.id]: value })
}

// TODO: Connect to API when ready
const handleCreateDatasetSubmit = (event: FormEvent<HTMLFormElement>) => {
setSubmitting(true)
event.preventDefault()
setTimeout(() => {
setSubmitting(false)
console.log('Form Submitted!')
setSubmitStatus(true)
}, 3000)
console.log(state)
// TODO: Callback for form submission should be here.
}

// TODO: Probably replace this with a FormSkeleton or remove entirely
if (isLoading) {
return <DatasetSkeleton />
}
Expand All @@ -66,11 +71,13 @@ export function CreateDataset() {
<RequiredFieldText />
{submitting && <div>Submtting Form...</div>}
<Form onSubmit={handleCreateDatasetSubmit}>
{submitStatus && <div>Form Submitted!</div>}
<Row>
<Col md={9}>
<Form.Group controlId="createDatasetTitle" required>
<Form.Group.Label>{t('datasetForm.title')}</Form.Group.Label>
<Form.Group.Input
data-cy="datasetFormInputTitle"
type="text"
placeholder="Dataset Title"
onChange={handleCreateDatasetFieldChange}
Expand Down Expand Up @@ -99,8 +106,10 @@ export function CreateDataset() {
action="#{DatasetPage.save()}"
update=":datasetForm,:messagePanel"
/> */}
<Button type="submit">{t('saveButton')}</Button>
<Button withSpacing variant="secondary">
<Button type="submit" data-cy="datasetFormSubmit">
{t('saveButton')}
</Button>
<Button withSpacing variant="secondary" data-cy="datasetFormCancel">
{t('cancelButton')}
</Button>
</Form>
Expand Down
24 changes: 24 additions & 0 deletions tests/component/views/create-dataset/CreateDataset.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { CreateDataset } from '../../../../src/views/create-dataset/CreateDataset'

describe('Form component', () => {
it('should render all form fields', () => {
cy.customMount(<CreateDataset />)
cy.get('[data-cy=datasetFormInputTitle]').should('be.visible')
cy.get('[data-cy=datasetFormSubmit]').should('be.visible')
cy.get('[data-cy=datasetFormCancel]').should('be.visible')
})

it('should submit the form with correct values', () => {
cy.customMount(<CreateDataset />)

cy.get('[data-cy=datasetFormInputTitle]').type('Test Dataset Title')

cy.get('[data-cy=datasetFormSubmit]').click()

// Wait for the form submission to complete (adjust the timeout as needed)
cy.wait(4000)

// Assert that the form was successfully submitted
cy.contains('Form Submitted!')
})
})

0 comments on commit f402382

Please sign in to comment.