From 08e5b2ec894d6c88fb30d60f787d552f7e7f5726 Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Mon, 29 Aug 2022 09:57:44 -0400 Subject: [PATCH] feat: test cy.contains support (#5) --- README.md | 6 +++ cypress/e2e/close-dialog.cy.js | 72 +++++++++++++++++++++++++++++++++- cypress/e2e/spec.cy.js | 14 +++++++ 3 files changed, 90 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c1a16d8..17e535c 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,8 @@ **DO NOT USE YET** +Tested with `cy.get`, `cy.contains` commands + ## Install Add this package as a dev dependency @@ -52,6 +54,10 @@ If you re-run the tests, you should see the messages appear in the console ![Debug messages in the console](./img/debug.png) +## See also + +- [cypress-ngx-ui-testing](https://github.com/swimlane/ngx-ui/tree/master/projects/swimlane/ngx-ui-testing) + ## Small print Author: Gleb Bahmutov <gleb.bahmutov@gmail.com> © 2022 diff --git a/cypress/e2e/close-dialog.cy.js b/cypress/e2e/close-dialog.cy.js index 10839d3..7ae70fe 100644 --- a/cypress/e2e/close-dialog.cy.js +++ b/cypress/e2e/close-dialog.cy.js @@ -2,10 +2,15 @@ import '../../src' it( - 'closes the survey dialog if it is open', + 'closes the survey dialog', { viewportWidth: 500, viewportHeight: 500 }, () => { - cy.visit('cypress/close-dialog.html') + cy.visit('cypress/close-dialog.html', { + onBeforeLoad(win) { + // trick the app to open the dialog + cy.stub(win.Math, 'random').returns(0) + }, + }) cy.get('dialog#survey') .if('visible') .wait(1000) @@ -16,3 +21,66 @@ it( cy.get('#main').should('be.visible') }, ) + +it( + 'skips the commands since the dialog is closed', + { viewportWidth: 500, viewportHeight: 500 }, + () => { + cy.visit('cypress/close-dialog.html', { + onBeforeLoad(win) { + // trick the app to close the dialog + cy.stub(win.Math, 'random').returns(1) + }, + }) + cy.get('dialog#survey') + .if('visible') + .wait(1000) + .contains('button', 'Close') + .click() + // if there is a dialog on top, + // then the main text is not visible + cy.get('#main').should('be.visible') + }, +) + +describe('cy.contains support', () => { + it( + 'clicks the close survey button', + { viewportWidth: 500, viewportHeight: 500 }, + () => { + cy.visit('cypress/close-dialog.html', { + onBeforeLoad(win) { + // trick the app to open the dialog + cy.stub(win.Math, 'random').returns(0) + }, + }) + cy.contains('dialog#survey button', 'Close') + .if('visible') + .wait(1000) + .click() + // if there is a dialog on top, + // then the main text is not visible + cy.get('#main').should('be.visible') + }, + ) + + it( + 'skips click when the button is hidden', + { viewportWidth: 500, viewportHeight: 500 }, + () => { + cy.visit('cypress/close-dialog.html', { + onBeforeLoad(win) { + // trick the app to hide the dialog + cy.stub(win.Math, 'random').returns(1) + }, + }) + cy.contains('dialog#survey button', 'Close') + .if('visible') + .wait(1000) + .click() + // if there is a dialog on top, + // then the main text is not visible + cy.get('#main').should('be.visible') + }, + ) +}) diff --git a/cypress/e2e/spec.cy.js b/cypress/e2e/spec.cy.js index 0829d93..802ce4b 100644 --- a/cypress/e2e/spec.cy.js +++ b/cypress/e2e/spec.cy.js @@ -21,3 +21,17 @@ it('clicks on the button if it is visible', () => { // but we can click on the visible button cy.get('button#load').if('visible').click() }) + +describe('cy.contains support', () => { + it('clicks on the button by text if exists', () => { + cy.visit('cypress/index.html') + cy.log('**button exists**') + cy.contains('button', 'Load').if().click() + cy.log('**attached assertions are passing**') + cy.contains('button', 'Load').if().should('be.visible') + cy.log('**button does not exist**') + cy.contains('button', 'does-not-exist').if().click() + cy.log('**attached assertions are skipped**') + cy.contains('button', 'does-not-exist').if().should('not.exist') + }) +})