From 1af9f2f28b2ca62936da8a8acca81fc87e2192f7 Mon Sep 17 00:00:00 2001 From: Kevin Fleischman Date: Wed, 25 Aug 2021 13:05:32 -0400 Subject: [PATCH] fix: stop ignoring current subject when inside a .within --- cypress/integration/find.spec.js | 14 ++++++++++++++ src/utils.js | 4 +++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/cypress/integration/find.spec.js b/cypress/integration/find.spec.js index 44a5577..1516213 100644 --- a/cypress/integration/find.spec.js +++ b/cypress/integration/find.spec.js @@ -119,6 +119,20 @@ describe('find* dom-testing-library commands', () => { }) }) + it('findByText inside within and with a previous subject', () => { + cy.get('section:nth-of-type(2)').within(() => { + cy.findByText(/Button Text 1/).should('exist') + }) + cy.get('#nested') + .findByText(/Button Text/) + .should('exist') + cy.get('section:nth-of-type(2)').within(() => { + cy.get('#nested') + .findByText(/Button Text/) + .should('exist') + }) + }) + it('findByText works when another page loads', () => { cy.findByText('Next Page').click() cy.findByText('New Page Loaded').should('exist') diff --git a/src/utils.js b/src/utils.js index e55749b..649e11d 100644 --- a/src/utils.js +++ b/src/utils.js @@ -6,8 +6,10 @@ function getFirstElement(jqueryOrElement) { } function getContainer(container) { + const subject = cy.state('subject') const withinContainer = cy.state('withinSubject') - if (withinContainer) { + + if (!subject && withinContainer) { return getFirstElement(withinContainer) } return getFirstElement(container)