diff --git a/cypress/e2e/input-value.cy.js b/cypress/e2e/input-value.cy.js new file mode 100644 index 0000000..3fbda68 --- /dev/null +++ b/cypress/e2e/input-value.cy.js @@ -0,0 +1,71 @@ +/// +// @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.$('')) + .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.$('')) + .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.$('')) + .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.$('')) + .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.$('')) + .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') + }) + }) +}) diff --git a/cypress/funky-input.html b/cypress/funky-input.html new file mode 100644 index 0000000..198f7ed --- /dev/null +++ b/cypress/funky-input.html @@ -0,0 +1,10 @@ + + +

Your name?

+ + diff --git a/src/index.js b/src/index.js index 7321d04..3976ed5 100644 --- a/src/index.js +++ b/src/index.js @@ -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') {