Skip to content

Commit b4f1d45

Browse files
authored
fix: Pass container from findBy through to waitFor (#868)
1 parent 71b4f46 commit b4f1d45

File tree

3 files changed

+90
-4
lines changed

3 files changed

+90
-4
lines changed

src/__node_tests__/index.js

+75
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,78 @@ test('byRole works without a global DOM', () => {
9696
</button>
9797
`)
9898
})
99+
100+
test('findBy works without a global DOM', async () => {
101+
const {window} = new JSDOM(`<div>
102+
<div data-testid="test-id" aria-label="test-label">test text content</div>
103+
<select><option>display value</option></select>
104+
<input placeholder="placeholder" />
105+
<img alt="test alt text" src="/lucy-ricardo.png" />
106+
<span title="test title" />
107+
<div role="dialog"></div>
108+
<div role="meter progressbar"></div>
109+
<header>header</header>
110+
<input type="hidden" />
111+
</div>`)
112+
113+
await expect(
114+
dtl.findByLabelText(window.document, 'test-label'),
115+
).resolves.toBeTruthy()
116+
await expect(
117+
dtl.findAllByLabelText(window.document, 'test-label'),
118+
).resolves.toHaveLength(1)
119+
await expect(
120+
dtl.findByPlaceholderText(window.document, 'placeholder'),
121+
).resolves.toBeTruthy()
122+
await expect(
123+
dtl.findAllByPlaceholderText(window.document, 'placeholder'),
124+
).resolves.toHaveLength(1)
125+
await expect(
126+
dtl.findByText(window.document, 'test text content'),
127+
).resolves.toBeTruthy()
128+
await expect(
129+
dtl.findAllByText(window.document, 'test text content'),
130+
).resolves.toHaveLength(1)
131+
await expect(
132+
dtl.findByAltText(window.document, 'test alt text'),
133+
).resolves.toBeTruthy()
134+
await expect(
135+
dtl.findAllByAltText(window.document, 'test alt text'),
136+
).resolves.toHaveLength(1)
137+
await expect(
138+
dtl.findByTitle(window.document, 'test title'),
139+
).resolves.toBeTruthy()
140+
await expect(
141+
dtl.findAllByTitle(window.document, 'test title'),
142+
).resolves.toHaveLength(1)
143+
await expect(
144+
dtl.findByDisplayValue(window.document, 'display value'),
145+
).resolves.toBeTruthy()
146+
await expect(
147+
dtl.findAllByDisplayValue(window.document, 'display value'),
148+
).resolves.toHaveLength(1)
149+
await expect(dtl.findByRole(window.document, 'dialog')).resolves.toBeTruthy()
150+
await expect(
151+
dtl.findAllByRole(window.document, 'dialog'),
152+
).resolves.toHaveLength(1)
153+
await expect(dtl.findByRole(window.document, 'meter')).resolves.toBeTruthy()
154+
await expect(
155+
dtl.findAllByRole(window.document, 'meter'),
156+
).resolves.toHaveLength(1)
157+
await expect(
158+
dtl.findByRole(window.document, 'progressbar', {queryFallbacks: true}),
159+
).resolves.toBeTruthy()
160+
await expect(
161+
dtl.findAllByRole(window.document, 'progressbar', {queryFallbacks: true}),
162+
).resolves.toHaveLength(1)
163+
await expect(dtl.findByRole(window.document, 'banner')).resolves.toBeTruthy()
164+
await expect(
165+
dtl.findAllByRole(window.document, 'banner'),
166+
).resolves.toHaveLength(1)
167+
await expect(
168+
dtl.findByTestId(window.document, 'test-id'),
169+
).resolves.toBeTruthy()
170+
await expect(
171+
dtl.findAllByTestId(window.document, 'test-id'),
172+
).resolves.toHaveLength(1)
173+
})

src/query-helpers.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,14 @@ function makeGetAllQuery(allQuery, getMissingError) {
9090
// this accepts a getter query function and returns a function which calls
9191
// waitFor and passing a function which invokes the getter.
9292
function makeFindQuery(getter) {
93-
return (container, text, options, waitForOptions) =>
94-
waitFor(() => {
95-
return getter(container, text, options)
96-
}, waitForOptions)
93+
return (container, text, options, waitForOptions) => {
94+
return waitFor(
95+
() => {
96+
return getter(container, text, options)
97+
},
98+
{container, ...waitForOptions},
99+
)
100+
}
97101
}
98102

99103
const wrapSingleQueryWithSuggestion = (query, queryAllByName, variant) => (

src/wait-for.js

+7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
setImmediate,
99
setTimeout,
1010
clearTimeout,
11+
checkContainerType,
1112
} from './helpers'
1213
import {getConfig, runWithExpensiveErrorDiagnosticsDisabled} from './config'
1314

@@ -89,6 +90,12 @@ function waitFor(
8990
await new Promise(r => setImmediate(r))
9091
}
9192
} else {
93+
try {
94+
checkContainerType(container)
95+
} catch (e) {
96+
reject(e)
97+
return
98+
}
9299
intervalId = setInterval(checkRealTimersCallback, interval)
93100
const {MutationObserver} = getWindowFromNode(container)
94101
observer = new MutationObserver(checkRealTimersCallback)

0 commit comments

Comments
 (0)