Skip to content

Commit

Permalink
fix: have.value assertions, closes #23
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Sep 6, 2022
1 parent 1cb629f commit f990a27
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 3 deletions.
71 changes: 71 additions & 0 deletions cypress/e2e/input-value.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/// <reference types="cypress" />
// @ts-check

import '../../src'

describe('Input element', () => {
it('types again if the value was corrupted', () => {
cy.visit('cypress/funky-input.html')
cy.get('#name')
.type('Cypress', { delay: 20 })
.if('not.have.value', 'Cypress')
.clear()
.type('Cypress')
.else()
.log('Input has expected value')
.finally()
.should('have.value', 'Cypress')
})

it('have.value positive', () => {
cy.wrap(Cypress.$('<input value="foo" >'))
.if('have.value', 'foo')
.then(cy.spy().as('if'))
.else()
.then(cy.spy().as('else'))
cy.get('@if').should('be.calledOnce')
cy.get('@else').should('not.be.called')
})

it('have.value negative', () => {
cy.wrap(Cypress.$('<input value="foo" >'))
.if('have.value', 'bar')
.then(cy.spy().as('if'))
.else()
.then(cy.spy().as('else'))
cy.get('@else').should('be.calledOnce')
cy.get('@if').should('not.be.called')
})

it('have.value positive', () => {
cy.wrap(Cypress.$('<input value="foo" >'))
.if('have.value', 'foo')
.then(cy.spy().as('if'))
.else()
.then(cy.spy().as('else'))
cy.get('@if').should('be.calledOnce')
cy.get('@else').should('not.be.called')
})

context('not.have.value', () => {
it('positive', () => {
cy.wrap(Cypress.$('<input value="foo" >'))
.if('not.have.value', 'foo')
.then(cy.spy().as('if'))
.else()
.then(cy.spy().as('else'))
cy.get('@else').should('be.calledOnce')
cy.get('@if').should('not.be.called')
})

it('negative', () => {
cy.wrap(Cypress.$('<input value="foo" >'))
.if('not.have.value', 'bar')
.then(cy.spy().as('if'))
.else()
.then(cy.spy().as('else'))
cy.get('@if').should('be.calledOnce')
cy.get('@else').should('not.be.called')
})
})
})
10 changes: 10 additions & 0 deletions cypress/funky-input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<body>
<input id="name" />
<p>Your name?</p>
<script>
setTimeout(() => {
// clear the input box suddenly
document.getElementById('name').value = ''
}, Math.random() * 30 + 70)
</script>
</body>
13 changes: 10 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,18 @@ Cypress.Commands.add(
let assertionsPassed = true
if (hasSubject && assertion) {
try {
if (assertion.startsWith('not')) {
if (assertion.startsWith('not') || assertion.startsWith('have')) {
const parts = assertion.split('.')
let assertionReduced = expect(subject).to
parts.forEach((assertionPart) => {
assertionReduced = assertionReduced[assertionPart]
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') {
Expand Down

0 comments on commit f990a27

Please sign in to comment.