Skip to content

Commit 6e9d7c5

Browse files
rbrtsmithKent C. Dodds
authored and
Kent C. Dodds
committed
fix(waitForElement): ensure waitForElement responds to attribute and textNode changes (#74)
* ensure waitForElement responds to attribute and textNode changes by default * Add rbrtsmith as a contributor * Update README with change mutationObserverOptions * Add missing new line in test
1 parent 3ba2600 commit 6e9d7c5

File tree

5 files changed

+75
-8
lines changed

5 files changed

+75
-8
lines changed

.all-contributorsrc

+11
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,17 @@
276276
"contributions": [
277277
"code"
278278
]
279+
},
280+
{
281+
"login": "rbrtsmith",
282+
"name": "Robert Smith",
283+
"avatar_url": "https://avatars2.githubusercontent.com/u/4982001?v=4",
284+
"profile": "http://rbrtsmith.com/",
285+
"contributions": [
286+
"bug",
287+
"ideas",
288+
"doc"
289+
]
279290
}
280291
]
281292
}

README.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
[![downloads][downloads-badge]][npmtrends]
1717
[![MIT License][license-badge]][license]
1818

19-
[![All Contributors](https://img.shields.io/badge/all_contributors-27-orange.svg?style=flat-square)](#contributors)
19+
[![All Contributors](https://img.shields.io/badge/all_contributors-28-orange.svg?style=flat-square)](#contributors)
2020
[![PRs Welcome][prs-badge]][prs]
2121
[![Code of Conduct][coc-badge]][coc]
2222

@@ -469,9 +469,8 @@ The default `container` is the global `document`. Make sure the elements you wai
469469
The default `timeout` is `4500ms` which will keep you under
470470
[Jest's default timeout of `5000ms`](https://facebook.github.io/jest/docs/en/jest-object.html#jestsettimeouttimeout).
471471

472-
<a name="mutationobserveroptions"></a>The default `mutationObserverOptions` is `{subtree: true, childList: true}` which will detect
473-
additions and removals of child elements (including text nodes) in the `container` and any of its descendants.
474-
It won't detect attribute changes unless you add `attributes: true` to the options.
472+
<a name="mutationobserveroptions"></a>The default `mutationObserverOptions` is `{subtree: true, childList: true, attributes: true, characterData: true}` which will detect
473+
additions and removals of child elements (including text nodes) in the `container` and any of its descendants. It will also detect attribute changes.
475474

476475
### `fireEvent`
477476

@@ -874,7 +873,7 @@ Thanks goes to these people ([emoji key][emojis]):
874873
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
875874
| [<img src="https://avatars1.githubusercontent.com/u/1241511?s=460&v=4" width="100px;"/><br /><sub><b>Anto Aravinth</b></sub>](https://github.com/antoaravinth)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=antoaravinth "Code") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=antoaravinth "Tests") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=antoaravinth "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/3462296?v=4" width="100px;"/><br /><sub><b>Jonah Moses</b></sub>](https://github.com/JonahMoses)<br />[📖](https://github.com/kentcdodds/dom-testing-library/commits?author=JonahMoses "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/4002543?v=4" width="100px;"/><br /><sub><b>Łukasz Gandecki</b></sub>](http://team.thebrain.pro)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=lgandecki "Code") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=lgandecki "Tests") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=lgandecki "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/498274?v=4" width="100px;"/><br /><sub><b>Ivan Babak</b></sub>](https://sompylasar.github.io)<br />[🐛](https://github.com/kentcdodds/dom-testing-library/issues?q=author%3Asompylasar "Bug reports") [🤔](#ideas-sompylasar "Ideas, Planning, & Feedback") [💻](https://github.com/kentcdodds/dom-testing-library/commits?author=sompylasar "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=sompylasar "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/4439618?v=4" width="100px;"/><br /><sub><b>Jesse Day</b></sub>](https://github.com/jday3)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=jday3 "Code") | [<img src="https://avatars0.githubusercontent.com/u/15199?v=4" width="100px;"/><br /><sub><b>Ernesto García</b></sub>](http://gnapse.github.io)<br />[💬](#question-gnapse "Answering Questions") [💻](https://github.com/kentcdodds/dom-testing-library/commits?author=gnapse "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=gnapse "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/2747424?v=4" width="100px;"/><br /><sub><b>Josef Maxx Blake</b></sub>](http://jomaxx.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=jomaxx "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=jomaxx "Documentation") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=jomaxx "Tests") |
876875
| [<img src="https://avatars3.githubusercontent.com/u/725236?v=4" width="100px;"/><br /><sub><b>Alex Cook</b></sub>](https://github.com/alecook)<br />[📖](https://github.com/kentcdodds/dom-testing-library/commits?author=alecook "Documentation") [💡](#example-alecook "Examples") | [<img src="https://avatars3.githubusercontent.com/u/10348212?v=4" width="100px;"/><br /><sub><b>Daniel Cook</b></sub>](https://github.com/dfcook)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=dfcook "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=dfcook "Documentation") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=dfcook "Tests") | [<img src="https://avatars2.githubusercontent.com/u/21194045?s=400&v=4" width="100px;"/><br /><sub><b>Thomas Chia</b></sub>](https://github.com/thchia)<br />[🐛](https://github.com/kentcdodds/dom-testing-library/issues?q=author%3Athchia "Bug reports") [💻](https://github.com/kentcdodds/dom-testing-library/commits?author=thchia "Code") | [<img src="https://avatars1.githubusercontent.com/u/28659384?v=4" width="100px;"/><br /><sub><b>Tim Deschryver</b></sub>](https://github.com/tdeschryver)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=tdeschryver "Code") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=tdeschryver "Tests") | [<img src="https://avatars3.githubusercontent.com/u/1571667?v=4" width="100px;"/><br /><sub><b>Alex Krolick</b></sub>](https://alexkrolick.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=alexkrolick "Code") | [<img src="https://avatars2.githubusercontent.com/u/2224291?v=4" width="100px;"/><br /><sub><b>Maddi Joyce</b></sub>](http://www.maddijoyce.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=maddijoyce "Code") | [<img src="https://avatars1.githubusercontent.com/u/25429764?v=4" width="100px;"/><br /><sub><b>Peter Kamps</b></sub>](https://github.com/npeterkamps)<br />[🐛](https://github.com/kentcdodds/dom-testing-library/issues?q=author%3Anpeterkamps "Bug reports") [💻](https://github.com/kentcdodds/dom-testing-library/commits?author=npeterkamps "Code") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=npeterkamps "Tests") |
877-
| [<img src="https://avatars2.githubusercontent.com/u/21689428?v=4" width="100px;"/><br /><sub><b>Jonathan Stoye</b></sub>](http://jonathanstoye.de)<br />[📖](https://github.com/kentcdodds/dom-testing-library/commits?author=JonathanStoye "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/4126644?v=4" width="100px;"/><br /><sub><b>Sanghyeon Lee</b></sub>](https://github.com/yongdamsh)<br />[💡](#example-yongdamsh "Examples") | [<img src="https://avatars3.githubusercontent.com/u/8015514?v=4" width="100px;"/><br /><sub><b>Justice Mba </b></sub>](https://github.com/Dajust)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=Dajust "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=Dajust "Documentation") [🤔](#ideas-Dajust "Ideas, Planning, & Feedback") | [<img src="https://avatars3.githubusercontent.com/u/340761?v=4" width="100px;"/><br /><sub><b>Wayne Crouch</b></sub>](https://github.com/wgcrouch)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=wgcrouch "Code") | [<img src="https://avatars1.githubusercontent.com/u/4996462?v=4" width="100px;"/><br /><sub><b>Ben Elliott</b></sub>](http://benjaminelliott.co.uk)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=benelliott "Code") | [<img src="https://avatars3.githubusercontent.com/u/577921?v=4" width="100px;"/><br /><sub><b>Ruben Costa</b></sub>](http://nuances.co)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=rubencosta "Code") |
876+
| [<img src="https://avatars2.githubusercontent.com/u/21689428?v=4" width="100px;"/><br /><sub><b>Jonathan Stoye</b></sub>](http://jonathanstoye.de)<br />[📖](https://github.com/kentcdodds/dom-testing-library/commits?author=JonathanStoye "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/4126644?v=4" width="100px;"/><br /><sub><b>Sanghyeon Lee</b></sub>](https://github.com/yongdamsh)<br />[💡](#example-yongdamsh "Examples") | [<img src="https://avatars3.githubusercontent.com/u/8015514?v=4" width="100px;"/><br /><sub><b>Justice Mba </b></sub>](https://github.com/Dajust)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=Dajust "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=Dajust "Documentation") [🤔](#ideas-Dajust "Ideas, Planning, & Feedback") | [<img src="https://avatars3.githubusercontent.com/u/340761?v=4" width="100px;"/><br /><sub><b>Wayne Crouch</b></sub>](https://github.com/wgcrouch)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=wgcrouch "Code") | [<img src="https://avatars1.githubusercontent.com/u/4996462?v=4" width="100px;"/><br /><sub><b>Ben Elliott</b></sub>](http://benjaminelliott.co.uk)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=benelliott "Code") | [<img src="https://avatars3.githubusercontent.com/u/577921?v=4" width="100px;"/><br /><sub><b>Ruben Costa</b></sub>](http://nuances.co)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=rubencosta "Code") | [<img src="https://avatars2.githubusercontent.com/u/4982001?v=4" width="100px;"/><br /><sub><b>Robert Smith</b></sub>](http://rbrtsmith.com/)<br />[🐛](https://github.com/kentcdodds/dom-testing-library/issues?q=author%3Arbrtsmith "Bug reports") [🤔](#ideas-rbrtsmith "Ideas, Planning, & Feedback") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=rbrtsmith "Documentation") |
878877

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

src/__tests__/__snapshots__/wait-for-element.js.snap

+16
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,22 @@ exports[`it throws the same error that the callback has thrown if timeout is exc
3838
/>
3939
`;
4040

41+
exports[`it waits characterData mutation 1`] = `
42+
<div>
43+
<div>
44+
initial text
45+
</div>
46+
</div>
47+
`;
48+
49+
exports[`it waits characterData mutation 2`] = `
50+
<div>
51+
<div>
52+
new text
53+
</div>
54+
</div>
55+
`;
56+
4157
exports[`it waits for the callback to return a value and only reacts to DOM mutations 1`] = `
4258
<div>
4359
<div

src/__tests__/wait-for-element.js

+38-2
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,44 @@ test('it waits for the next DOM mutation with default callback', async () => {
129129
return promise
130130
})
131131

132-
test('it waits for the attributes mutation if configured', async () => {
132+
test('it waits characterData mutation', async () => {
133+
const {container} = render(`<div>initial text</div>`)
134+
135+
const callback = jest
136+
.fn(() => container.textContent === 'new text')
137+
.mockName('callback')
138+
const successHandler = jest.fn().mockName('successHandler')
139+
const errorHandler = jest.fn().mockName('errorHandler')
140+
141+
const promise = waitForElement(callback, {container}).then(
142+
successHandler,
143+
errorHandler,
144+
)
145+
146+
// Promise callbacks are always asynchronous.
147+
expect(successHandler).toHaveBeenCalledTimes(0)
148+
expect(errorHandler).toHaveBeenCalledTimes(0)
149+
expect(callback).toHaveBeenCalledTimes(1)
150+
expect(container).toMatchSnapshot()
151+
152+
await skipSomeTimeForMutationObserver()
153+
154+
expect(callback).toHaveBeenCalledTimes(1)
155+
expect(successHandler).toHaveBeenCalledTimes(0)
156+
expect(errorHandler).toHaveBeenCalledTimes(0)
157+
158+
container.querySelector('div').innerHTML = 'new text'
159+
await skipSomeTimeForMutationObserver()
160+
161+
expect(successHandler).toHaveBeenCalledTimes(1)
162+
expect(errorHandler).toHaveBeenCalledTimes(0)
163+
expect(callback).toHaveBeenCalledTimes(2)
164+
expect(container).toMatchSnapshot()
165+
166+
return promise
167+
})
168+
169+
test('it waits for the attributes mutation', async () => {
133170
const {container} = render(``)
134171

135172
const callback = jest
@@ -140,7 +177,6 @@ test('it waits for the attributes mutation if configured', async () => {
140177

141178
const promise = waitForElement(callback, {
142179
container,
143-
mutationObserverOptions: {attributes: true},
144180
}).then(successHandler, errorHandler)
145181

146182
expect(callback).toHaveBeenCalledTimes(1)

src/wait-for-element.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ function waitForElement(
55
{
66
container = document,
77
timeout = 4500,
8-
mutationObserverOptions = {subtree: true, childList: true},
8+
mutationObserverOptions = {
9+
subtree: true,
10+
childList: true,
11+
attributes: true,
12+
characterData: true,
13+
},
914
} = {},
1015
) {
1116
return new Promise((resolve, reject) => {

0 commit comments

Comments
 (0)