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

Commit acd8c55

Browse files
committed
add mocking readme
1 parent 400f85d commit acd8c55

File tree

3 files changed

+95
-18
lines changed

3 files changed

+95
-18
lines changed
Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,47 @@
1-
example from https://reactjs.org/docs/testing-recipes.html#mocking-modules
1+
# Mocking ES6 imports
2+
3+
The original example comes from https://reactjs.org/docs/testing-recipes.html#mocking-modules
4+
5+
The [contact.js](contact.js) component imports [map.js](map.js) component. But the real Map is expensive to render - it uses Google Maps, etc. Thus during tests we would like to replace the real Map with `DummyMap` component that only renders the props.
6+
7+
See [spec.js](spec.js) test file. The recommended approach is to mock the ES6 import.
8+
9+
```js
10+
// contact.js
11+
import Map from './map'
12+
export default function Contact(props) {
13+
// renders <Map ...>
14+
}
15+
16+
// spec.js
17+
import Contact from './contact'
18+
import * as MapModule from './map'
19+
20+
const DummyMap = props => (
21+
<div data-testid="map">
22+
DummyMap {props.center.lat}:{props.center.long}
23+
</div>
24+
)
25+
26+
it('renders stubbed Map', () => {
27+
// DummyMap component will be called with props and any other arguments
28+
cy.stub(MapModule, 'default').callsFake(DummyMap)
29+
30+
cy.viewport(500, 500)
31+
const center = { lat: 0, long: 0 }
32+
mount(
33+
<Contact
34+
name="Joni Baez"
35+
email="test@example.com"
36+
site="http://test.com"
37+
center={center}
38+
/>,
39+
)
40+
41+
cy.contains('Contact Joni Baez via')
42+
// confirm DummyMap renders "0:0" passed via props
43+
cy.contains('[data-testid="map"]', '0:0').should('be.visible')
44+
})
45+
```
46+
47+
![Dummy map test](images/dummy-map.png)
176 KB
Loading
Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import React from 'react'
22
import { mount } from 'cypress-react-unit-test'
3+
// Component "Contact" has child component "Map" that is expensive to render
34
import Contact from './contact'
45
import Map from './map'
6+
import * as MapModule from './map'
57

6-
it('should render contact information', () => {
8+
describe('Mock imported component', () => {
79
// mock Map component used by Contact component
810
// whenever React tries to instantiate using Map constructor
911
// call DummyMap constructor
@@ -12,22 +14,51 @@ it('should render contact information', () => {
1214
DummyMap {props.center.lat}:{props.center.long}
1315
</div>
1416
)
15-
cy.stub(React, 'createElement')
16-
.callThrough()
17-
.withArgs(Map)
18-
.callsFake((constructor, props) => React.createElement(DummyMap, props))
1917

20-
cy.viewport(500, 500)
21-
const center = { lat: 0, long: 0 }
22-
mount(
23-
<Contact
24-
name="Joni Baez"
25-
email="test@example.com"
26-
site="http://test.com"
27-
center={center}
28-
/>,
29-
)
18+
context('by stubbing React.createElement method', () => {
19+
// stubbing like this works, but is less than ideal
20+
it('should render contact information', () => {
21+
cy.stub(React, 'createElement')
22+
.callThrough()
23+
.withArgs(Map)
24+
.callsFake((constructor, props) => React.createElement(DummyMap, props))
25+
26+
cy.viewport(500, 500)
27+
const center = { lat: 0, long: 0 }
28+
mount(
29+
<Contact
30+
name="Joni Baez"
31+
email="test@example.com"
32+
site="http://test.com"
33+
center={center}
34+
/>,
35+
)
36+
37+
cy.contains('Contact Joni Baez via')
38+
cy.contains('[data-testid="map"]', '0:0').should('be.visible')
39+
})
40+
})
41+
42+
context('via mocking ES6 default import', () => {
43+
// recommended
44+
it('renders stubbed Map', () => {
45+
// DummyMap component will be called with props and any other arguments
46+
cy.stub(MapModule, 'default').callsFake(DummyMap)
47+
48+
cy.viewport(500, 500)
49+
const center = { lat: 0, long: 0 }
50+
mount(
51+
<Contact
52+
name="Joni Baez"
53+
email="test@example.com"
54+
site="http://test.com"
55+
center={center}
56+
/>,
57+
)
3058

31-
cy.contains('Contact Joni Baez via')
32-
cy.contains('[data-testid="map"]', '0:0').should('be.visible')
59+
cy.contains('Contact Joni Baez via')
60+
// confirm DummyMap renders "0:0" passed via props
61+
cy.contains('[data-testid="map"]', '0:0').should('be.visible')
62+
})
63+
})
3364
})

0 commit comments

Comments
 (0)