Skip to content

Commit 1af9f2f

Browse files
istatesidekentcdodds
authored andcommitted
fix: stop ignoring current subject when inside a .within
1 parent 53e76f6 commit 1af9f2f

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

cypress/integration/find.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,20 @@ describe('find* dom-testing-library commands', () => {
119119
})
120120
})
121121

122+
it('findByText inside within and with a previous subject', () => {
123+
cy.get('section:nth-of-type(2)').within(() => {
124+
cy.findByText(/Button Text 1/).should('exist')
125+
})
126+
cy.get('#nested')
127+
.findByText(/Button Text/)
128+
.should('exist')
129+
cy.get('section:nth-of-type(2)').within(() => {
130+
cy.get('#nested')
131+
.findByText(/Button Text/)
132+
.should('exist')
133+
})
134+
})
135+
122136
it('findByText works when another page loads', () => {
123137
cy.findByText('Next Page').click()
124138
cy.findByText('New Page Loaded').should('exist')

src/utils.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ function getFirstElement(jqueryOrElement) {
66
}
77

88
function getContainer(container) {
9+
const subject = cy.state('subject')
910
const withinContainer = cy.state('withinSubject')
10-
if (withinContainer) {
11+
12+
if (!subject && withinContainer) {
1113
return getFirstElement(withinContainer)
1214
}
1315
return getFirstElement(container)

0 commit comments

Comments
 (0)