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
Show file tree
Hide file tree
Changes from 25 commits
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
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,15 @@
"code",
"test"
]
},
{
"login": "Tolsee",
"name": "Tulsi Sapkota",
"avatar_url": "https://avatars0.githubusercontent.com/u/16590492?v=4",
"profile": "https://github.com/Tolsee",
"contributions": [
"code"
]
}
]
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
[![downloads][downloads-badge]][npmtrends]
[![MIT License][license-badge]][license]

[![All Contributors](https://img.shields.io/badge/all_contributors-50-orange.svg?style=flat-square)](#contributors)
[![All Contributors](https://img.shields.io/badge/all_contributors-51-orange.svg?style=flat-square)](#contributors)
[![PRs Welcome][prs-badge]][prs]
[![Code of Conduct][coc-badge]][coc]

Expand Down Expand Up @@ -138,7 +138,7 @@ Thanks goes to these people ([emoji key][emojis]):
| [<img src="https://avatars3.githubusercontent.com/u/881986?v=4" width="100px;" alt="dadamssg"/><br /><sub><b>dadamssg</b></sub>](https://github.com/dadamssg)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=dadamssg "Code") | [<img src="https://avatars1.githubusercontent.com/u/186971?v=4" width="100px;" alt="Neil Kistner"/><br /><sub><b>Neil Kistner</b></sub>](https://neilkistner.com/)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=wyze "Code") | [<img src="https://avatars3.githubusercontent.com/u/1448597?v=4" width="100px;" alt="Ben Chauvette"/><br /><sub><b>Ben Chauvette</b></sub>](http://bdchauvette.net/)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=bdchauvette "Code") | [<img src="https://avatars2.githubusercontent.com/u/777527?v=4" width="100px;" alt="Jeff Baumgardt"/><br /><sub><b>Jeff Baumgardt</b></sub>](https://github.com/JeffBaumgardt)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=JeffBaumgardt "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=JeffBaumgardt "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/4658208?v=4" width="100px;" alt="Matan Kushner"/><br /><sub><b>Matan Kushner</b></sub>](http://matchai.me)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=matchai "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=matchai "Documentation") [🤔](#ideas-matchai "Ideas, Planning, & Feedback") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=matchai "Tests") | [<img src="https://avatars2.githubusercontent.com/u/5779538?v=4" width="100px;" alt="Alex Wendte"/><br /><sub><b>Alex Wendte</b></sub>](http://www.wendtedesigns.com/)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=themostcolm "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=themostcolm "Documentation") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=themostcolm "Tests") | [<img src="https://avatars0.githubusercontent.com/u/2196208?v=4" width="100px;" alt="Tamas Fodor"/><br /><sub><b>Tamas Fodor</b></sub>](https://github.com/ruffle1986)<br />[📖](https://github.com/kentcdodds/dom-testing-library/commits?author=ruffle1986 "Documentation") |
| [<img src="https://avatars3.githubusercontent.com/u/14793495?v=4" width="100px;" alt="Benjamin Eckardt"/><br /><sub><b>Benjamin Eckardt</b></sub>](https://github.com/BenjaminEckardt)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=BenjaminEckardt "Code") | [<img src="https://avatars3.githubusercontent.com/u/205752?v=4" width="100px;" alt="Ryan Campbell"/><br /><sub><b>Ryan Campbell</b></sub>](https://github.com/campbellr)<br />[📖](https://github.com/kentcdodds/dom-testing-library/commits?author=campbellr "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/1335519?v=4" width="100px;" alt="Taylor Briggs"/><br /><sub><b>Taylor Briggs</b></sub>](https://taylor-briggs.com)<br />[⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=TaylorBriggs "Tests") | [<img src="https://avatars2.githubusercontent.com/u/132233?v=4" width="100px;" alt="John Gozde"/><br /><sub><b>John Gozde</b></sub>](https://github.com/jgoz)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=jgoz "Code") | [<img src="https://avatars2.githubusercontent.com/u/3382565?v=4" width="100px;" alt="C. T. Lin"/><br /><sub><b>C. T. Lin</b></sub>](https://github.com/chentsulin)<br />[📖](https://github.com/kentcdodds/dom-testing-library/commits?author=chentsulin "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/5312329?v=4" width="100px;" alt="Terrence Wong"/><br /><sub><b>Terrence Wong</b></sub>](http://terrencewwong.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=terrencewwong "Code") | [<img src="https://avatars0.githubusercontent.com/u/12230408?v=4" width="100px;" alt="Soo Jae Hwang"/><br /><sub><b>Soo Jae Hwang</b></sub>](https://www.ossfinder.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=misoguy "Code") |
| [<img src="https://avatars0.githubusercontent.com/u/19773?v=4" width="100px;" alt="Royston Shufflebotham"/><br /><sub><b>Royston Shufflebotham</b></sub>](https://github.com/RoystonS)<br />[🐛](https://github.com/kentcdodds/dom-testing-library/issues?q=author%3ARoystonS "Bug reports") [💻](https://github.com/kentcdodds/dom-testing-library/commits?author=RoystonS "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=RoystonS "Documentation") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=RoystonS "Tests") | [<img src="https://avatars0.githubusercontent.com/u/591673?v=4" width="100px;" alt="Vadim Brodsky"/><br /><sub><b>Vadim Brodsky</b></sub>](http://www.vadimbrodsky.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=VadimBrodsky "Code") | [<img src="https://avatars3.githubusercontent.com/u/499898?v=4" width="100px;" alt="Eunjae Lee"/><br /><sub><b>Eunjae Lee</b></sub>](https://twitter.com/eunjae_lee)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=eunjae-lee "Code") | [<img src="https://avatars2.githubusercontent.com/u/167743?v=4" width="100px;" alt="David Peter"/><br /><sub><b>David Peter</b></sub>](http://davidpeter.me)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=sarenji "Code") | [<img src="https://avatars0.githubusercontent.com/u/13174025?v=4" width="100px;" alt="Shy Alter"/><br /><sub><b>Shy Alter</b></sub>](https://twitter.com/@puemos)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=puemos "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=puemos "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/11966621?v=4" width="100px;" alt="Łukasz Makuch"/><br /><sub><b>Łukasz Makuch</b></sub>](https://lukaszmakuch.pl)<br />[📦](#platform-lukaszmakuch "Packaging/porting to new platform") | [<img src="https://avatars1.githubusercontent.com/u/11150235?v=4" width="100px;" alt="Tyler Haas"/><br /><sub><b>Tyler Haas</b></sub>](https://github.com/tylerthehaas)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=tylerthehaas "Code") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=tylerthehaas "Tests") |
| [<img src="https://avatars2.githubusercontent.com/u/482561?v=4" width="100px;" alt="Vesa Laakso"/><br /><sub><b>Vesa Laakso</b></sub>](http://vesalaakso.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=valscion "Code") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=valscion "Tests") |
| [<img src="https://avatars2.githubusercontent.com/u/482561?v=4" width="100px;" alt="Vesa Laakso"/><br /><sub><b>Vesa Laakso</b></sub>](http://vesalaakso.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=valscion "Code") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=valscion "Tests") | [<img src="https://avatars0.githubusercontent.com/u/16590492?v=4" width="100px;" alt="Tulsi Sapkota"/><br /><sub><b>Tulsi Sapkota</b></sub>](https://github.com/Tolsee)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=Tolsee "Code") |

<!-- ALL-CONTRIBUTORS-LIST:END -->

Expand Down
334 changes: 334 additions & 0 deletions src/__tests__/wait-for-element-to-be-removed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,334 @@
import {waitForElementToBeRemoved} from '../'
// adds special assertions like toBeTruthy
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

toBeTruthy is built-into jest. This adds things like toBeInTheDocument. But I don't think we need this comment anyway. Please remove it.

import 'jest-dom/extend-expect'
import {render} from './helpers/test-utils'
import document from './helpers/document'

jest.useFakeTimers()

const skipSomeTimeForMutationObserver = (delayMs = 50) => {
jest.advanceTimersByTime(delayMs)
jest.runAllImmediates()
}

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>`,
)

const testEle = render(
`<div data-testid="the-element-we-are-looking-for"></div>`,
).container.firstChild
testEle.parentNode.removeChild(testEle)
container.appendChild(testEle)

let nextElIndex = 0
const makeMutationFn = () => () => {
container.appendChild(
render(
`<div data-testid="another-element-that-causes-mutation-${++nextElIndex}"></div>`,
).container.firstChild,
)
}

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')),
() => getByTestId('the-element-we-are-looking-for'),
],
]

const callback = jest
.fn(() => {
throw new Error('No more calls are expected.')
})
.mockName('callback')
.mockImplementation(() => true)

const successHandler = jest.fn().mockName('successHandler')
const errorHandler = jest.fn().mockName('errorHandler')

const promise = waitForElementToBeRemoved(callback, {container}).then(
successHandler,
errorHandler,
)

// One synchronous `callback` call is expected.
expect(callback).toHaveBeenCalledTimes(1)
expect(successHandler).toHaveBeenCalledTimes(0)
expect(errorHandler).toHaveBeenCalledTimes(0)

skipSomeTimeForMutationObserver()

// No more expected calls without DOM mutations.
expect(callback).toHaveBeenCalledTimes(1)
expect(successHandler).toHaveBeenCalledTimes(0)
expect(errorHandler).toHaveBeenCalledTimes(0)

// Perform mutations one by one, waiting for each to trigger `MutationObserver`.
for (const [mutationImpl, callbackImpl] of mutationsAndCallbacks) {
callback.mockImplementation(callbackImpl)
mutationImpl()
skipSomeTimeForMutationObserver()
}

await promise

expect(callback).toHaveBeenCalledTimes(1 + mutationsAndCallbacks.length)
expect(successHandler).toHaveBeenCalledTimes(1)
expect(successHandler).toHaveBeenCalledWith(true)
expect(errorHandler).toHaveBeenCalledTimes(0)
})

test('it waits characterData mutation', async () => {
const {container} = render(`<div>initial text</div>`)

const callback = jest
.fn(() => container.textContent === 'initial text')
.mockName('callback')
const successHandler = jest.fn().mockName('successHandler')
const errorHandler = jest.fn().mockName('errorHandler')

const promise = waitForElementToBeRemoved(callback, {container}).then(
successHandler,
errorHandler,
)

expect(successHandler).toHaveBeenCalledTimes(0)
expect(errorHandler).toHaveBeenCalledTimes(0)
expect(callback).toHaveBeenCalledTimes(1)

skipSomeTimeForMutationObserver()

expect(callback).toHaveBeenCalledTimes(1)
expect(successHandler).toHaveBeenCalledTimes(0)
expect(errorHandler).toHaveBeenCalledTimes(0)

container.querySelector('div').innerHTML = 'new text'
skipSomeTimeForMutationObserver()
await promise

expect(successHandler).toHaveBeenCalledTimes(1)
expect(successHandler).toHaveBeenCalledWith(true)
expect(errorHandler).toHaveBeenCalledTimes(0)
expect(callback).toHaveBeenCalledTimes(2)
})

test('it waits for the attributes mutation', async () => {
const {container} = render(``)
container.setAttribute('data-test-attribute', 'PASSED')

const callback = jest
.fn(() => container.getAttribute('data-test-attribute'))
.mockName('callback')
const successHandler = jest.fn().mockName('successHandler')
const errorHandler = jest.fn().mockName('errorHandler')

const promise = waitForElementToBeRemoved(callback, {
container,
}).then(successHandler, errorHandler)

expect(callback).toHaveBeenCalledTimes(1)
expect(successHandler).toHaveBeenCalledTimes(0)
expect(errorHandler).toHaveBeenCalledTimes(0)

skipSomeTimeForMutationObserver()

expect(callback).toHaveBeenCalledTimes(1)
expect(successHandler).toHaveBeenCalledTimes(0)
expect(errorHandler).toHaveBeenCalledTimes(0)

container.removeAttribute('data-test-attribute')
skipSomeTimeForMutationObserver()
await promise

expect(callback).toHaveBeenCalledTimes(2)
expect(successHandler).toHaveBeenCalledTimes(1)
expect(successHandler).toHaveBeenCalledWith(true)
expect(errorHandler).toHaveBeenCalledTimes(0)
})

test('it throws if timeout is exceeded', async () => {
const {container} = render(``)

const callback = jest.fn(() => true).mockName('callback')
const successHandler = jest.fn().mockName('successHandler')
const errorHandler = jest.fn().mockName('errorHandler')

const promise = waitForElementToBeRemoved(callback, {
container,
timeout: 70,
mutationObserverOptions: {attributes: true},
}).then(successHandler, errorHandler)

expect(callback).toHaveBeenCalledTimes(1)
expect(successHandler).toHaveBeenCalledTimes(0)
expect(errorHandler).toHaveBeenCalledTimes(0)

container.setAttribute('data-test-attribute', 'something changed once')
skipSomeTimeForMutationObserver(50)

expect(callback).toHaveBeenCalledTimes(2)
expect(successHandler).toHaveBeenCalledTimes(0)
expect(errorHandler).toHaveBeenCalledTimes(0)

container.setAttribute('data-test-attribute', 'something changed twice')
skipSomeTimeForMutationObserver(50)
await promise

expect(callback).toHaveBeenCalledTimes(3)
expect(successHandler).toHaveBeenCalledTimes(0)
expect(errorHandler).toHaveBeenCalledTimes(1)
expect(errorHandler).toHaveBeenCalledWith(
new Error('Timed out in waitForElementToBeRemoved.'),
)
})

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
.fn(() => getByTestId('initial-element'))
.mockName('callback')
const callbackForFalsy = jest
.fn(() => queryByTestId('initial-element'))
.mockName('callback')
const callbackForEmptyArray = jest
.fn(() => queryAllByTestId('initial-element'))
.mockName('callback')
const successHandler = jest.fn().mockName('successHandler')
const errorHandler = jest.fn().mockName('errorHandler')

const errorPromise = waitForElementToBeRemoved(callbackForError, {
container,
timeout: 70,
mutationObserverOptions: {attributes: true},
}).then(successHandler, errorHandler)
const falsyPromise = waitForElementToBeRemoved(callbackForFalsy, {
container,
timeout: 70,
mutationObserverOptions: {attributes: true},
}).then(successHandler, errorHandler)
const emptyArrayPromise = waitForElementToBeRemoved(callbackForEmptyArray, {
container,
timeout: 70,
mutationObserverOptions: {attributes: true},
}).then(successHandler, errorHandler)

expect(callbackForError).toHaveBeenCalledTimes(1)
expect(callbackForFalsy).toHaveBeenCalledTimes(1)
expect(callbackForEmptyArray).toHaveBeenCalledTimes(1)

expect(successHandler).toHaveBeenCalledTimes(0)
expect(errorHandler).toHaveBeenCalledTimes(0)

await errorPromise
await falsyPromise
await emptyArrayPromise

expect(successHandler).toHaveBeenCalledTimes(0)
expect(errorHandler).toHaveBeenCalledTimes(3)

container.setAttribute('data-test-attribute', 'something changed once')
skipSomeTimeForMutationObserver(50)

expect(callbackForError).toHaveBeenCalledTimes(1)
expect(callbackForFalsy).toHaveBeenCalledTimes(1)
expect(successHandler).toHaveBeenCalledTimes(0)
expect(errorHandler).toHaveBeenCalledTimes(3)
expect(errorHandler.mock.calls[0]).toMatchInlineSnapshot(`
Array [
[Error: Unable to find an element by: [data-testid="initial-element"]

<div />],
]
`)
expect(errorHandler).toHaveBeenNthCalledWith(
2,
new 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).toHaveBeenNthCalledWith(
3,
new 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 () => {
render(``)
const el = document.createElement('p')
document.body.appendChild(el)
el.innerHTML = 'I changed!'
const callback = jest
.fn(() => el.textContent === 'I changed!')
.mockName('callback')
const successHandler = jest.fn().mockName('successHandler')
const errorHandler = jest.fn().mockName('errorHandler')

let promise
if (typeof window === 'undefined') {
promise = waitForElementToBeRemoved(callback, {container: document}).then(
successHandler,
errorHandler,
)
} else {
promise = waitForElementToBeRemoved(callback).then(
successHandler,
errorHandler,
)
}

skipSomeTimeForMutationObserver()

expect(callback).toHaveBeenCalledTimes(1)
expect(successHandler).toHaveBeenCalledTimes(0)
expect(errorHandler).toHaveBeenCalledTimes(0)

el.innerHTML = 'Changed!'
skipSomeTimeForMutationObserver()
await promise

expect(callback).toHaveBeenCalledTimes(2)
expect(successHandler).toHaveBeenCalledTimes(1)
expect(successHandler).toHaveBeenCalledWith(true)
expect(errorHandler).toHaveBeenCalledTimes(0)

document.getElementsByTagName('html')[0].innerHTML = '' // cleans the document
})

test('throws an error if callback is not a function', async () => {
const successHandler = jest.fn().mockName('successHandler')
const errorHandler = jest.fn().mockName('errorHandler')

let promise
if (typeof window === 'undefined') {
promise = waitForElementToBeRemoved(undefined, {container: document}).then(
successHandler,
errorHandler,
)
} else {
promise = waitForElementToBeRemoved().then(successHandler, errorHandler)
}

skipSomeTimeForMutationObserver()
await promise

expect(errorHandler).toHaveBeenCalledTimes(1)
expect(errorHandler).toHaveBeenCalledWith(
new Error(
'waitForElementToBeRemoved requires a function as the first parameter',
),
)
expect(successHandler).toHaveBeenCalledTimes(0)
})
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as queryHelpers from './query-helpers'
export * from './queries'
export * from './wait'
export * from './wait-for-element'
export * from './wait-for-element-to-be-removed'
export * from './wait-for-dom-change'
export {getDefaultNormalizer} from './matches'
export * from './get-node-text'
Expand Down
Loading