Skip to content

Commit efb61d4

Browse files
committed
test: specs for dependant required fields
1 parent bc93ba4 commit efb61d4

File tree

2 files changed

+287
-16
lines changed

2 files changed

+287
-16
lines changed

tests/component/sections/dataset/dataset-files/DatasetFilesScrollable.spec.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,9 +361,11 @@ describe('DatasetFilesScrollable', () => {
361361
/>
362362
)
363363
cy.findByRole('columnheader', { name: '10 of 200 Files displayed' }).should('exist')
364-
cy.get('table > thead > tr > th > input[type=checkbox]').click()
364+
cy.get('table > thead > tr > th > input[type=checkbox]').click({ force: true })
365365
cy.findByText('10 files are currently selected.').should('exist')
366-
cy.findByRole('button', { name: 'Select all 200 files in this dataset.' }).click()
366+
cy.findByRole('button', { name: 'Select all 200 files in this dataset.' }).click({
367+
force: true
368+
})
367369
cy.findByText('200 files are currently selected.').should('exist')
368370
})
369371

tests/component/sections/shared/dataset-metadata-form/DatasetMetadataForm.spec.tsx

Lines changed: 283 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { fireEvent } from '@testing-library/react'
12
import { MetadataBlockName } from '../../../../../src/dataset/domain/models/Dataset'
23
import { DatasetRepository } from '../../../../../src/dataset/domain/repositories/DatasetRepository'
34
import { TypeMetadataFieldOptions } from '../../../../../src/metadata-block-info/domain/models/MetadataBlockInfo'
@@ -85,13 +86,73 @@ const metadataBlocksInfoOnCreateModeWithAstroBlock =
8586
}
8687
})
8788

89+
const metadataBlocksInfoOnCreateModeWithComposedNotMultipleField =
90+
MetadataBlockInfoMother.getByCollectionIdDisplayedOnCreateTrue({
91+
id: 4,
92+
name: 'astrophysics',
93+
displayName: 'Astronomy and Astrophysics Metadata',
94+
displayOnCreate: false,
95+
metadataFields: {
96+
producer: {
97+
name: 'producer',
98+
displayName: 'Producer',
99+
title: 'Producer',
100+
type: 'NONE',
101+
watermark: '',
102+
description:
103+
'The entity, such a person or organization, managing the finances or other administrative processes involved in the creation of the Dataset',
104+
multiple: false,
105+
isControlledVocabulary: false,
106+
displayFormat: '',
107+
isRequired: false,
108+
displayOrder: 36,
109+
typeClass: 'compound',
110+
displayOnCreate: false,
111+
childMetadataFields: {
112+
producerName: {
113+
name: 'producerName',
114+
displayName: 'Producer Name',
115+
title: 'Name',
116+
type: 'TEXT',
117+
watermark: '1) FamilyName, GivenName or 2) Organization',
118+
description:
119+
"The name of the entity, e.g. the person's name or the name of an organization",
120+
multiple: false,
121+
isControlledVocabulary: false,
122+
displayFormat: '#VALUE',
123+
isRequired: true,
124+
displayOrder: 37,
125+
typeClass: 'primitive',
126+
displayOnCreate: false
127+
},
128+
producerAffiliation: {
129+
name: 'producerAffiliation',
130+
displayName: 'Producer Affiliation',
131+
title: 'Affiliation',
132+
type: 'TEXT',
133+
watermark: 'Organization XYZ',
134+
description:
135+
"The name of the entity affiliated with the producer, e.g. an organization's name",
136+
multiple: false,
137+
isControlledVocabulary: false,
138+
displayFormat: '(#VALUE)',
139+
isRequired: false,
140+
displayOrder: 38,
141+
typeClass: 'primitive',
142+
displayOnCreate: false
143+
}
144+
}
145+
}
146+
}
147+
})
148+
88149
const metadataBlocksInfoOnEditMode =
89150
MetadataBlockInfoMother.getByCollectionIdDisplayedOnCreateFalse()
90151
const wrongCollectionMetadataBlocksInfo =
91152
MetadataBlockInfoMother.wrongCollectionMetadataBlocksInfo()
92153
const testUser = UserMother.create()
93154

94-
const fillRequiredFields = () => {
155+
const fillRequiredFieldsOnCreate = () => {
95156
cy.findByLabelText(/^Title/i).type('Test Dataset Title')
96157

97158
cy.findByText('Author')
@@ -210,16 +271,6 @@ describe('DatasetMetadataForm', () => {
210271
.should('exist')
211272
.should('have.attr', 'aria-required', 'true')
212273
.should('have.data', 'fieldtype', TypeMetadataFieldOptions.Text)
213-
// cy.findByText('Title').children('div').trigger('mouseover')
214-
// cy.document().its('body').findByText('The main title of the Dataset').should('exist')
215-
216-
// cy.findByText('Subtitle').children('div').trigger('mouseover')
217-
// cy.document()
218-
// .its('body')
219-
// .findByText(
220-
// 'A secondary title that amplifies or states certain limitations on the main title'
221-
// )
222-
// .should('exist')
223274

224275
// Composed field
225276
cy.findByText('Author')
@@ -1226,7 +1277,7 @@ describe('DatasetMetadataForm', () => {
12261277
metadataBlockInfoRepository={metadataBlockInfoRepository}
12271278
/>
12281279
)
1229-
fillRequiredFields()
1280+
fillRequiredFieldsOnCreate()
12301281
cy.findByText(/Save Dataset/i).click()
12311282
cy.findByText('Title is required').should('not.exist')
12321283
cy.findByText('Author Name is required').should('not.exist')
@@ -1458,6 +1509,7 @@ describe('DatasetMetadataForm', () => {
14581509

14591510
cy.findByText('Error').should('exist')
14601511
})
1512+
14611513
it('cancel button is clickable', () => {
14621514
cy.customMount(
14631515
<DatasetMetadataForm
@@ -1542,7 +1594,7 @@ describe('DatasetMetadataForm', () => {
15421594
/>
15431595
)
15441596
// Fields are being send correctly, we are just forcing a create error to check if the error message is being displayed correctly
1545-
fillRequiredFields()
1597+
fillRequiredFieldsOnCreate()
15461598

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

@@ -1564,7 +1616,7 @@ describe('DatasetMetadataForm', () => {
15641616
/>
15651617
)
15661618

1567-
fillRequiredFields()
1619+
fillRequiredFieldsOnCreate()
15681620

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

@@ -1575,6 +1627,100 @@ describe('DatasetMetadataForm', () => {
15751627
})
15761628
})
15771629

1630+
describe('When dataset metadata update fails', () => {
1631+
it('should show edit error message from the client-javascript client when the dataset edition fails', () => {
1632+
datasetRepository.updateMetadata = cy
1633+
.stub()
1634+
.rejects(new Error('Error from the api javascript client'))
1635+
metadataBlockInfoRepository.getByColecctionId = cy
1636+
.stub()
1637+
.resolves(wrongCollectionMetadataBlocksInfo)
1638+
1639+
cy.customMount(
1640+
<DatasetMetadataForm
1641+
mode="edit"
1642+
collectionId="root"
1643+
datasetRepository={datasetRepository}
1644+
metadataBlockInfoRepository={metadataBlockInfoRepository}
1645+
datasetPersistentID={dataset.persistentId}
1646+
datasetMetadaBlocksCurrentValues={dataset.metadataBlocks}
1647+
/>
1648+
)
1649+
1650+
// Fill one non required field to undisable the Save button, is disabled if fields are not dirty
1651+
cy.findByLabelText('Title').type('Some note')
1652+
1653+
cy.findAllByText(/Save Changes/i)
1654+
.first()
1655+
.click()
1656+
1657+
cy.findByText('Validation Error').should('exist')
1658+
cy.findByText(/Error from the api javascript client/).should('exist')
1659+
})
1660+
1661+
it('should only show field error part of the error message from the api when the dataset edition fails', () => {
1662+
datasetRepository.updateMetadata = cy
1663+
.stub()
1664+
.rejects(
1665+
new Error(
1666+
'Validation Failed: Point of Contact E-mail [email protected] is not a valid email address. (Invalid value:edu.harvard.iq.dataverse.DatasetFieldValueValue[ id=null ]).java.util.stream.ReferencePipeline$3@561b5200'
1667+
)
1668+
)
1669+
1670+
cy.customMount(
1671+
<DatasetMetadataForm
1672+
mode="edit"
1673+
collectionId="root"
1674+
datasetRepository={datasetRepository}
1675+
metadataBlockInfoRepository={metadataBlockInfoRepository}
1676+
datasetPersistentID={dataset.persistentId}
1677+
datasetMetadaBlocksCurrentValues={dataset.metadataBlocks}
1678+
/>
1679+
)
1680+
1681+
cy.findByLabelText(/^Title/i)
1682+
.clear()
1683+
.type('New Title')
1684+
1685+
cy.findAllByText(/Save Changes/i)
1686+
.first()
1687+
.click()
1688+
1689+
cy.findByText('Validation Error').should('exist')
1690+
cy.findByText(/Point of Contact E-mail test@test.c is not a valid email address./).should(
1691+
'exist'
1692+
)
1693+
})
1694+
1695+
it('should show locale error message when the dataset edition fails with an unknown error message', () => {
1696+
datasetRepository.updateMetadata = cy.stub().rejects('Some not expected error')
1697+
1698+
cy.customMount(
1699+
<DatasetMetadataForm
1700+
mode="edit"
1701+
collectionId="root"
1702+
datasetRepository={datasetRepository}
1703+
metadataBlockInfoRepository={metadataBlockInfoRepository}
1704+
datasetPersistentID={dataset.persistentId}
1705+
datasetMetadaBlocksCurrentValues={dataset.metadataBlocks}
1706+
/>
1707+
)
1708+
1709+
cy.findByLabelText(/^Title/i)
1710+
.clear()
1711+
.type('New Title')
1712+
1713+
cy.findAllByText(/Save Changes/i)
1714+
.first()
1715+
.click()
1716+
1717+
cy.findByText('Validation Error').should('exist')
1718+
cy.findByText(
1719+
/Required fields were missed or there was a validation error. Please scroll down to see details./
1720+
).should('exist')
1721+
})
1722+
})
1723+
15781724
it('adds a new field and removes a field to a composed multiple field', () => {
15791725
cy.customMount(
15801726
<DatasetMetadataForm
@@ -1626,4 +1772,127 @@ describe('DatasetMetadataForm', () => {
16261772

16271773
cy.findByLabelText(`Delete Alternative Title`).should('not.exist')
16281774
})
1775+
1776+
it('should not submit the form when pressing enter key if submit button is not focused', () => {
1777+
cy.customMount(
1778+
<DatasetMetadataForm
1779+
mode="create"
1780+
collectionId="root"
1781+
datasetRepository={datasetRepository}
1782+
metadataBlockInfoRepository={metadataBlockInfoRepository}
1783+
/>
1784+
)
1785+
1786+
// We simulate using focusing on an input and pressing enter key
1787+
cy.findByLabelText(/^Title/i)
1788+
.focus()
1789+
.type('{enter}')
1790+
1791+
// Validation error shouldn't be shown as form wasn't submitted
1792+
cy.findByText('Title is required').should('not.exist')
1793+
})
1794+
it('should submit the form when pressing enter key if submit button is indeed focused', () => {
1795+
cy.customMount(
1796+
<DatasetMetadataForm
1797+
mode="create"
1798+
collectionId="root"
1799+
datasetRepository={datasetRepository}
1800+
metadataBlockInfoRepository={metadataBlockInfoRepository}
1801+
/>
1802+
)
1803+
1804+
// Type something so submit button is not disabled
1805+
cy.findByLabelText(/^Title/i).type('Some title')
1806+
1807+
cy.findByText(/Save Dataset/i)
1808+
.focus()
1809+
.type('{enter}')
1810+
1811+
// Validation error shouldn be shown as form was submitted
1812+
cy.findByText('Author Name is required').should('exist')
1813+
})
1814+
1815+
describe('should make field required if some of the siblings are filled and viceversa and show helper message', () => {
1816+
it('for a composed field multiple', () => {
1817+
cy.customMount(
1818+
<DatasetMetadataForm
1819+
mode="edit"
1820+
collectionId="root"
1821+
datasetRepository={datasetRepository}
1822+
metadataBlockInfoRepository={metadataBlockInfoRepository}
1823+
datasetPersistentID={dataset.persistentId}
1824+
datasetMetadaBlocksCurrentValues={dataset.metadataBlocks}
1825+
/>
1826+
)
1827+
1828+
cy.findByText('Producer')
1829+
.should('exist')
1830+
.closest('.row')
1831+
.within(() => {
1832+
cy.findByText(
1833+
'One or more of these fields may become required if you add to one or more of these optional fields.'
1834+
).should('exist')
1835+
1836+
cy.findByLabelText('Name', { exact: true })
1837+
.should('exist')
1838+
.should('have.attr', 'aria-required', 'false')
1839+
1840+
cy.findByLabelText('Affiliation', { exact: true })
1841+
.should('exist')
1842+
.type('something to trigger sibling Name field to become required')
1843+
1844+
cy.findByLabelText(/^Name/).should('exist').should('have.attr', 'aria-required', 'true')
1845+
1846+
cy.findByLabelText('Affiliation', { exact: true }).clear()
1847+
1848+
cy.findByLabelText(/^Name/).should('exist').should('have.attr', 'aria-required', 'false')
1849+
})
1850+
})
1851+
it('for a composed field NOT multiple', () => {
1852+
metadataBlockInfoRepository.getByColecctionId = cy
1853+
.stub()
1854+
.resolves(metadataBlocksInfoOnCreateModeWithComposedNotMultipleField)
1855+
1856+
cy.customMount(
1857+
<DatasetMetadataForm
1858+
mode="edit"
1859+
collectionId="root"
1860+
datasetRepository={datasetRepository}
1861+
metadataBlockInfoRepository={metadataBlockInfoRepository}
1862+
datasetPersistentID={dataset.persistentId}
1863+
datasetMetadaBlocksCurrentValues={dataset.metadataBlocks}
1864+
/>
1865+
)
1866+
1867+
cy.get('.accordion > :nth-child(3)').within(() => {
1868+
// Open accordion and wait for it to open
1869+
cy.get('.accordion-button').click()
1870+
cy.wait(300)
1871+
cy.findByText('Producer')
1872+
.should('exist')
1873+
.closest('.row')
1874+
.within(() => {
1875+
cy.findByText(
1876+
'One or more of these fields may become required if you add to one or more of these optional fields.'
1877+
).should('exist')
1878+
1879+
cy.findByLabelText('Name', { exact: true })
1880+
.should('exist')
1881+
.should('have.attr', 'aria-required', 'false')
1882+
1883+
cy.findByLabelText('Affiliation', { exact: true })
1884+
.should('exist')
1885+
.type('something to trigger sibling Name field to become required')
1886+
1887+
cy.findByLabelText(/^Name/).should('exist').should('have.attr', 'aria-required', 'true')
1888+
1889+
cy.findByLabelText('Affiliation', { exact: true }).clear()
1890+
1891+
cy.findByLabelText(/^Name/)
1892+
.should('exist')
1893+
.should('have.attr', 'aria-required', 'false')
1894+
})
1895+
})
1896+
})
1897+
})
16291898
})

0 commit comments

Comments
 (0)