Skip to content

Commit d105706

Browse files
npeterkampsKent C. Dodds
authored and
Kent C. Dodds
committed
feat(within): Support cy.within (#11)
* feat(within): Support cy.within. Also support passing in a container within the options object. * Installed wait-port and use it within test:cypress:run to avoid race-conditions * Improvements based on feedback. * Documented jQuery vs DOM nodes. * Use Cypress.cy global. * Renamed function and parameter. * Moved utils file. * Use the cy global.
1 parent 7d14b89 commit d105706

File tree

8 files changed

+64
-12
lines changed

8 files changed

+64
-12
lines changed

.all-contributorsrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
"contributions": [
4848
"code",
4949
"doc",
50-
"ideas"
50+
"ideas",
51+
"test"
5152
]
5253
}
5354
],

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,16 @@ cy.getAllByText('Jackie Chan').click()
7474
cy.queryByText('Button Text').should('exist')
7575
cy.queryByText('Non-existing Button Text').should('not.exist')
7676
cy.queryByLabelText('Label text', { timeout: 7000 }).should('exist')
77+
cy.get('form').within(() => {
78+
cy.getByText('Button Text').should('exist')
79+
})
80+
cy.get('form').then((subject) => {
81+
cy.getByText('Button Text', { container: subject }).should('exist')
82+
})
7783
```
7884

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+
7987
## Other Solutions
8088

8189
I'm not aware of any, if you are please [make a pull request][prs] and add it
@@ -87,7 +95,7 @@ Thanks goes to these people ([emoji key][emojis]):
8795

8896
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
8997
<!-- prettier-ignore -->
90-
| [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub><b>Kent C. Dodds</b></sub>](https://kentcdodds.com)<br />[💻](https://github.com/kentcdodds/cypress-testing-library/commits?author=kentcdodds "Code") [📖](https://github.com/kentcdodds/cypress-testing-library/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/kentcdodds/cypress-testing-library/commits?author=kentcdodds "Tests") | [<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/cypress-testing-library/commits?author=sompylasar "Code") [🤔](#ideas-sompylasar "Ideas, Planning, & Feedback") | [<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/cypress-testing-library/commits?author=lgandecki "Code") [⚠️](https://github.com/kentcdodds/cypress-testing-library/commits?author=lgandecki "Tests") | [<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/cypress-testing-library/commits?author=npeterkamps "Code") [📖](https://github.com/kentcdodds/cypress-testing-library/commits?author=npeterkamps "Documentation") [🤔](#ideas-npeterkamps "Ideas, Planning, & Feedback") |
98+
| [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub><b>Kent C. Dodds</b></sub>](https://kentcdodds.com)<br />[💻](https://github.com/kentcdodds/cypress-testing-library/commits?author=kentcdodds "Code") [📖](https://github.com/kentcdodds/cypress-testing-library/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/kentcdodds/cypress-testing-library/commits?author=kentcdodds "Tests") | [<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/cypress-testing-library/commits?author=sompylasar "Code") [🤔](#ideas-sompylasar "Ideas, Planning, & Feedback") | [<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/cypress-testing-library/commits?author=lgandecki "Code") [⚠️](https://github.com/kentcdodds/cypress-testing-library/commits?author=lgandecki "Tests") | [<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/cypress-testing-library/commits?author=npeterkamps "Code") [📖](https://github.com/kentcdodds/cypress-testing-library/commits?author=npeterkamps "Documentation") [🤔](#ideas-npeterkamps "Ideas, Planning, & Feedback") [⚠️](https://github.com/kentcdodds/cypress-testing-library/commits?author=npeterkamps "Tests") |
9199
| :---: | :---: | :---: | :---: |
92100
<!-- ALL-CONTRIBUTORS-LIST:END -->
93101

cypress/fixtures/test-app/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ <h2>getByPlaceholderText</h2>
2929
<section>
3030
<h2>getByText</h2>
3131
<button onclick="this.innerText = 'Button Clicked'">Button Text</button>
32+
<div id="nested">
33+
<h3>getByText within</h3>
34+
<button onclick="this.innerText = 'Button Clicked'">Button Text</button>
35+
</div>
3236
</section>
3337
<section>
3438
<h2>getByLabelText</h2>

cypress/integration/commands.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,20 @@ describe('dom-testing-library commands', () => {
3636
cy.queryByText('Button Text').should('exist')
3737
cy.queryByText('Non-existing Button Text').should('not.exist')
3838
})
39+
40+
it('getByText within', () => {
41+
cy.get('#nested')
42+
.within(() => {
43+
cy.getByText('Button Text').click()
44+
})
45+
})
46+
47+
it('getByText in container', () => {
48+
cy.get('#nested')
49+
.then((subject) => {
50+
cy.getByText('Button Text', { container: subject }).click()
51+
})
52+
})
3953
})
4054

4155
/* global cy */

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
"test": "npm-run-all --parallel test:unit test:cypress",
1414
"test:unit": "kcd-scripts test --no-watch",
1515
"test:unit:watch": "kcd-scripts test",
16-
"test:cypress:serve": "serve -l 13370 ./cypress/fixtures/test-app",
17-
"test:cypress:run": "cypress run",
16+
"test:cypress:serve": "serve --listen 13370 ./cypress/fixtures/test-app",
17+
"test:cypress:run": "wait-port --timeout 10000 localhost:13370 && cypress run",
1818
"test:cypress:open": "cypress open",
1919
"test:cypress": "npm-run-all --silent --parallel --race test:cypress:serve test:cypress:run",
2020
"test:cypress:dev": "npm-run-all --silent --parallel --race test:cypress:serve test:cypress:open",
@@ -45,7 +45,8 @@
4545
"cypress": "^3.0.1",
4646
"kcd-scripts": "^0.37.0",
4747
"npm-run-all": "^4.1.2",
48-
"serve": "^7.2.0"
48+
"serve": "^7.2.0",
49+
"wait-port": "^0.2.2"
4950
},
5051
"peerDependencies": {
5152
"cypress": "^2.1.0 || ^3.0.0"

src/add-commands.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {commands} from './'
22

33
commands.forEach(({name, command}) => {
4-
Cypress.Commands.add(name, command.bind(null, cy))
4+
Cypress.Commands.add(name, command)
55
})
66

7-
/* global Cypress, cy */
7+
/* global Cypress */

src/index.js

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

34
const defaults = {
45
timeout: 3000,
@@ -7,17 +8,19 @@ const defaults = {
78
const commands = Object.keys(queries).map(queryName => {
89
return {
910
name: queryName,
10-
command: (cy, ...args) => {
11+
command: (...args) => {
1112
const lastArg = args[args.length - 1]
1213
const waitOptions = (typeof lastArg === 'object')
1314
? Object.assign({}, defaults, lastArg)
1415
: defaults
1516

1617
const queryImpl = queries[queryName]
17-
const baseCommandImpl = doc =>
18-
waitForElement(() => queryImpl(doc, ...args), Object.assign({}, waitOptions, {
19-
container: doc,
18+
const baseCommandImpl = doc => {
19+
const container = getContainer(waitOptions.container || doc);
20+
return waitForElement(() => queryImpl(container, ...args), Object.assign({}, waitOptions, {
21+
container,
2022
}))
23+
}
2124
let commandImpl
2225
if (
2326
queryName.startsWith('queryBy') ||
@@ -64,4 +67,4 @@ const commands = Object.keys(queries).map(queryName => {
6467
export {commands}
6568

6669
/* eslint no-new-func:0 */
67-
/* globals Cypress */
70+
/* globals Cypress, cy */

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 = 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, cy */

0 commit comments

Comments
 (0)