Skip to content

Commit

Permalink
fix: Subject does not exist assertion (#46)
Browse files Browse the repository at this point in the history
* add exist spec

* fix: handle element not exist assertion
  • Loading branch information
bahmutov committed Oct 18, 2022
1 parent 66bd158 commit 939ab7f
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 40 deletions.
40 changes: 40 additions & 0 deletions cypress/e2e/exists.cy.js
@@ -0,0 +1,40 @@
/// <reference types="cypress" />
// @ts-check

import '../../src'

beforeEach(() => {
cy.visit('cypress/index.html')
})

it('checks an element that exists', () => {
cy.get('#fruits')
.if('exist')
.then(cy.spy().as('if'))
.else()
.then(cy.spy().as('else'))
cy.get('@if').should('have.been.calledOnce')
cy.get('@else').should('not.have.been.called')
})

it('checks an element that does not exists', () => {
cy.get('#not-found')
.if('exist')
.then(cy.spy().as('if'))
.else()
.then(cy.spy().as('else'))
cy.get('@else').should('have.been.calledOnce')
cy.get('@if').should('not.have.been.called')
})

// https://github.com/bahmutov/cypress-if/issues/45
it('checks an element that does not exists using not.exist', () => {
cy.get('#not-found').should('not.exist')
cy.get('#not-found')
.if('not.exist')
.then(cy.spy().as('if'))
.else()
.then(cy.spy().as('else'))
cy.get('@if').should('have.been.calledOnce')
cy.get('@else').should('not.have.been.called')
})
86 changes: 46 additions & 40 deletions src/index.js
Expand Up @@ -58,6 +58,48 @@ Cypress.Commands.add(
let hasSubject = Boolean(subject)
let assertionsPassed = true

const evaluateAssertion = () => {
try {
if (Cypress._.isFunction(assertion)) {
const result = assertion(subject)
if (Cypress._.isBoolean(result)) {
// function was a predicate
if (!result) {
throw new Error('Predicate function failed')
}
}
} else if (
assertion.startsWith('not') ||
assertion.startsWith('have')
) {
const parts = assertion.split('.')
let assertionReduced = expect(subject).to
parts.forEach((assertionPart, k) => {
if (
k === parts.length - 1 &&
typeof assertionValue !== 'undefined'
) {
assertionReduced = assertionReduced[assertionPart](assertionValue)
} else {
assertionReduced = assertionReduced[assertionPart]
}
})
} else {
if (typeof assertionValue !== 'undefined') {
expect(subject).to.be[assertion](assertionValue)
} else {
expect(subject).to.be[assertion]
}
}
} catch (e) {
console.error(e)
assertionsPassed = false
if (e.message.includes('Invalid Chai property')) {
throw e
}
}
}

// check if the previous command was cy.task
// and it has failed and it was expected
if (
Expand All @@ -78,46 +120,10 @@ Cypress.Commands.add(
assertionsPassed = false
}
} else if (hasSubject && assertion) {
try {
if (Cypress._.isFunction(assertion)) {
const result = assertion(subject)
if (Cypress._.isBoolean(result)) {
// function was a predicate
if (!result) {
throw new Error('Predicate function failed')
}
}
} else if (
assertion.startsWith('not') ||
assertion.startsWith('have')
) {
const parts = assertion.split('.')
let assertionReduced = expect(subject).to
parts.forEach((assertionPart, k) => {
if (
k === parts.length - 1 &&
typeof assertionValue !== 'undefined'
) {
assertionReduced =
assertionReduced[assertionPart](assertionValue)
} else {
assertionReduced = assertionReduced[assertionPart]
}
})
} else {
if (typeof assertionValue !== 'undefined') {
expect(subject).to.be[assertion](assertionValue)
} else {
expect(subject).to.be[assertion]
}
}
} catch (e) {
console.error(e)
assertionsPassed = false
if (e.message.includes('Invalid Chai property')) {
throw e
}
}
evaluateAssertion()
} else if (subject === undefined && assertion) {
evaluateAssertion()
hasSubject = true
}
}

Expand Down

0 comments on commit 939ab7f

Please sign in to comment.