Skip to content

Commit

Permalink
fix: some test error
Browse files Browse the repository at this point in the history
  • Loading branch information
ChengShi-1 committed Feb 25, 2025
1 parent 7ce1129 commit 935d3ed
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 52 deletions.
6 changes: 2 additions & 4 deletions src/sections/shared/contact/contact-modal/contact-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ export const ContactModal = ({
}
})

const { reset } = methods

const onSubmit = async (data: ContactFormData) => {
const formData: FeedbackDTO = {
subject: data.subject,
Expand All @@ -64,13 +62,13 @@ export const ContactModal = ({
}

await submitForm(formData)
toast.success(t('contact.contactSuccess'))
reset()
methods.reset()
}

useEffect(() => {
if (submissionStatus === SubmissionStatus.SubmitComplete) {
handleClose()
toast.success(t('contact.contactSuccess'))
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [submissionStatus])
Expand Down
2 changes: 1 addition & 1 deletion tests/component/sections/collection/Collection.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ describe('Collection page', () => {
cy.findByRole('tooltip').should('be.visible').and('have.text', 'Email Collection Contact')
})

it('shows the alert when the information was sent to contact successfully', () => {
it('shows the toast when the information was sent to contact successfully', () => {
cy.customMount(
<Collection
collectionRepository={collectionRepository}
Expand Down
30 changes: 23 additions & 7 deletions tests/component/sections/contact/ContactButton.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('ContactButton', () => {
.click()
})

it('shows correct contact title based on different contact object', () => {
it('shows contact button and correct title if it is in collection page ', () => {
cy.customMount(
<ContactButton
toContactName="Test Dataset"
Expand All @@ -50,7 +50,7 @@ describe('ContactButton', () => {
cy.findByText(/Email Dataset Contact/i).should('exist')
})

it('shows contact owner button if it is in dataset page ', () => {
it('shows contact owner button and correct title if it is in dataset page ', () => {
cy.customMount(
<ContactButton
toContactName="Test Dataset"
Expand Down Expand Up @@ -80,15 +80,30 @@ describe('ContactButton', () => {
})
cy.findByRole('dialog').should('not.exist')
})
})

describe('ContactButton Error', () => {
beforeEach(() => {
it('should submit form with numeric id succefully ', () => {
cy.findByTestId('captchaNumbers')
.invoke('text')
.then((text) => {
const matches = text.match(/(\d+)\s*\+\s*(\d+)\s*=/)
if (matches) {
const num1 = parseInt(matches[1], 10)
const num2 = parseInt(matches[2], 10)
const answer = num1 + num2
cy.findByTestId('fromEmail').type('email@dataverse.com')
cy.findByTestId('subject').type('subject')
cy.findByTestId('body').type('message')
cy.findByTestId('captchaInput').type(answer.toString())
cy.findByRole('button', { name: /Submit/i }).click()
}
})
})

it('should send alert if the submission is failed', () => {
contactRepository.sendFeedbacktoOwners = cy
.stub()
.rejects(new Error('Failed to submit contact info'))
})
it('should send alert if the submission is failed', () => {

cy.customMount(
<ContactButton
toContactName="Test Dataset"
Expand All @@ -101,6 +116,7 @@ describe('ContactButton Error', () => {
cy.findByRole('button', { name: /Contact/i })
.should('exist')
.click()

cy.findByTestId('captchaNumbers')
.invoke('text')
.then((text) => {
Expand Down
2 changes: 1 addition & 1 deletion tests/component/sections/dataset/Dataset.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ describe('Dataset', () => {
cy.findByText(/The metadata for this dataset has been updated./).should('exist')
})

it('shows the alert when the information was sent to contact successfully', () => {
it('shows the toast when the information was sent to contact successfully', () => {
const testDataset = DatasetMother.create()
mountWithDataset(
<Dataset
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,42 +82,3 @@ describe('Contact Modal', () => {
})
})
})

it('should submit form and transform numeric id correctly ', () => {
contactRepository.sendFeedbacktoOwners = cy.stub().resolves([mockContacts])
const title = 'Email Collection Contact'
const toContactName = 'Root'

cy.customMount(
<ContactModal
show
title={title}
handleClose={() => {}}
toContactName={toContactName}
id={12}
contactRepository={contactRepository}
/>
)

cy.findByTestId('captchaNumbers')
.invoke('text')
.then((text) => {
const matches = text.match(/(\d+)\s*\+\s*(\d+)\s*=/)
if (matches) {
const num1 = parseInt(matches[1], 10)
const num2 = parseInt(matches[2], 10)
const answer = num1 + num2
cy.findByTestId('fromEmail').type('email@dataverse.com')
cy.findByTestId('subject').type('Test Subject')
cy.findByTestId('body').type('This is a test message.')
cy.findByTestId('captchaInput').type(answer.toString())
cy.findByRole('button', { name: /Submit/i }).click()
}
cy.wrap(contactRepository.sendFeedbacktoOwners).should('have.been.calledWith', {
subject: 'Test Subject',
body: 'This is a test message.',
fromEmail: 'email@dataverse.com',
targetId: 12
})
})
})

0 comments on commit 935d3ed

Please sign in to comment.