Skip to content

Commit

Permalink
fix: support not.checked assertion, closes #8
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Aug 31, 2022
1 parent 54f8156 commit 39d2479
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1 +1,2 @@
node_modules/
cypress/videos/
14 changes: 14 additions & 0 deletions cypress/checkbox.html
@@ -0,0 +1,14 @@
<html>
<head></head>
<body>
<div>
<input type="checkbox" id="enrolled" />
<span>Enrolled already</span>
</div>

<div>
<input type="checkbox" id="agreed" checked />
<span>Agree</span>
</div>
</body>
</html>
15 changes: 15 additions & 0 deletions cypress/e2e/checked.cy.js
@@ -0,0 +1,15 @@
/// <reference types="cypress" />

import '../../src'

describe('checkbox', () => {
it('checks the box when it is not checked already', () => {
cy.visit('cypress/checkbox.html')
cy.get('#enrolled').if('not.checked').check()
})

it('does nothing if the box is already checked', () => {
cy.visit('cypress/checkbox.html')
cy.get('#agreed').if('not.checked').check()
})
})
12 changes: 10 additions & 2 deletions src/index.js
Expand Up @@ -5,14 +5,22 @@ Cypress.Commands.add(
{ prevSubject: true },
function (subject, assertion) {
const cmd = cy.state('current')
debug('if', cmd.attributes, 'subject', subject)
debug('if', cmd.attributes, 'subject', subject, 'assertion?', assertion)
debug('next command', cmd.next)

const hasSubject = Boolean(subject)
let assertionsPassed = true
if (hasSubject && assertion) {
try {
expect(subject).to.be[assertion]
if (assertion.startsWith('not')) {
const parts = assertion.split('.')
let assertionReduced = expect(subject).to
parts.forEach((assertionPart) => {
assertionReduced = assertionReduced[assertionPart]
})
} else {
expect(subject).to.be[assertion]
}
} catch (e) {
console.error(e)
assertionsPassed = false
Expand Down

0 comments on commit 39d2479

Please sign in to comment.