1+ import { fireEvent } from '@testing-library/react'
12import { MetadataBlockName } from '../../../../../src/dataset/domain/models/Dataset'
23import { DatasetRepository } from '../../../../../src/dataset/domain/repositories/DatasetRepository'
34import { 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+
88149const metadataBlocksInfoOnEditMode =
89150 MetadataBlockInfoMother . getByCollectionIdDisplayedOnCreateFalse ( )
90151const wrongCollectionMetadataBlocksInfo =
91152 MetadataBlockInfoMother . wrongCollectionMetadataBlocksInfo ( )
92153const testUser = UserMother . create ( )
93154
94- const fillRequiredFields = ( ) => {
155+ const fillRequiredFieldsOnCreate = ( ) => {
95156 cy . findByLabelText ( / ^ T i t l e / 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 ( / S a v e D a t a s e t / 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 ( / S a v e D a t a s e t / i) . click ( )
15481600
@@ -1564,7 +1616,7 @@ describe('DatasetMetadataForm', () => {
15641616 />
15651617 )
15661618
1567- fillRequiredFields ( )
1619+ fillRequiredFieldsOnCreate ( )
15681620
15691621 cy . findByText ( / S a v e D a t a s e t / 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 ( / S a v e C h a n g e s / i)
1654+ . first ( )
1655+ . click ( )
1656+
1657+ cy . findByText ( 'Validation Error' ) . should ( 'exist' )
1658+ cy . findByText ( / E r r o r f r o m t h e a p i j a v a s c r i p t c l i e n t / ) . 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 ( / ^ T i t l e / i)
1682+ . clear ( )
1683+ . type ( 'New Title' )
1684+
1685+ cy . findAllByText ( / S a v e C h a n g e s / i)
1686+ . first ( )
1687+ . click ( )
1688+
1689+ cy . findByText ( 'Validation Error' ) . should ( 'exist' )
1690+ cy . findByText ( / P o i n t o f C o n t a c t E - m a i l t e s t @ t e s t .c i s n o t a v a l i d e m a i l a d d r e s s ./ ) . 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 ( / ^ T i t l e / i)
1710+ . clear ( )
1711+ . type ( 'New Title' )
1712+
1713+ cy . findAllByText ( / S a v e C h a n g e s / i)
1714+ . first ( )
1715+ . click ( )
1716+
1717+ cy . findByText ( 'Validation Error' ) . should ( 'exist' )
1718+ cy . findByText (
1719+ / R e q u i r e d f i e l d s w e r e m i s s e d o r t h e r e w a s a v a l i d a t i o n e r r o r . P l e a s e s c r o l l d o w n t o s e e d e t a i l s ./
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 ( / ^ T i t l e / 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 ( / ^ T i t l e / i) . type ( 'Some title' )
1806+
1807+ cy . findByText ( / S a v e D a t a s e t / 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 ( / ^ N a m e / ) . should ( 'exist' ) . should ( 'have.attr' , 'aria-required' , 'true' )
1845+
1846+ cy . findByLabelText ( 'Affiliation' , { exact : true } ) . clear ( )
1847+
1848+ cy . findByLabelText ( / ^ N a m e / ) . 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 ( / ^ N a m e / ) . should ( 'exist' ) . should ( 'have.attr' , 'aria-required' , 'true' )
1888+
1889+ cy . findByLabelText ( 'Affiliation' , { exact : true } ) . clear ( )
1890+
1891+ cy . findByLabelText ( / ^ N a m e / )
1892+ . should ( 'exist' )
1893+ . should ( 'have.attr' , 'aria-required' , 'false' )
1894+ } )
1895+ } )
1896+ } )
1897+ } )
16291898} )
0 commit comments