-
Notifications
You must be signed in to change notification settings - Fork 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(waitForElementToBeRemoved): add the new utility function (#218)
* Add tests and snapshots * Implement waitForElementToBeRemoved method * Export waitForElementToBeRemoved * Add as contributor * Update README.md after rebase * Throw error is the element is not present in the first place * Add test for full coverage * User jest time faker * Add typings * Cleanup * Update snapshot * Get rid of iife * Cleanup from review * Check for empty array as well * Check for error, falsy and empty to check whether the element is removed * error not defined error fix * Change snapshot to inline snapshot * Update snapshot * Only observe mutations if synchronous test passes * only observer if synchronous test passes fix * Await for the respective promise to resolve in test * Snapshot test to normal toHaveBeenCalledWith assertions * Update comment * Remove comment * test: improve tests for wait-for-element-to-be-removed * test: fix everything * test: do not test unstable node versions
- Loading branch information
Showing
24 changed files
with
186 additions
and
104 deletions.
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
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 |
---|---|---|
|
@@ -6,7 +6,6 @@ cache: | |
notifications: | ||
email: false | ||
node_js: | ||
- 'node' | ||
- '10' | ||
- '8' | ||
install: npm install | ||
|
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
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
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 |
---|---|---|
@@ -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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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.