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

Commit 62ae45e

Browse files
committed
feat: add cy.render method, close #122
1 parent 21640d3 commit 62ae45e

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,20 @@ cy.mount(<Button name='Orange' orange />, null, { cssFile })
113113

114114
See [cypress/integration/inject-style-spec.js](cypress/integration/inject-style-spec.js) for more examples.
115115

116+
### re-render
117+
118+
Sometimes you need to re-render the component, for example when a Redux store changes. After `cy.mount` completes (asynchronously), you can call `cy.render(JSXElement)` method.
119+
120+
```js
121+
cy.mount(<El text='first' />)
122+
cy.contains('first')
123+
.then(() => {
124+
cy.render(<El text='second' />)
125+
})
126+
```
127+
128+
See [bahmutov/redux-counter-example](https://github.com/bahmutov/redux-counter-example) example spec file [src/App.cy-spec.js](https://github.com/bahmutov/redux-counter-example/blob/master/src/App.cy-spec.js).
129+
116130
## Configuration
117131

118132
If your React and React DOM libraries are installed in non-standard paths (think monorepo scenario), you can tell this plugin where to find them. In `cypress.json` specify paths like this:

cypress/integration/rerender-spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// <reference types="cypress" />
2+
/// <reference types="../../lib" />
3+
import React from 'react'
4+
5+
/* eslint-env mocha */
6+
describe('re-render', () => {
7+
it('works with El', () => {
8+
const El = ({ text }) => <div>{text}</div>
9+
cy.mount(<El text='first' />)
10+
cy.contains('first')
11+
.then(() => {
12+
cy.render(<El text='second' />)
13+
})
14+
cy.contains('second')
15+
})
16+
})

lib/index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,10 @@ declare namespace Cypress {
7676
**/
7777
mount: (component: JSXElement, alias?: string, options?: Partial<MountOptions>) => Chainable<void>
7878
get<S = any>(alias: string | symbol | Function, options?: Partial<Loggable & Timeoutable>): Chainable<any>
79+
/**
80+
* Utility method to re-render a new / updated component
81+
* Should only be called AFTER `cy.mount`
82+
*/
83+
render: (jsx: JSXElement) => void
7984
}
8085
}

lib/index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,14 @@ export const mount = (jsx: JSXElement, alias?: string, options?: Partial<MountOp
155155
.then(win => {
156156
const { ReactDOM } = win as Window & {ReactDOM: any}
157157
const document = cy.state('document')
158-
const component = ReactDOM.render(
159-
jsx,
160-
document.getElementById('cypress-jsdom')
161-
)
158+
const target: Element = document.getElementById('cypress-jsdom')
159+
160+
const render = (currentJsx: JSXElement) => ReactDOM.render(currentJsx, target)
161+
// expose utility to re-render a component from tests
162+
cy.render = (jsx: JSXElement) => {
163+
render(jsx)
164+
}
165+
const component = render(jsx);
162166
cy.wrap(component, { log: false }).as(alias || displayname)
163167
})
164168
cy.copyComponentStyles(jsx)

0 commit comments

Comments
 (0)