Skip to content

Commit 04c5850

Browse files
lgandeckiKent C. Dodds
authored and
Kent C. Dodds
committed
feat: support queryBy* queryAllBy* (#5)
* initial work for enabling queries * update docs, contributors * fixed typo
1 parent adf3d83 commit 04c5850

File tree

5 files changed

+76
-34
lines changed

5 files changed

+76
-34
lines changed

.all-contributorsrc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@
2828
"code",
2929
"ideas"
3030
]
31+
},
32+
{
33+
"login": "lgandecki",
34+
"name": "Łukasz Gandecki",
35+
"avatar_url": "https://avatars1.githubusercontent.com/u/4002543?v=4",
36+
"profile": "http://team.thebrain.pro",
37+
"contributions": [
38+
"code",
39+
"test"
40+
]
3141
}
3242
],
3343
"repoType": "github"

README.md

Lines changed: 12 additions & 5 deletions
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-2-orange.svg?style=flat-square)](#contributors)
19+
[![All Contributors](https://img.shields.io/badge/all_contributors-3-orange.svg?style=flat-square)](#contributors)
2020
[![PRs Welcome][prs-badge]][prs]
2121
[![Code of Conduct][coc-badge]][coc]
2222

@@ -42,7 +42,6 @@ This allows you to use all the useful [`dom-testing-library`][dom-testing-librar
4242

4343
* [Installation](#installation)
4444
* [Usage](#usage)
45-
* [Inspiration](#inspiration)
4645
* [Other Solutions](#other-solutions)
4746
* [Contributors](#contributors)
4847
* [LICENSE](#license)
@@ -68,7 +67,15 @@ Add this line to your project's `cypress/support/commands.js`:
6867
import 'cypress-testing-library/add-commands';
6968
```
7069

71-
You can now use all of `dom-testing-library`'s `getBy` and `getAllBy` commands. [See `dom-testing-library` repo for reference](https://github.com/kentcdodds/dom-testing-library#usage)
70+
You can now use all of `dom-testing-library`'s `getBy`, `getAllBy`, `queryBy` and `queryAllBy` commands. [See `dom-testing-library` repo for reference](https://github.com/kentcdodds/dom-testing-library#usage)
71+
72+
To show some simple examples (from [cypress/integration/commands.spec.js](cypress/integration/commands.spec.js)):
73+
74+
```javascript
75+
cy.getAllByText('Jackie Chan').click()
76+
cy.queryByText('Button Text').should('exist')
77+
cy.queryByText('Non-existing Button Text').should('not.exist')
78+
```
7279

7380
## Other Solutions
7481

@@ -82,8 +89,8 @@ Thanks goes to these people ([emoji key][emojis]):
8289
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
8390

8491
<!-- prettier-ignore -->
85-
| [<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") |
86-
| :---: | :---: |
92+
| [<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") |
93+
| :---: | :---: | :---: |
8794

8895
<!-- ALL-CONTRIBUTORS-LIST:END -->
8996

cypress/integration/commands.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ describe('dom-testing-library commands', () => {
3131
it('getAllByText', () => {
3232
cy.getAllByText('Jackie Chan').click({multiple: true})
3333
})
34+
35+
it('queryByText', () => {
36+
cy.queryByText('Button Text').should('exist')
37+
cy.queryByText('Non-existing Button Text').should('not.exist')
38+
})
3439
})
3540

3641
/* global cy */

src/__tests__/__snapshots__/commands.js.snap

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,24 @@
22

33
exports[`exports expected commands 1`] = `
44
Array [
5+
"queryByPlaceholderText",
6+
"queryAllByPlaceholderText",
57
"getByPlaceholderText",
68
"getAllByPlaceholderText",
9+
"queryByText",
10+
"queryAllByText",
711
"getByText",
812
"getAllByText",
13+
"queryByLabelText",
14+
"queryAllByLabelText",
915
"getByLabelText",
1016
"getAllByLabelText",
17+
"queryByAltText",
18+
"queryAllByAltText",
1119
"getByAltText",
1220
"getAllByAltText",
21+
"queryByTestId",
22+
"queryAllByTestId",
1323
"getByTestId",
1424
"getAllByTestId",
1525
]

src/index.js

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,49 @@
11
import {queries, waitForElement} from 'dom-testing-library'
22

3-
const commands = Object.keys(queries)
4-
.filter(
5-
queryName =>
6-
queryName.startsWith('getBy') || queryName.startsWith('getAllBy'),
7-
)
8-
.map(queryName => {
9-
return {
10-
name: queryName,
11-
command: (cy, ...args) => {
12-
const queryImpl = queries[queryName]
13-
const commandImpl = doc =>
14-
waitForElement(() => queryImpl(doc, ...args), {container: doc})
15-
const thenHandler = new Function(
16-
'commandImpl',
17-
`
3+
const commands = Object.keys(queries).map(queryName => {
4+
return {
5+
name: queryName,
6+
command: (cy, ...args) => {
7+
const queryImpl = queries[queryName]
8+
const baseCommandImpl = doc =>
9+
waitForElement(() => queryImpl(doc, ...args), {
10+
container: doc,
11+
timeout: 3000,
12+
})
13+
let commandImpl
14+
if (
15+
queryName.startsWith('queryBy') ||
16+
queryName.startsWith('queryAllBy')
17+
) {
18+
commandImpl = doc =>
19+
baseCommandImpl(doc).catch(_ =>
20+
doc.querySelector('.___cypressNotExistingSelector'),
21+
)
22+
} else {
23+
commandImpl = doc => baseCommandImpl(doc)
24+
}
25+
const thenHandler = new Function(
26+
'commandImpl',
27+
`
1828
return function Command__${queryName}(thenArgs) {
1929
return commandImpl(thenArgs.document)
2030
}
2131
`,
22-
)(commandImpl)
23-
return cy
24-
.window({log: false})
25-
.then(thenHandler)
26-
.then(subject => {
27-
Cypress.log({
28-
$el: subject,
29-
name: queryName,
30-
message: args,
31-
})
32-
return subject
32+
)(commandImpl)
33+
return cy
34+
.window({log: false})
35+
.then(thenHandler)
36+
.then(subject => {
37+
Cypress.log({
38+
$el: subject,
39+
name: queryName,
40+
message: args,
3341
})
34-
},
35-
}
36-
})
42+
return subject
43+
})
44+
},
45+
}
46+
})
3747

3848
export {commands}
3949

0 commit comments

Comments
 (0)