Skip to content

Commit 6394e7e

Browse files
committed
Improvements based on feedback.
* Documented jQuery vs DOM nodes. * Use Cypress.cy global. * Renamed function and parameter. * Moved utils file.
1 parent 72510dc commit 6394e7e

File tree

4 files changed

+25
-23
lines changed

4 files changed

+25
-23
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ cy.get('form').then((subject) => {
8282
})
8383
```
8484

85+
`cypress-testing-library` supports both jQuery elements and DOM nodes. This is necessary because Cypress uses jQuery elements, while `dom-testing-library` expects DOM nodes. When you pass a jQuery element as `container`, it will get the first DOM node from the collection and use that as the `container` parameter for the `dom-testing-library` functions.
86+
8587
## Other Solutions
8688

8789
I'm not aware of any, if you are please [make a pull request][prs] and add it

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {queries, waitForElement} from 'dom-testing-library'
2-
import {getContainer} from './support/utils'
2+
import {getContainer} from './utils'
33

44
const defaults = {
55
timeout: 3000,
@@ -16,7 +16,7 @@ const commands = Object.keys(queries).map(queryName => {
1616

1717
const queryImpl = queries[queryName]
1818
const baseCommandImpl = doc => {
19-
const container = getContainer(cy, waitOptions.container || doc);
19+
const container = getContainer(waitOptions.container || doc);
2020
return waitForElement(() => queryImpl(container, ...args), Object.assign({}, waitOptions, {
2121
container,
2222
}))

src/support/utils.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/utils.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function getFirstElement(jqueryOrElement) {
2+
if (Cypress.dom.isJquery(jqueryOrElement)) {
3+
return jqueryOrElement.get(0)
4+
}
5+
return jqueryOrElement
6+
}
7+
8+
function getContainer(container) {
9+
const withinContainer = Cypress.cy.state('withinSubject')
10+
if (withinContainer) {
11+
return getFirstElement(withinContainer)
12+
}
13+
return getFirstElement(container)
14+
}
15+
16+
export {
17+
getFirstElement,
18+
getContainer,
19+
}
20+
21+
/* globals Cypress */

0 commit comments

Comments
 (0)