-
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
Change snapshot to inline snapshot
- Loading branch information
commit 20ada81c2bb1b7cabf943db8934f6293d9d441ed
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 |
---|---|---|
|
@@ -11,8 +11,8 @@ const skipSomeTimeForMutationObserver = (delayMs = 50) => { | |
jest.runAllImmediates() | ||
} | ||
|
||
test('it waits for the callback to throw error or a falsy value and only reacts to DOM mutations', async () => { | ||
const {container, getByTestId} = render( | ||
test('it waits for the callback to throw error, a falsy value or empty array and only reacts to DOM mutations', async () => { | ||
const {container, getByTestId, queryAllByTestId} = render( | ||
`<div data-testid="initial-element"> | ||
</div>`, | ||
) | ||
|
@@ -35,6 +35,10 @@ test('it waits for the callback to throw error or a falsy value and only reacts | |
const mutationsAndCallbacks = [ | ||
[makeMutationFn(), () => true], | ||
[makeMutationFn(), () => getByTestId('the-element-we-are-looking-for')], | ||
[ | ||
makeMutationFn(), | ||
() => queryAllByTestId('the-element-we-are-looking-for'), | ||
], | ||
[ | ||
() => | ||
container.removeChild(getByTestId('the-element-we-are-looking-for')), | ||
|
@@ -80,7 +84,11 @@ test('it waits for the callback to throw error or a falsy value and only reacts | |
|
||
expect(callback).toHaveBeenCalledTimes(1 + mutationsAndCallbacks.length) | ||
expect(successHandler).toHaveBeenCalledTimes(1) | ||
expect(successHandler.mock.calls[0]).toMatchSnapshot() | ||
expect(successHandler.mock.calls[0]).toMatchInlineSnapshot(` | ||
Array [ | ||
true, | ||
] | ||
`) | ||
expect(errorHandler).toHaveBeenCalledTimes(0) | ||
}) | ||
|
||
|
@@ -113,7 +121,11 @@ test('it waits characterData mutation', async () => { | |
await promise | ||
|
||
expect(successHandler).toHaveBeenCalledTimes(1) | ||
expect(successHandler.mock.calls[0]).toMatchSnapshot() | ||
expect(successHandler.mock.calls[0]).toMatchInlineSnapshot(` | ||
Array [ | ||
true, | ||
] | ||
`) | ||
expect(errorHandler).toHaveBeenCalledTimes(0) | ||
expect(callback).toHaveBeenCalledTimes(2) | ||
}) | ||
|
@@ -148,7 +160,11 @@ test('it waits for the attributes mutation', async () => { | |
|
||
expect(callback).toHaveBeenCalledTimes(2) | ||
expect(successHandler).toHaveBeenCalledTimes(1) | ||
expect(successHandler.mock.calls[0]).toMatchSnapshot() | ||
expect(successHandler.mock.calls[0]).toMatchInlineSnapshot(` | ||
Array [ | ||
true, | ||
] | ||
`) | ||
expect(errorHandler).toHaveBeenCalledTimes(0) | ||
}) | ||
|
||
|
@@ -183,10 +199,14 @@ test('it throws if timeout is exceeded', async () => { | |
expect(callback).toHaveBeenCalledTimes(3) | ||
expect(successHandler).toHaveBeenCalledTimes(0) | ||
expect(errorHandler).toHaveBeenCalledTimes(1) | ||
expect(errorHandler.mock.calls[0]).toMatchSnapshot() | ||
expect(errorHandler.mock.calls[0]).toMatchInlineSnapshot(` | ||
Array [ | ||
[Error: Timed out in waitForElementToBeRemoved.], | ||
] | ||
`) | ||
}) | ||
|
||
test('it returns error immediately if there callback returns falsy value or error before any mutations', async () => { | ||
test('it returns error immediately if there callback returns falsy value, empty array or error before any mutations', async () => { | ||
const {container, getByTestId, queryByTestId, queryAllByTestId} = render(``) | ||
|
||
const callbackForError = jest | ||
|
@@ -235,8 +255,16 @@ test('it returns error immediately if there callback returns falsy value or erro | |
expect(successHandler).toHaveBeenCalledTimes(0) | ||
expect(errorHandler).toHaveBeenCalledTimes(3) | ||
expect(errorHandler.mock.calls[0]).toMatchSnapshot() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess I'm fine with this being a snapshot, but please make it inline. |
||
expect(errorHandler.mock.calls[1]).toMatchSnapshot() | ||
expect(errorHandler.mock.calls[3]).toMatchSnapshot() | ||
expect(errorHandler.mock.calls[1]).toMatchInlineSnapshot(` | ||
Array [ | ||
[Error: The callback function which was passed did not return an element or non-empty array of elements. waitForElementToBeRemoved requires that the element(s) exist before waiting for removal.], | ||
] | ||
`) | ||
expect(errorHandler.mock.calls[2]).toMatchInlineSnapshot(` | ||
Array [ | ||
[Error: The callback function which was passed did not return an element or non-empty array of elements. waitForElementToBeRemoved requires that the element(s) exist before waiting for removal.], | ||
] | ||
`) | ||
}) | ||
|
||
test('works if a container is not defined', async () => { | ||
|
@@ -275,7 +303,11 @@ test('works if a container is not defined', async () => { | |
|
||
expect(callback).toHaveBeenCalledTimes(2) | ||
expect(successHandler).toHaveBeenCalledTimes(1) | ||
expect(successHandler.mock.calls[0]).toMatchSnapshot() | ||
expect(successHandler.mock.calls[0]).toMatchInlineSnapshot(` | ||
Array [ | ||
true, | ||
] | ||
`) | ||
expect(errorHandler).toHaveBeenCalledTimes(0) | ||
|
||
document.getElementsByTagName('html')[0].innerHTML = '' // cleans the document | ||
|
@@ -299,6 +331,10 @@ test('throws an error if callback is not a function', async () => { | |
await promise | ||
|
||
expect(errorHandler).toHaveBeenCalledTimes(1) | ||
expect(errorHandler.mock.calls[0]).toMatchSnapshot() | ||
expect(errorHandler.mock.calls[0]).toMatchInlineSnapshot(` | ||
Array [ | ||
[Error: waitForElementToBeRemoved requires a function as the first parameter], | ||
] | ||
`) | ||
expect(successHandler).toHaveBeenCalledTimes(0) | ||
}) |
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.
Any reason this couldn't be simplified to:
Or you could also replace this line and the one above with:
I don't really care either way, but using snapshots for this doesn't make any sense to me personally.
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.
Yeah, make sense. I am a little 🆕 to
jest
that is the only reason.