Skip to content

Commit

Permalink
feat: cy.else can take a message to log (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov authored Jan 19, 2024
1 parent c734ee4 commit 47befa9
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 3 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@ cy.get('#enrolled')
.check()
```

You can print a message if the `ELSE` branch is taken

```js
cy.get('...').if('...').else().log('a message')
// same as
cy.get('...').if('...').else('a message')
```

## Multiple commands

Sometimes it makes sense to place the "if" or "else" commands into `.then()` block
Expand Down
6 changes: 6 additions & 0 deletions cypress/e2e/else.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ import '../../src'
describe('else branch', () => {
it('takes the if branch', () => {
cy.wrap(42).if('equal', 42).log('if branch').else().log('else branch')
cy.log('**built-in log**')
cy.wrap(42).if('equal', 42).log('if branch').else('else branch')
})

it('takes the else branch', () => {
cy.wrap(42).if('equal', 1).log('if branch').else().log('else branch')
cy.log('**built-in log**')
cy.wrap(42).if('equal', 1).log('if branch').else('else branch')
cy.log('**prints numbers**')
cy.wrap(42).if('equal', 1).log('if branch').else(42)
})

it('can have multiple if-else', () => {
Expand Down
5 changes: 4 additions & 1 deletion src/index-v11.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,15 @@ Cypress.Commands.add(
},
)

Cypress.Commands.add('else', { prevSubject: true }, (subject) => {
Cypress.Commands.add('else', { prevSubject: true }, (subject, text) => {
debug('else command, subject', subject)
if (typeof subject === 'undefined') {
// find the subject from the "if()" before
subject = findMyIfSubject(cy.state('current').attributes)
}
if (typeof text !== undefined) {
cy.log(text)
}
if (subject) {
cy.wrap(subject, { log: false })
}
Expand Down
9 changes: 8 additions & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,19 @@ declare namespace Cypress {
* execute if the previous `.if()` command skipped
* the "IF" branch. Note: `.if()` passes its subject
* to the `.else()`
* You can also print a message if the ELSE branch
* is taken
* @param message Message to print to the console. Optional.
* @example
* cy.get('checkox#agree')
* .if('checked').log('Already agreed')
* .else().check()
* @example
* cy.get('...')
* .if('not.visible').log('Not visible')
* .else('visible')
*/
else(): Chainable<any>
else(message?: any): Chainable<any>

/**
* Finishes if/else commands and continues
Expand Down
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,15 @@ if (major < 12) {
},
)

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

0 comments on commit 47befa9

Please sign in to comment.