Skip to content
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
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 Feb 22, 2019
c547962
Implement waitForElementToBeRemoved method
Tolsee Feb 22, 2019
cc21522
Export waitForElementToBeRemoved
Tolsee Feb 22, 2019
c75e492
Add as contributor
Tolsee Feb 22, 2019
a349725
Update README.md after rebase
Tolsee Feb 22, 2019
d1d303d
Throw error is the element is not present in the first place
Tolsee Feb 23, 2019
39fea0e
Add test for full coverage
Tolsee Feb 23, 2019
36c4e4c
User jest time faker
Tolsee Feb 24, 2019
f5b081d
Add typings
Tolsee Feb 24, 2019
2dedb1f
Cleanup
Tolsee Feb 24, 2019
54cbc89
Update snapshot
Tolsee Feb 24, 2019
999ff5c
Get rid of iife
Tolsee Feb 25, 2019
e7a018e
Cleanup from review
Tolsee Feb 26, 2019
0518e78
Check for empty array as well
Tolsee Feb 26, 2019
d6150af
Check for error, falsy and empty to check whether the element is removed
Tolsee Feb 26, 2019
4e2f81c
error not defined error fix
Tolsee Feb 27, 2019
20ada81
Change snapshot to inline snapshot
Tolsee Feb 27, 2019
46a952f
Update snapshot
Tolsee Feb 27, 2019
618c240
Merge branch 'ts-wait-for-element-to-be-removed' of https://github.co…
Tolsee Feb 27, 2019
af327d1
Merge branch 'master' into ts-wait-for-element-to-be-removed
Tolsee Feb 27, 2019
505d783
Only observe mutations if synchronous test passes
Tolsee Feb 27, 2019
6fcfc83
only observer if synchronous test passes fix
Tolsee Feb 27, 2019
579c876
Await for the respective promise to resolve in test
Tolsee Feb 27, 2019
2f7452d
Snapshot test to normal toHaveBeenCalledWith assertions
Tolsee Feb 28, 2019
20cd110
Update comment
Tolsee Feb 28, 2019
bc2e3f2
Remove comment
Tolsee Feb 28, 2019
9834de2
test: improve tests for wait-for-element-to-be-removed
Mar 9, 2019
17f136f
test: fix everything
Mar 9, 2019
6664f0a
test: do not test unstable node versions
Mar 9, 2019
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
Prev Previous commit
Next Next commit
Implement waitForElementToBeRemoved method
  • Loading branch information
Tolsee committed Feb 22, 2019
commit c547962fec5cc3a780cbee3c02becbd82d387d6d
53 changes: 53 additions & 0 deletions src/wait-for-element-to-be-removed.js
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',
Copy link
Collaborator

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.

)
}
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}