Skip to content

Commit 856864a

Browse files
authored
add example using cypress-react-selector (#197)
1 parent 41eefdd commit 856864a

File tree

5 files changed

+63
-4
lines changed

5 files changed

+63
-4
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ Spec | Description
144144
[tutorial](cypress/component/advanced/tutorial) | A few tests adopted from [ReactJS Tutorial](https://reactjs.org/tutorial/tutorial.html)
145145
[portal](cypress/component/advanced/portal) | Component test for `ReactDOM.createPortal` feature
146146
[react-bootstrap](cypress/component/advanced/react-bootstrap) | Confirms [react-bootstrap](https://react-bootstrap.github.io/) components are working
147+
[select React component](cypress/component/advanced/react-book-example/src/components/ProductsList.spec.js) | Uses [cypress-react-selector](https://github.com/abhinaba-ghosh/cypress-react-selector) to find DOM elements using React component name and state values
147148
<!-- prettier-ignore-end -->
148149

149150
### Full examples

cypress/component/advanced/react-book-example/src/components/ProductsList.jsx

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
import React from 'react'
22
import { getProducts } from '../products'
33

4+
class AProduct extends React.Component {
5+
constructor(props) {
6+
super(props)
7+
this.state = {
8+
name: props.name,
9+
}
10+
}
11+
12+
render() {
13+
return <div className="product">{this.state.name}</div>
14+
}
15+
}
16+
417
const Products = ({ products }) => (
518
<React.Fragment>
619
{products.map(product => (
7-
<div>{product.name}</div>
20+
<AProduct key={product.id} name={product.name} />
821
))}
922
</React.Fragment>
1023
)
@@ -25,7 +38,11 @@ class ProductsContainer extends React.Component {
2538
}
2639

2740
render() {
28-
return <Products products={this.state.products} />
41+
return (
42+
<div className="product-container">
43+
<Products products={this.state.products} />
44+
</div>
45+
)
2946
}
3047
}
3148

cypress/component/advanced/react-book-example/src/components/ProductsList.spec.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/// <reference types="cypress" />
2+
import 'cypress-react-selector'
23
import { mount } from 'cypress-react-unit-test'
34
import React from 'react'
45
import ProductsList from './ProductsList.jsx'
@@ -10,9 +11,22 @@ it('renders without crashing', () => {
1011
.withArgs('http://myapi.com/products')
1112
.resolves({
1213
json: cy.stub().resolves({
13-
products: [{ id: 1, name: 'Mocked data' }],
14+
products: [
15+
{ id: 1, name: 'First item' },
16+
{ id: 2, name: 'Second item' },
17+
],
1418
}),
1519
})
1620
mount(<ProductsList />)
17-
cy.contains('Mocked data').should('be.visible')
21+
cy.contains('First item').should('be.visible')
22+
cy.get('.product').should('have.length', 2)
23+
24+
// use https://github.com/abhinaba-ghosh/cypress-react-selector
25+
// to find DOM elements by React component constructor name or state
26+
cy.waitForReact(1000, '#cypress-root')
27+
cy.react('ProductsContainer').should('have.class', 'product-container')
28+
cy.react('AProduct').should('have.length', 2)
29+
cy.react('AProduct', { name: 'Second item' })
30+
.should('be.visible')
31+
.and('have.text', 'Second item')
1832
})

package-lock.json

+26
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
"css-loader": "2.1.1",
7171
"cypress": "4.5.0",
7272
"cypress-plugin-snapshots": "1.4.3",
73+
"cypress-react-selector": "0.1.2",
7374
"date-fns": "2.13.0",
7475
"husky": "3.1.0",
7576
"lint-staged": "9.5.0",

0 commit comments

Comments
 (0)