Skip to content

Commit

Permalink
fix: stop ignoring current subject when inside a .within
Browse files Browse the repository at this point in the history
  • Loading branch information
istateside authored and kentcdodds committed Sep 2, 2021
1 parent 53e76f6 commit 1af9f2f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
14 changes: 14 additions & 0 deletions cypress/integration/find.spec.js
Expand Up @@ -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')
Expand Down
4 changes: 3 additions & 1 deletion src/utils.js
Expand Up @@ -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)
Expand Down

0 comments on commit 1af9f2f

Please sign in to comment.