Skip to content
This repository was archived by the owner on Mar 5, 2022. It is now read-only.

Commit 54857cd

Browse files
authored
add example re-rendering component after changing props (#426)
1 parent e8cfb50 commit 54857cd

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ Spec | Description
142142
[css modules](cypress/component/basic/css-modules) | Shows that component that using css modules styles works
143143
[network](cypress/component/basic/network) | Confirms we can use `cy.route` to stub / spy on component's network calls
144144
[no-visit](cypress/component/basic/no-visit) | Component specs cannot call `cy.visit`
145+
[re-render](cypress/component/basic/re-render) | Checking how the component re-renders when its props change
145146
[react-book-by-chris-noring](cypress/component/basic/react-book-by-chris-noring) | Copied test examples from [React Book](https://softchris.github.io/books/react) and adapted for Cypress component tests
146147
[react-tutorial](cypress/component/basic/react-tutorial) | Tests from official [ReactJS tutorial](https://reactjs.org/tutorial/tutorial.html) copied and adapted for Cypress component tests
147148
[stub-example](cypress/component/basic/stub-example) | Uses `cy.stub` as component props
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# re-render
2+
3+
If you want to see how a component re-renders in reaction to changed props, you have to make a mini-web app around it. See [spec.js](spec.js) but in essence you never keep the component, but create a new one.
4+
5+
![Spec](images/my-input.gif)
Loading
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import React from 'react'
2+
import { mount } from 'cypress-react-unit-test'
3+
4+
const MyInput = ({ inputVal, onInputChanged }) => {
5+
console.log('MyInput "%s"', inputVal)
6+
return (
7+
<>
8+
<input
9+
type="text"
10+
value={inputVal}
11+
onChange={e => onInputChanged(e.target.value)}
12+
/>
13+
<p>You entered {inputVal} </p>
14+
</>
15+
)
16+
}
17+
18+
describe('My Input', () => {
19+
it('updates when props change', () => {
20+
// can we shorten this example
21+
// that checks if the MyInput is re-rendering?
22+
const App = () => {
23+
const [message, setMessage] = React.useState('')
24+
return (
25+
<>
26+
<MyInput
27+
inputVal={message}
28+
onInputChanged={newValue => {
29+
setMessage(newValue)
30+
return null
31+
}}
32+
/>
33+
</>
34+
)
35+
}
36+
mount(<App />)
37+
38+
/* Update props */
39+
cy.get('input').type('hello there!')
40+
cy.contains('You entered hello there!')
41+
})
42+
})

0 commit comments

Comments
 (0)