Skip to content

fix: Pass container from findBy through to waitFor #868

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions src/__node_tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,78 @@ test('byRole works without a global DOM', () => {
</button>
`)
})

test('findBy works without a global DOM', async () => {
const {window} = new JSDOM(`<div>
<div data-testid="test-id" aria-label="test-label">test text content</div>
<select><option>display value</option></select>
<input placeholder="placeholder" />
<img alt="test alt text" src="/lucy-ricardo.png" />
<span title="test title" />
<div role="dialog"></div>
<div role="meter progressbar"></div>
<header>header</header>
<input type="hidden" />
</div>`)

await expect(
dtl.findByLabelText(window.document, 'test-label'),
).resolves.toBeTruthy()
await expect(
dtl.findAllByLabelText(window.document, 'test-label'),
).resolves.toHaveLength(1)
await expect(
dtl.findByPlaceholderText(window.document, 'placeholder'),
).resolves.toBeTruthy()
await expect(
dtl.findAllByPlaceholderText(window.document, 'placeholder'),
).resolves.toHaveLength(1)
await expect(
dtl.findByText(window.document, 'test text content'),
).resolves.toBeTruthy()
await expect(
dtl.findAllByText(window.document, 'test text content'),
).resolves.toHaveLength(1)
await expect(
dtl.findByAltText(window.document, 'test alt text'),
).resolves.toBeTruthy()
await expect(
dtl.findAllByAltText(window.document, 'test alt text'),
).resolves.toHaveLength(1)
await expect(
dtl.findByTitle(window.document, 'test title'),
).resolves.toBeTruthy()
await expect(
dtl.findAllByTitle(window.document, 'test title'),
).resolves.toHaveLength(1)
await expect(
dtl.findByDisplayValue(window.document, 'display value'),
).resolves.toBeTruthy()
await expect(
dtl.findAllByDisplayValue(window.document, 'display value'),
).resolves.toHaveLength(1)
await expect(dtl.findByRole(window.document, 'dialog')).resolves.toBeTruthy()
await expect(
dtl.findAllByRole(window.document, 'dialog'),
).resolves.toHaveLength(1)
await expect(dtl.findByRole(window.document, 'meter')).resolves.toBeTruthy()
await expect(
dtl.findAllByRole(window.document, 'meter'),
).resolves.toHaveLength(1)
await expect(
dtl.findByRole(window.document, 'progressbar', {queryFallbacks: true}),
).resolves.toBeTruthy()
await expect(
dtl.findAllByRole(window.document, 'progressbar', {queryFallbacks: true}),
).resolves.toHaveLength(1)
await expect(dtl.findByRole(window.document, 'banner')).resolves.toBeTruthy()
await expect(
dtl.findAllByRole(window.document, 'banner'),
).resolves.toHaveLength(1)
await expect(
dtl.findByTestId(window.document, 'test-id'),
).resolves.toBeTruthy()
await expect(
dtl.findAllByTestId(window.document, 'test-id'),
).resolves.toHaveLength(1)
})
12 changes: 8 additions & 4 deletions src/query-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,14 @@ function makeGetAllQuery(allQuery, getMissingError) {
// this accepts a getter query function and returns a function which calls
// waitFor and passing a function which invokes the getter.
function makeFindQuery(getter) {
return (container, text, options, waitForOptions) =>
waitFor(() => {
return getter(container, text, options)
}, waitForOptions)
return (container, text, options, waitForOptions) => {
return waitFor(
() => {
return getter(container, text, options)
},
{container, ...waitForOptions},
)
}
}

const wrapSingleQueryWithSuggestion = (query, queryAllByName, variant) => (
Expand Down
7 changes: 7 additions & 0 deletions src/wait-for.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
setImmediate,
setTimeout,
clearTimeout,
checkContainerType,
} from './helpers'
import {getConfig, runWithExpensiveErrorDiagnosticsDisabled} from './config'

Expand Down Expand Up @@ -89,6 +90,12 @@ function waitFor(
await new Promise(r => setImmediate(r))
}
} else {
try {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change was needed to keep these warnings intact. Without it, getWindowFromNode(container) throws and the error messages do not match the expected error. I thought it best to keep the warnings/behavior the same rather than update the test snapshots to match the new error message.

https://github.com/testing-library/dom-testing-library/blob/master/src/__tests__/base-queries-warn-on-invalid-container.js#L95-L127

checkContainerType(container)
} catch (e) {
reject(e)
return
}
intervalId = setInterval(checkRealTimersCallback, interval)
const {MutationObserver} = getWindowFromNode(container)
observer = new MutationObserver(checkRealTimersCallback)
Expand Down