-
Notifications
You must be signed in to change notification settings - Fork 470
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
Wait for element to be removed #218
Merged
kentcdodds
merged 29 commits into
testing-library:master
from
Tolsee:ts-wait-for-element-to-be-removed
Mar 9, 2019
Merged
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
f3eca1c
Add tests and snapshots
Tolsee c547962
Implement waitForElementToBeRemoved method
Tolsee cc21522
Export waitForElementToBeRemoved
Tolsee c75e492
Add as contributor
Tolsee a349725
Update README.md after rebase
Tolsee d1d303d
Throw error is the element is not present in the first place
Tolsee 39fea0e
Add test for full coverage
Tolsee 36c4e4c
User jest time faker
Tolsee f5b081d
Add typings
Tolsee 2dedb1f
Cleanup
Tolsee 54cbc89
Update snapshot
Tolsee 999ff5c
Get rid of iife
Tolsee e7a018e
Cleanup from review
Tolsee 0518e78
Check for empty array as well
Tolsee d6150af
Check for error, falsy and empty to check whether the element is removed
Tolsee 4e2f81c
error not defined error fix
Tolsee 20ada81
Change snapshot to inline snapshot
Tolsee 46a952f
Update snapshot
Tolsee 618c240
Merge branch 'ts-wait-for-element-to-be-removed' of https://github.co…
Tolsee af327d1
Merge branch 'master' into ts-wait-for-element-to-be-removed
Tolsee 505d783
Only observe mutations if synchronous test passes
Tolsee 6fcfc83
only observer if synchronous test passes fix
Tolsee 579c876
Await for the respective promise to resolve in test
Tolsee 2f7452d
Snapshot test to normal toHaveBeenCalledWith assertions
Tolsee 20cd110
Update comment
Tolsee bc2e3f2
Remove comment
Tolsee 9834de2
test: improve tests for wait-for-element-to-be-removed
17f136f
test: fix everything
6664f0a
test: do not test unstable node versions
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Implement waitForElementToBeRemoved method
- Loading branch information
commit c547962fec5cc3a780cbee3c02becbd82d387d6d
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import {getDocument, getSetImmediate, newMutationObserver} from './helpers' | ||
|
||
function waitForElementToBeRemoved( | ||
callback, | ||
{ | ||
container = getDocument(), | ||
timeout = 4500, | ||
mutationObserverOptions = { | ||
subtree: true, | ||
childList: true, | ||
attributes: true, | ||
characterData: true, | ||
}, | ||
} = {}, | ||
) { | ||
return new Promise((resolve, reject) => { | ||
if (typeof callback !== 'function') { | ||
reject( | ||
'waitForElementToBeRemoved requires a callback as the first parameter', | ||
) | ||
} | ||
const timer = setTimeout(onTimeout, timeout) | ||
const observer = newMutationObserver(onMutation) | ||
observer.observe(container, mutationObserverOptions) | ||
function onDone(error, result) { | ||
const setImmediate = getSetImmediate() | ||
clearTimeout(timer) | ||
Tolsee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
setImmediate(() => observer.disconnect()) | ||
if (error) { | ||
reject(error) | ||
} else { | ||
resolve(result) | ||
} | ||
} | ||
function onMutation() { | ||
try { | ||
const result = callback() | ||
if (!result) { | ||
onDone(null, true) | ||
} | ||
// If `callback` returns truthy value, wait for the next mutation or timeout. | ||
} catch (error) { | ||
onDone(null, true) | ||
} | ||
} | ||
function onTimeout() { | ||
onDone(new Error('Timed out in waitForElementToBeRemoved.'), null) | ||
} | ||
onMutation() | ||
}) | ||
} | ||
|
||
export {waitForElementToBeRemoved} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: requires a function. Callback is jargon.