From 39d2479d85e37d48dcd64de5561cf9593fec5513 Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Tue, 30 Aug 2022 20:57:26 -0400 Subject: [PATCH] fix: support not.checked assertion, closes #8 --- .gitignore | 1 + cypress/checkbox.html | 14 ++++++++++++++ cypress/e2e/checked.cy.js | 15 +++++++++++++++ src/index.js | 12 ++++++++++-- 4 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 cypress/checkbox.html create mode 100644 cypress/e2e/checked.cy.js diff --git a/.gitignore b/.gitignore index c2658d7..db03ec7 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules/ +cypress/videos/ diff --git a/cypress/checkbox.html b/cypress/checkbox.html new file mode 100644 index 0000000..337d93d --- /dev/null +++ b/cypress/checkbox.html @@ -0,0 +1,14 @@ + + + +
+ + Enrolled already +
+ +
+ + Agree +
+ + diff --git a/cypress/e2e/checked.cy.js b/cypress/e2e/checked.cy.js new file mode 100644 index 0000000..8a7d5c7 --- /dev/null +++ b/cypress/e2e/checked.cy.js @@ -0,0 +1,15 @@ +/// + +import '../../src' + +describe('checkbox', () => { + it('checks the box when it is not checked already', () => { + cy.visit('cypress/checkbox.html') + cy.get('#enrolled').if('not.checked').check() + }) + + it('does nothing if the box is already checked', () => { + cy.visit('cypress/checkbox.html') + cy.get('#agreed').if('not.checked').check() + }) +}) diff --git a/src/index.js b/src/index.js index 798b7eb..4d9f99a 100644 --- a/src/index.js +++ b/src/index.js @@ -5,14 +5,22 @@ Cypress.Commands.add( { prevSubject: true }, function (subject, assertion) { const cmd = cy.state('current') - debug('if', cmd.attributes, 'subject', subject) + debug('if', cmd.attributes, 'subject', subject, 'assertion?', assertion) debug('next command', cmd.next) const hasSubject = Boolean(subject) let assertionsPassed = true if (hasSubject && assertion) { try { - expect(subject).to.be[assertion] + if (assertion.startsWith('not')) { + const parts = assertion.split('.') + let assertionReduced = expect(subject).to + parts.forEach((assertionPart) => { + assertionReduced = assertionReduced[assertionPart] + }) + } else { + expect(subject).to.be[assertion] + } } catch (e) { console.error(e) assertionsPassed = false