Skip to content

Commit 066231a

Browse files
committed
react examples
1 parent ed83150 commit 066231a

File tree

1 file changed

+45
-13
lines changed

1 file changed

+45
-13
lines changed

content/api/commands/mount.md

+45-13
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,17 @@ file in the `include` option:
133133
"include": ["./src", "cypress.d.ts"]
134134
```
135135

136-
## Examples
136+
## Additional Mount Commands
137137

138-
You can create as many custom mount commands as you need. There might be times
139-
when you need to do additional setup for certain tests that other tests don't
140-
need. In this case, it is good to create a new command for those tests.
138+
You can create additional mount commands to fit your needs. For instance, there
139+
might be times when you need to do additional setup for certain tests that other
140+
tests don't need. In this case, it is good to create a new command for those
141+
tests.
141142

142143
Below are some examples for specific use cases.
143144

145+
## React Examples
146+
144147
### React Router
145148

146149
If you have a component that consumes a hook or component from React Router, you
@@ -156,10 +159,13 @@ Cypress.Commands.add('mountWithRouter', (component, options) => {
156159
const { routerProps = { initialEntries: ['/'] }, ...mountOptions } = options
157160

158161
const wrapped = <MemoryRouter {...routerProps}>{component}</MemoryRouter>
162+
159163
return mount(wrapped, mountOptions)
160164
})
161165
```
162166

167+
You can pass in custom props for the `MemoryRouter` in the `options` param.
168+
163169
Example usage:
164170

165171
```jsx
@@ -173,22 +179,48 @@ it('Logout link should appear when logged in', () => {
173179
})
174180
```
175181

176-
### React Redux
182+
### Redux
177183

178-
When using Redux, your component might consume data from a Redux store. Below is
179-
an example that setups a Redux Provider and
184+
To use a component that consumes state or actions from a Redux store, you can
185+
create a `mountWithRedux` command that will wrap your component in a Redux
186+
Provider:
180187

181188
```jsx
182189
import { mount } from '@cypress/react'
183-
import { Provider as ReduxProvider } from 'react-redux'
184-
import { getStore } from '../../redux/store'
190+
import { Provider } from 'react-redux'
191+
import { store } from '../../redux/store'
185192

186-
Cypress.Commands.add('mountWithRedux', (component, options) => {
187-
const { reduxProps = { store: getStore() }, ...mountOptions } = options
193+
Cypress.Commands.add('mountWithRedux', (component, options = {}) => {
194+
const { reduxProps = { store: store }, ...mountOptions } = options
195+
196+
const wrapped = <Provider {...reduxProps}>{component}</Provider>
188197

189-
const wrapped = <ReduxProvider {...reduxProps}>{component}</ReduxProvider>
190198
return mount(wrapped, mountOptions)
191199
})
192200
```
193201

194-
The below example sets up a
202+
The `options` param has a `reduxProps` property that you can use to pass in a
203+
custom store. If `reduxProps` isn't specified, it will use the default store
204+
imported from `store.js`.
205+
206+
Example Usage:
207+
208+
```jsx
209+
import { store } from '../redux/store'
210+
import { setUser } from '../redux/userSlice'
211+
import { UserProfile } from './UserProfile'
212+
213+
it('User profile should display users name', () => {
214+
const user = { name: 'test person' }
215+
// setUser is an action exported from the user slice
216+
store.dispatch(setUser(user))
217+
cy.mountWithRedux(<UserProfile />)
218+
cy.get('div.name').should('have.text', user.name)
219+
})
220+
```
221+
222+
## Vue Examples
223+
224+
### Vue Plugins
225+
226+
### Global Vue Components

0 commit comments

Comments
 (0)