-
Notifications
You must be signed in to change notification settings - Fork 469
Wait for element to be removed #218
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
Merged
kentcdodds
merged 29 commits into
testing-library:master
from
Tolsee:ts-wait-for-element-to-be-removed
Mar 9, 2019
Merged
Changes from 28 commits
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
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
@@ -1,20 +1,6 @@ | ||
const baseConfig = require('kcd-scripts/jest') | ||
|
||
module.exports = { | ||
collectCoverageFrom: baseConfig.collectCoverageFrom, | ||
coverageThreshold: baseConfig.coverageThreshold, | ||
projects: [ | ||
{ | ||
...baseConfig, | ||
displayName: 'jsdom', | ||
testEnvironment: 'jest-environment-jsdom', | ||
}, | ||
{ | ||
...baseConfig, | ||
displayName: 'node', | ||
testEnvironment: 'jest-environment-node', | ||
}, | ||
], | ||
// this is for eslint | ||
modulePaths: baseConfig.modulePaths, | ||
...baseConfig, | ||
testEnvironment: 'jest-environment-jsdom', | ||
} |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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
This file contains hidden or 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
40 changes: 40 additions & 0 deletions
40
src/__tests__/wait-for-element-to-be-removed.fake-timers.js
This file contains hidden or 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,40 @@ | ||
import 'jest-dom/extend-expect' | ||
import {waitForElementToBeRemoved} from '../' | ||
import {render} from './helpers/test-utils' | ||
|
||
jest.useFakeTimers() | ||
|
||
test('requires a function as the first parameter', () => { | ||
return expect( | ||
waitForElementToBeRemoved(), | ||
).rejects.toThrowErrorMatchingInlineSnapshot( | ||
`"waitForElementToBeRemoved requires a function as the first parameter"`, | ||
) | ||
}) | ||
|
||
test('requires an element to exist first', () => { | ||
return expect( | ||
waitForElementToBeRemoved(() => null), | ||
).rejects.toThrowErrorMatchingInlineSnapshot( | ||
`"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('requires an unempty array of elements to exist first', () => { | ||
return expect( | ||
waitForElementToBeRemoved(() => []), | ||
).rejects.toThrowErrorMatchingInlineSnapshot( | ||
`"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('times out after 4500ms by default', () => { | ||
const {container} = render(`<div></div>`) | ||
const promise = expect( | ||
waitForElementToBeRemoved(() => container), | ||
).rejects.toThrowErrorMatchingInlineSnapshot( | ||
`"Timed out in waitForElementToBeRemoved."`, | ||
) | ||
jest.advanceTimersByTime(4501) | ||
return promise | ||
}) |
This file contains hidden or 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,36 @@ | ||
import 'jest-dom/extend-expect' | ||
import {waitForElementToBeRemoved} from '../' | ||
import {renderIntoDocument} from './helpers/test-utils' | ||
|
||
test('resolves on mutation only when the element is removed', async () => { | ||
const {queryAllByTestId} = renderIntoDocument(` | ||
<div data-testid="div"></div> | ||
<div data-testid="div"></div> | ||
`) | ||
const divs = queryAllByTestId('div') | ||
// first mutation | ||
setTimeout(() => { | ||
divs.forEach(d => d.setAttribute('id', 'mutated')) | ||
}) | ||
// removal | ||
setTimeout(() => { | ||
divs.forEach(div => div.parentElement.removeChild(div)) | ||
}, 100) | ||
// the timeout is here for two reasons: | ||
// 1. It helps test the timeout config | ||
// 2. The element should be removed immediately | ||
// so if it doesn't in the first 100ms then we know something's wrong | ||
// so we'll fail early and not wait the full timeout | ||
await waitForElementToBeRemoved(() => queryAllByTestId('div'), {timeout: 200}) | ||
}) | ||
|
||
test('resolves on mutation if callback throws an error', async () => { | ||
const {getByTestId} = renderIntoDocument(` | ||
<div data-testid="div"></div> | ||
`) | ||
const div = getByTestId('div') | ||
setTimeout(() => { | ||
div.parentElement.removeChild(div) | ||
}) | ||
await waitForElementToBeRemoved(() => getByTestId('div'), {timeout: 100}) | ||
}) |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
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.
Honestly, running all the exact same tests in
node
led to some poor testing practices and I didn't like the over-testing either. If someone really wants to have tests like this, then we can add aother/node/__tests__/
directory with aother/node/jest.config.js
file. I want the tests to be different.Also, I don't think it's worthwhile to have tests for node. I'm pretty sure there's a very small number of people who benefit from this.