Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: pass the subject to the else branch #11

Merged
merged 1 commit into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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