Skip to content

Commit 34f6c18

Browse files
committed
Add Mock A Function That A Component Imports as a react til
1 parent 85e30d5 commit 34f6c18

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
1010
For a steady stream of TILs from a variety of rocketeers, checkout
1111
[til.hashrocket.com](https://til.hashrocket.com/).
1212

13-
_661 TILs and counting..._
13+
_662 TILs and counting..._
1414

1515
---
1616

@@ -464,6 +464,7 @@ _661 TILs and counting..._
464464
- [Inactive And Active Component Styles With Radium](react/inactive-and-active-component-styles-with-radium.md)
465465
- [Inline Style Attributes Should Be Camel Cased](react/inline-style-attributes-should-be-camel-cased.md)
466466
- [Mapping Over One Or Many Children](react/mapping-over-one-or-many-children.md)
467+
- [Mock A Function That A Component Imports](react/mock-a-function-that-a-component-imports.md)
467468
- [Passing Props Down To React-Router Route](react/passing-props-down-to-react-router-route.md)
468469
- [Proxy To An API Server In Development With CRA](react/proxy-to-an-api-server-in-development-with-cra.md)
469470
- [Quickly Search For A Component With React DevTools](react/quickly-search-for-a-component-with-react-devtools.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Mock A Function That A Component Imports
2+
3+
You have a component that relies on an imported function,
4+
`isAuthenticated()`.
5+
6+
```javascript
7+
// MyComponent.js
8+
import React from 'react';
9+
import { isAuthenticated } from './authentication';
10+
11+
const MyComponent = (props) => {
12+
if (isAuthenticated()) {
13+
return (<div>{/* ... */}</div>);
14+
} else {
15+
return (<div>Not authenticated</div>);
16+
}
17+
};
18+
```
19+
20+
You'd like to test that component without having to manage the
21+
authentication of a user. One option is to mock out that function. This can
22+
be done with some help from [`jest.fn()` and the `mock.mockReturnValue()`
23+
function](https://github.com/jbranchaud/til/blob/master/javascript/mock-a-function-with-return-values-using-jest.md).
24+
25+
```javascript
26+
// MyComponent.test.js
27+
// ... various testing imports
28+
29+
import * as authModules from './authentication';
30+
31+
it('renders the component', () => {
32+
authModules.isAuthenticated = jest.fn().mockReturnValue(true);
33+
34+
const wrapper = shallow(<MyComponent />);
35+
expect(toJson(wrapper)).toMatchSnapshot();
36+
});
37+
```
38+
39+
By importing the same module and functions used by `MyComponent`, we are
40+
then able to replace them (specifically, `isAuthenticated`) with a mocked
41+
version of the function that returns whatever value we'd like. As
42+
`MyComponent` is being rendered, it will invoked our mocked version of
43+
`isAuthenticated` instead of the actual one.
44+
45+
You could test the other direction like so:
46+
47+
```javascript
48+
authModules.isAuthenticated = jest.fn().mockReturnValue(false);
49+
```

0 commit comments

Comments
 (0)