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

Commit 14eb6aa

Browse files
chore: Add in-memory router example (#424)
Co-authored-by: Gleb Bahmutov <gleb.bahmutov@gmail.com>
1 parent bf94dea commit 14eb6aa

File tree

5 files changed

+70
-19
lines changed

5 files changed

+70
-19
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ Spec | Description
173173
[mocking-axios](cypress/component/advanced/mocking-axios) | Stubbing methods from a 3rd party component like `axios`
174174
[mocking-component](cypress/component/advanced/mocking-component) | Replaced a child component with dummy component during test
175175
[mocking-imports](cypress/component/advanced/mocking-imports) | Stub a named ES6 import in various situations
176-
[react-router-v6](cypress/component/advanced/react-router-v6) | Example testing a [React Router v6](https://github.com/ReactTraining/react-router)
176+
[react-router-v6](cypress/component/advanced/react-router-v6) | Example testing a [React Router v6](https://github.com/ReactTraining/react-router). Both browser and in memory routers
177177
[renderless](cypress/component/advanced/renderless) | Testing a component that does not need to render itself into the DOM
178178
[set-timeout-example](cypress/component/advanced/set-timeout-example) | Control the clock with `cy.tick` and test loading components that use `setTimeout`
179179
[test-retries](cypress/component/advanced/test-retries) | This component is compatible with [Cypress Test Retries](https://github.com/cypress-io/cypress/pull/3968)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# React Router v6
2+
3+
We are testing the navigation in the [app.jsx](app.jsx) when it is surrounded by a React Router from [react-router-dom](https://github.com/ReactTraining/react-router#readme)
4+
5+
- [spec.js](spec.js) uses `BrowserRouter`
6+
- [in-memory-spec.js](in-memory-spec.js) uses `MemoryRouter`
7+
8+
![In memory router spec](images/in-memory.gif)
2.81 MB
Loading
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/// <reference types="cypress" />
2+
import React from 'react'
3+
import { mount } from 'cypress-react-unit-test'
4+
import { App } from './app.jsx'
5+
import { MemoryRouter } from 'react-router-dom'
6+
7+
describe('React Memory Router', () => {
8+
it('navigates through the link without changing url', () => {
9+
cy.viewport(600, 300)
10+
mount(
11+
<MemoryRouter
12+
initialEntries={['/about', '/two', { pathname: '/three' }]}
13+
initialIndex={0}
14+
>
15+
<App />
16+
</MemoryRouter>,
17+
)
18+
19+
// we are mocking the initial open route with `initialIndex`
20+
// so we should see "About" component
21+
cy.log('**About** component')
22+
cy.contains('h2', 'About')
23+
// because the routing is in memory, the URL stays at the spec filename
24+
cy.location('pathname').should('match', /in-memory-spec.js$/)
25+
26+
// Go to home route
27+
cy.contains('a', 'Home').click()
28+
29+
cy.log('**Home** component')
30+
cy.contains('h2', 'Home') // from the "Home" component
31+
// still at the spec url
32+
cy.location('pathname').should('match', /in-memory-spec.js$/)
33+
34+
// Go to about route
35+
cy.log('back to **About** component')
36+
cy.contains('a', 'About').click()
37+
38+
cy.contains('h2', 'About')
39+
// still at the spec url
40+
cy.location('pathname').should('match', /in-memory-spec.js$/)
41+
})
42+
})
Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
1-
// example comes from https://github.com/ReactTraining/react-router/blob/dev/docs/guides/getting-started.md
21
/// <reference types="cypress" />
32
import React from 'react'
43
import { mount } from 'cypress-react-unit-test'
54
import { App } from './app.jsx'
65
import { BrowserRouter as Router } from 'react-router-dom'
76

8-
it('shows links', () => {
9-
cy.viewport(600, 300)
10-
mount(
11-
<Router>
12-
<App />
13-
</Router>,
14-
)
7+
describe('React Router', () => {
8+
it('shows links', () => {
9+
cy.viewport(600, 300)
10+
mount(
11+
<Router>
12+
<App />
13+
</Router>,
14+
)
1515

16-
cy.get('nav').should('be.visible')
17-
cy.contains('Home')
18-
.click()
19-
.location('pathname')
20-
.should('equal', '/') // Home route
21-
cy.contains('h2', 'Home')
22-
cy.contains('About')
23-
.click()
24-
.location('pathname')
25-
.should('equal', '/about') // About route
16+
cy.get('nav').should('be.visible')
17+
cy.contains('Home')
18+
.click()
19+
.location('pathname')
20+
.should('equal', '/') // Home route
21+
cy.contains('h2', 'Home')
22+
cy.contains('About')
23+
.click()
24+
.location('pathname')
25+
.should('equal', '/about') // About route
26+
})
2627
})

0 commit comments

Comments
 (0)