You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Mar 5, 2022. It is now read-only.
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
+
importMapfrom'./map'
12
+
exportdefaultfunctionContact(props) {
13
+
// renders <Map ...>
14
+
}
15
+
16
+
// spec.js
17
+
importContactfrom'./contact'
18
+
import*asMapModulefrom'./map'
19
+
20
+
constDummyMap=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
+
constcenter= { 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
0 commit comments