Skip to content

Commit

Permalink
fix: Pass container from findBy through to waitFor (#868)
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanforce authored Jan 12, 2021
1 parent 71b4f46 commit b4f1d45
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 4 deletions.
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 {
checkContainerType(container)
} catch (e) {
reject(e)
return
}
intervalId = setInterval(checkRealTimersCallback, interval)
const {MutationObserver} = getWindowFromNode(container)
observer = new MutationObserver(checkRealTimersCallback)
Expand Down

0 comments on commit b4f1d45

Please sign in to comment.