Skip to content

Commit

Permalink
feat: pass the subject to the else branch (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Aug 31, 2022
1 parent 0d84a27 commit 63f2700
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
11 changes: 11 additions & 0 deletions README.md
Expand Up @@ -66,6 +66,17 @@ cy.contains('Accept cookies')
.log('no cookie banner')
```

The subject from the `.if()` command will be passed to the `.else()` chain, this allows you to work with the original element:

```js
cy.get('#enrolled')
.if('checked')
.log('**already enrolled**')
// the checkbox should be passed into .else()
.else()
.check()
```

## Debugging

This module uses [debug](https://github.com/debug-js/debug#readme) module to output verbose browser console messages when needed. To turn the logging on, open the browser's DevTools console and set the local storage entry:
Expand Down
2 changes: 1 addition & 1 deletion cypress/e2e/checked.cy.js
Expand Up @@ -39,7 +39,7 @@ describe('checkbox', () => {
cy.get('#enrolled').should('be.checked')
})

it.skip('passes the subject to the else() branch', () => {
it('passes the subject to the else() branch', () => {
cy.visit('cypress/checkbox.html')
cy.get('#enrolled')
.if('checked')
Expand Down
10 changes: 10 additions & 0 deletions cypress/e2e/wrap.cy.js
Expand Up @@ -12,4 +12,14 @@ describe('wrapped value', () => {
cy.wrap(1).if('equal', 42).then(cy.spy().as('action')).then(cy.log)
cy.get('@action').should('not.have.been.called')
})

context('.else', () => {
it('passes the subject to the else branch', () => {
cy.wrap(1).if('equal', 42).log('if branch').else().should('equal', 1)
})

it('passes the subject if().else()', () => {
cy.wrap(1).if('equal', 42).else().should('equal', 1)
})
})
})
28 changes: 27 additions & 1 deletion src/index.js
Expand Up @@ -14,13 +14,30 @@ function skipRestOfTheChain(cmd) {
}
}

function findMyIfSubject(elseCommandAttributes) {
if (!elseCommandAttributes) {
return
}
if (elseCommandAttributes.name === 'if') {
return elseCommandAttributes.ifSubject
}
if (elseCommandAttributes.prev) {
return findMyIfSubject(elseCommandAttributes.prev.attributes)
}
}

Cypress.Commands.add(
'if',
{ prevSubject: true },
function (subject, assertion, assertionValue) {
const cmd = cy.state('current')
debug('if', cmd.attributes, 'subject', subject, 'assertion?', assertion)
debug('next command', cmd.next)
debug('if() current subject', cy.currentSubject())
// console.log('subjects', cy.state('subjects'))
// keep the subject, if there is an "else" branch
// it can look it up to use
cmd.attributes.ifSubject = subject

const hasSubject = Boolean(subject)
let assertionsPassed = true
Expand Down Expand Up @@ -116,7 +133,16 @@ Cypress.Commands.add(
)

Cypress.Commands.add('else', { prevSubject: true }, (subject) => {
debug('else command, subject', subject)
// debug('else command, subject', subject)
// debug('current subject', cy.currentSubject())
// debug('current command attributes', cy.state('current').attributes)
// console.log('subjects', cy.state('subjects'))
// debugger
debugger
if (typeof subject === 'undefined') {
// find the subject from the "if()" before
subject = findMyIfSubject(cy.state('current').attributes)
}
if (subject) {
cy.wrap(subject, { log: false })
}
Expand Down

0 comments on commit 63f2700

Please sign in to comment.