Skip to content

Commit

Permalink
fix: prevent crashing on elements without parentElement (#871)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio authored Jan 12, 2021
1 parent cb70902 commit 6d32457
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
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

0 comments on commit 6d32457

Please sign in to comment.