Skip to content

fix: prevent crashing on elements without parentElement #871

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
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
14 changes: 14 additions & 0 deletions src/__tests__/wait-for-element-to-be-removed.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ test('requires an element to exist first', () => {
)
})

test("requires element's parent to exist first", () => {
const {getByTestId} = renderIntoDocument(`
<div data-testid="div">asd</div>
`)
const div = getByTestId('div')
div.parentElement.removeChild(div)

return expect(
waitForElementToBeRemoved(div),
).rejects.toThrowErrorMatchingInlineSnapshot(
`"The element(s) given to waitForElementToBeRemoved are already removed. waitForElementToBeRemoved requires that the element(s) exist(s) before waiting for removal."`,
)
})

test('requires an unempty array of elements to exist first', () => {
return expect(
waitForElementToBeRemoved([]),
Expand Down
1 change: 1 addition & 0 deletions src/wait-for-element-to-be-removed.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ async function waitForElementToBeRemoved(callback, options) {
const elements = Array.isArray(callback) ? callback : [callback]
const getRemainingElements = elements.map(element => {
let parent = element.parentElement
if(parent === null) return () => null
while (parent.parentElement) parent = parent.parentElement
return () => (parent.contains(element) ? element : null)
})
Expand Down