Skip to content

Commit 4410b4c

Browse files
dfcookKent C. Dodds
authored and
Kent C. Dodds
committed
feat: add bindElementToQueries utility function for libraries using this API (#18)
* Add bindQueriesToElement utility function for libraries using this API * Add documentation and update contributors * Rename to bindElementToQueries as this seems to be more appropriate Closes #17
1 parent f2683d9 commit 4410b4c

File tree

5 files changed

+34
-8
lines changed

5 files changed

+34
-8
lines changed

.all-contributorsrc

+11
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,17 @@
160160
"doc",
161161
"example"
162162
]
163+
},
164+
{
165+
"login": "dfcook",
166+
"name": "Daniel Cook",
167+
"avatar_url": "https://avatars3.githubusercontent.com/u/10348212?v=4",
168+
"profile": "https://github.com/dfcook",
169+
"contributions": [
170+
"code",
171+
"doc,
172+
"test"
173+
]
163174
}
164175
]
165176
}

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ when a real user uses it.
8383
* [Using other assertion libraries](#using-other-assertion-libraries)
8484
* [`TextMatch`](#textmatch)
8585
* [`query` APIs](#query-apis)
86+
* [`bindElementToQueries`](#bindElementToQueries)
8687
* [Debugging](#debugging)
8788
* [Implementations](#implementations)
8889
* [FAQ](#faq)
@@ -468,6 +469,12 @@ expect(submitButton).toBeNull() // it doesn't exist
468469
expect(submitButton).not.toBeInTheDOM()
469470
```
470471

472+
## `bindElementToQueries`
473+
474+
`bindElementToQueries` takes a DOM element and binds it to the raw query functions, allowing them
475+
to be used without specifying a container. It is the recommended approach for libraries built on this API
476+
and is in use in `react-testing-library` and `vue-testing-library`.
477+
471478
## Debugging
472479

473480
When you use any `get` calls in your test cases, the current state of the `container`

src/__tests__/helpers/test-utils.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
1-
import * as queries from '../../queries'
1+
import {bindElementToQueries} from '../../bind-element-to-queries'
22

33
function render(html) {
44
const container = document.createElement('div')
55
container.innerHTML = html
6-
const containerQueries = Object.entries(queries).reduce(
7-
(helpers, [key, fn]) => {
8-
helpers[key] = fn.bind(null, container)
9-
return helpers
10-
},
11-
{},
12-
)
6+
const containerQueries = bindElementToQueries(container)
137
return {container, ...containerQueries}
148
}
159

src/bind-element-to-queries.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as queries from './queries'
2+
3+
function bindElementToQueries(element) {
4+
return Object.entries(queries).reduce(
5+
(helpers, [key, fn]) => {
6+
helpers[key] = fn.bind(null, element)
7+
return helpers
8+
},
9+
{},
10+
)
11+
}
12+
13+
export {bindElementToQueries}

src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ export * from './wait'
99
export * from './wait-for-element'
1010
export * from './matches'
1111
export * from './events'
12+
export * from './bind-element-to-queries'

0 commit comments

Comments
 (0)