Skip to content

Commit

Permalink
Fix same error appearing multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
hjvoid committed Sep 17, 2024
1 parent 5c9609c commit 3073a91
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
8 changes: 7 additions & 1 deletion app/assets/javascripts/browsered/form-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ var init = function () {
var errorAnchor = document.createElement('a')
errorAnchor.setAttribute('href', '#' + error.cssKey)
errorAnchor.id = error.cssKey + '-error'
if(!document.getElementById(errorAnchor.id)){
if (!document.getElementById(errorAnchor.id)) {
errorAnchor.innerText = error.value
listElement.appendChild(errorAnchor)
if (addType === 'append') {
Expand Down Expand Up @@ -192,8 +192,14 @@ var init = function () {
if (isAnExpiryDateField) {
expiryMonthElement.classList.remove(CSS_ERROR_CLASS)
expiryYearElement.classList.remove(CSS_ERROR_CLASS)
if (document.getElementById('expiry-month-error')) {
document.getElementById('expiry-month-error').remove()
}
} else {
input.classList.remove(CSS_ERROR_CLASS)
if (document.getElementById(`${input.id}-error`)) {
document.getElementById(`${input.id}-error`).remove()
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,23 @@ describe('Card details page validation', () => {
cy.get('[data-cy=expiry-month]').not('have.class', 'govuk-input--error')
cy.get('[data-cy=expiry-year]').not('have.class', 'govuk-input--error')
})

it('Should remove summary field errors after clicking out of a correctly validated field', () => {
cy.task('setupStubs', createPaymentChargeStubs)
cy.visit(`/secure/${tokenId}`)

cy.location('pathname').should('eq', `/card_details/${chargeId}`)
cy.window().its('chargeId').should('eq', `${chargeId}`)

// {backspace} Deletes character to the left of the cursor. Used as an empty string causes a Cypress error
cy.get('[data-cy=cardholder-name]').type('{backspace}')
cy.get('#card-details').submit()

cy.get('#cardholder-name-error').should('exist')

cy.get('[data-cy=cardholder-name]').clear().type('Jeff')
cy.get('[data-cy=expiry-month]').click()

cy.get('#cardholder-name-error').should('not.exist')
})
})

0 comments on commit 3073a91

Please sign in to comment.