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)