Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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) return () => null
while (parent.parentElement) parent = parent.parentElement
return () => (parent.contains(element) ? element : null)
})
Expand Down