Skip to content

Commit 079bffb

Browse files
author
Alex
committed
Implements changes requested on 10-9-18. Removes matchSnapshots. Correctly formats README.md. Changes Promise to return a mutationsList. Adds documentation for mutationsList.
1 parent ba4a70e commit 079bffb

File tree

5 files changed

+96
-108
lines changed

5 files changed

+96
-108
lines changed

.all-contributorsrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@
344344
"profile": "http://www.wendtedesigns.com/",
345345
"contributions": [
346346
"code",
347+
"doc",
347348
"test"
348349
]
349350
}

README.md

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ when a real user uses it.
6969
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
7070
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
7171

72-
7372
- [Installation](#installation)
7473
- [Usage](#usage)
7574
- [`getByLabelText`](#getbylabeltext)
@@ -85,8 +84,7 @@ when a real user uses it.
8584
- [`waitForElement`](#waitforelement)
8685
- [`waitForDomChange`](#waitfordomchange)
8786
- [`fireEvent`](#fireevent)
88-
- [`fireEvent[eventName]`](#fireeventeventname)
89-
- [`getNodeText`](#getnodetext)
87+
- [`getNodeText`](#getnodetext)
9088
- [Custom Jest Matchers](#custom-jest-matchers)
9189
- [Using other assertion libraries](#using-other-assertion-libraries)
9290
- [`TextMatch`](#textmatch)
@@ -502,7 +500,6 @@ function waitForElement<T>(
502500
When in need to wait for DOM elements to appear, disappear, or change you can use `waitForElement`.
503501
The `waitForElement` function is a small wrapper around the [`MutationObserver`](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver).
504502

505-
506503
Here's a simple example:
507504

508505
```javascript
@@ -548,20 +545,18 @@ additions and removals of child elements (including text nodes) in the `containe
548545
### `waitForDomChange`
549546

550547
```typescript
551-
function waitForDomChange<T>(
552-
options?: {
553-
container?: HTMLElement
554-
timeout?: number
555-
mutationObserverOptions?: MutationObserverInit
556-
},
557-
): Promise<T>
548+
function waitForDomChange<T>(options?: {
549+
container?: HTMLElement
550+
timeout?: number
551+
mutationObserverOptions?: MutationObserverInit
552+
}): Promise<T>
558553
```
559554

560-
When in need to wait for DOM to change you can use `waitForDomChange`.
561-
The `waitForDomChange` function is a small wrapper
562-
around the
555+
When in need to wait for the DOM to change you can use `waitForDomChange`. The `waitForDomChange`
556+
function is a small wrapper around the
563557
[`MutationObserver`](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver).
564-
Here is an example where the Promise will be resolved because the container is changed
558+
559+
Here is an example where the Promise will be resolved because the container is changed:
565560

566561
```javascript
567562
const container = document.createElement('div')
@@ -573,6 +568,27 @@ container.append(document.createElement('p'))
573568
// waitForDomChange would throw an error
574569
```
575570

571+
The promise will resolve with a [`mutationsList`](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver/MutationObserver) which you can use to determine what kind of a change (or changes) affected the container
572+
573+
```javascript
574+
const container = document.createElement('div')
575+
container.setAttribute('data-cool', 'true')
576+
waitForDomChange({container}).then(mutationsList => {
577+
const mutation = mutationsList[0]
578+
console.log(
579+
`was cool: ${mutation.oldValue}\ncurrently cool: ${
580+
mutation.target.dataset.cool
581+
}`,
582+
)
583+
})
584+
container.setAttribute('data-cool', 'false')
585+
/*
586+
logs:
587+
was cool: true
588+
currently cool: false
589+
*/
590+
```
591+
576592
### `fireEvent`
577593

578594
```typescript
@@ -629,7 +645,7 @@ fireEvent.change(getByLabelText(/picture/i), {
629645
})
630646
```
631647

632-
#### `getNodeText`
648+
### `getNodeText`
633649

634650
```typescript
635651
getNodeText(node: HTMLElement)
@@ -1026,7 +1042,8 @@ Thanks goes to these people ([emoji key][emojis]):
10261042
| [<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") |
10271043
| [<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") |
10281044
| [<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") |
1029-
| [<img src="https://avatars3.githubusercontent.com/u/881986?v=4" width="100px;"/><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;"/><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;"/><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;"/><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;"/><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;"/><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 "Tests") |
1045+
| [<img src="https://avatars3.githubusercontent.com/u/881986?v=4" width="100px;"/><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;"/><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;"/><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;"/><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;"/><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;"/><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") |
1046+
10301047
<!-- ALL-CONTRIBUTORS-LIST:END -->
10311048

10321049
This project follows the [all-contributors][all-contributors] specification.
Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,43 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`it throws if timeout is exceeded 1`] = `
3+
exports[`it waits for the attributes mutation 1`] = `
44
Array [
5-
[Error: Timed out in waitForDomChange.],
5+
Array [
6+
Object {
7+
"addedNodes": Array [],
8+
"attributeName": "data-test-attribute",
9+
"attributeNamespace": null,
10+
"nextSibling": null,
11+
"oldValue": null,
12+
"previousSibling": null,
13+
"removedNodes": Array [],
14+
"target": <div
15+
data-test-attribute="PASSED"
16+
/>,
17+
"type": "attributes",
18+
},
19+
],
620
]
721
`;
822

9-
exports[`it throws if timeout is exceeded 2`] = `<div />`;
10-
11-
exports[`it waits characterData mutation 1`] = `
12-
<div>
13-
<div>
14-
initial text
15-
</div>
16-
</div>
17-
`;
18-
19-
exports[`it waits characterData mutation 2`] = `
20-
<div>
21-
<div>
22-
new text
23-
</div>
24-
</div>
25-
`;
26-
2723
exports[`it waits for the next DOM mutation 1`] = `
28-
<body>
29-
<div />
30-
</body>
31-
`;
32-
33-
exports[`it waits for the next DOM mutation 2`] = `
34-
<body>
35-
<div />
36-
</body>
24+
Array [
25+
Array [
26+
Object {
27+
"addedNodes": Array [
28+
<div />,
29+
],
30+
"attributeName": null,
31+
"attributeNamespace": null,
32+
"nextSibling": null,
33+
"oldValue": null,
34+
"previousSibling": null,
35+
"removedNodes": Array [],
36+
"target": <body>
37+
<div />
38+
</body>,
39+
"type": "childList",
40+
},
41+
],
42+
]
3743
`;

0 commit comments

Comments
 (0)