Skip to content

Commit 418581f

Browse files
authored
fix(TS): waitForElementToBeRemoved: fix return type (#631)
1) Resolve to the type param T Keeping references to removed elements is a probable source of bugs and confusing tests. A reference should be easy enough to obtain with other test utilities; I submit that there's no added value in providing it here. 2) Resolve to boolean (or literal `true`) Not a bad choice by any means, but not particularly meaningful (the fulfillment of the promise is signal enough IMO) 3) Resolve to something empty This is what I've gone with. We are _literally_ waiting for removal, after all :) Fixes #610
1 parent 7f8a15e commit 418581f

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

src/__tests__/wait-for-element-to-be-removed.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,18 @@ test('requires an unempty array of elements to exist first (function form)', ()
7777
)
7878
})
7979

80+
test('after successful removal, fullfills promise with empty value (undefined)', () => {
81+
const {getByTestId} = renderIntoDocument(`
82+
<div data-testid="div"></div>
83+
`)
84+
const div = getByTestId('div')
85+
const waitResult = waitForElementToBeRemoved(() => getByTestId('div'), {
86+
timeout: 100,
87+
})
88+
div.parentElement.removeChild(div)
89+
return expect(waitResult).resolves.toBeUndefined()
90+
})
91+
8092
describe('timers', () => {
8193
const expectElementToBeRemoved = async () => {
8294
const importedWaitForElementToBeRemoved = importModule()

src/wait-for-element-to-be-removed.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ async function waitForElementToBeRemoved(callback, options) {
3434
result = callback()
3535
} catch (error) {
3636
if (error.name === 'TestingLibraryElementError') {
37-
return true
37+
return undefined
3838
}
3939
throw error
4040
}
4141
if (!isRemoved(result)) {
4242
throw timeoutError
4343
}
44-
return true
44+
return undefined
4545
}, options)
4646
}
4747

types/wait-for-element-to-be-removed.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ import { waitForOptions } from "./wait-for";
33
export function waitForElementToBeRemoved<T>(
44
callback: (() => T) | T,
55
options?: waitForOptions,
6-
): Promise<T>;
6+
): Promise<void>;

0 commit comments

Comments
 (0)