@@ -133,14 +133,17 @@ file in the `include` option:
133
133
"include" : [" ./src" , " cypress.d.ts" ]
134
134
```
135
135
136
- ## Examples
136
+ ## Additional Mount Commands
137
137
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.
141
142
142
143
Below are some examples for specific use cases.
143
144
145
+ ## React Examples
146
+
144
147
### React Router
145
148
146
149
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) => {
156
159
const { routerProps = { initialEntries: [' /' ] }, ... mountOptions } = options
157
160
158
161
const wrapped = < MemoryRouter {... routerProps}> {component}< / MemoryRouter>
162
+
159
163
return mount (wrapped, mountOptions)
160
164
})
161
165
```
162
166
167
+ You can pass in custom props for the ` MemoryRouter ` in the ` options ` param.
168
+
163
169
Example usage:
164
170
165
171
``` jsx
@@ -173,22 +179,48 @@ it('Logout link should appear when logged in', () => {
173
179
})
174
180
```
175
181
176
- ### React Redux
182
+ ### Redux
177
183
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:
180
187
181
188
``` jsx
182
189
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'
185
192
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>
188
197
189
- const wrapped = < ReduxProvider {... reduxProps}> {component}< / ReduxProvider>
190
198
return mount (wrapped, mountOptions)
191
199
})
192
200
```
193
201
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