fix(bug): indeterminate checkbox state doesn't get changed (#21665)

This commit is contained in:
kshastri
2022-06-01 21:51:39 +05:30
committed by GitHub
parent 02148acf33
commit 4b4e992710
2 changed files with 37 additions and 0 deletions

View File

@@ -71,6 +71,21 @@ describe('src/cy/commands/actions/check', () => {
})
})
// https://github.com/cypress-io/cypress/issues/19098
it('removes indeterminate prop when checkbox is checked', () => {
const intermediateCheckbox = $(`<input type='checkbox' name="indeterminate">`)
intermediateCheckbox.prop('indeterminate', true)
$('body').append(intermediateCheckbox)
cy.get(':checkbox[name=\'indeterminate\']').check().then(($checkbox) => {
const hasIndeterminate = $checkbox.prop('indeterminate')
expect(hasIndeterminate).to.be.false
})
})
it('is a noop if already checked', () => {
const checkbox = ':checkbox[name=\'colors\'][value=\'blue\']'
@@ -839,6 +854,21 @@ describe('src/cy/commands/actions/check', () => {
})
})
// https://github.com/cypress-io/cypress/issues/19098
it('removes indeterminate prop when checkbox is unchecked', () => {
const indeterminateCheckbox = $(`<input type='checkbox' name="indeterminate">`)
indeterminateCheckbox.prop('indeterminate', true)
$('body').append(indeterminateCheckbox)
cy.get(':checkbox[name=\'indeterminate\']').uncheck().then(($checkbox) => {
const hasIndeterminate = $checkbox.prop('indeterminate')
expect(hasIndeterminate).to.be.false
})
})
it('unchecks a checkbox', () => {
cy.get('[name=birds][value=cockatoo]').uncheck().then(($checkbox) => {
expect($checkbox).not.to.be.checked

View File

@@ -113,6 +113,13 @@ const checkOrUncheck = (Cypress, cy, type, subject, values: any[] = [], userOpti
cy.ensureVisibility($el, options._log)
}
// if the checkbox is in an indeterminate state, checking or unchecking should set the
// prop to false to move it into a "determinate" state
// https://github.com/cypress-io/cypress/issues/19098
if ($el.prop('indeterminate')) {
$el.prop('indeterminate', false)
}
if (options._log) {
const inputType = $el.is(':radio') ? 'radio' : 'checkbox'