diff --git a/README.md b/README.md index 0901e12..2b87679 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/cypress/e2e/checked.cy.js b/cypress/e2e/checked.cy.js index 3437cdf..2a09d3d 100644 --- a/cypress/e2e/checked.cy.js +++ b/cypress/e2e/checked.cy.js @@ -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') diff --git a/cypress/e2e/wrap.cy.js b/cypress/e2e/wrap.cy.js index cf9f89c..9904451 100644 --- a/cypress/e2e/wrap.cy.js +++ b/cypress/e2e/wrap.cy.js @@ -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) + }) + }) }) diff --git a/src/index.js b/src/index.js index 368777a..ad20f33 100644 --- a/src/index.js +++ b/src/index.js @@ -14,6 +14,18 @@ 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 }, @@ -21,6 +33,11 @@ Cypress.Commands.add( 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 @@ -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 }) }