Skip to content

Commit

Permalink
fix: Fix the provided Jest mock so it works with the instructions in …
Browse files Browse the repository at this point in the history
…the README

Fixes #345
Fixes #327
  • Loading branch information
matt-oakes committed Apr 14, 2020
1 parent 82a484a commit a24ebb1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
1 change: 0 additions & 1 deletion README.md
Expand Up @@ -430,7 +430,6 @@ setupFiles: ['<rootDir>/jest.setup.js']
You should then add the following to your Jest setup file to mock the NetInfo Native Module:
```js
import { NativeModules } from 'react-native';
import RNCNetInfoMock from '@react-native-community/netinfo/jest/netinfo-mock.js';

jest.mock('@react-native-community/netinfo', () => RNCNetInfoMock);
Expand Down
20 changes: 12 additions & 8 deletions jest/netinfo-mock.js
Expand Up @@ -3,20 +3,24 @@
*/
/* eslint-env jest */

const RNCNetInfoMock = {
getCurrentState: jest.fn(),
addListener: jest.fn(),
removeListeners: jest.fn(),
};

RNCNetInfoMock.getCurrentState.mockResolvedValue({
const defaultState = {
type: 'cellular',
isConnected: true,
isInternetReachable: true,
details: {
isConnectionExpensive: true,
cellularGeneration: '3g',
},
});
};

const RNCNetInfoMock = {
configure: jest.fn(),
fetch: jest.fn(),
addEventListener: jest.fn(),
useNetInfo: jest.fn(),
};

RNCNetInfoMock.fetch.mockResolvedValue(defaultState);
RNCNetInfoMock.useNetInfo.mockResolvedValue(defaultState);

This comment has been minimized.

Copy link
@kuladeepu

kuladeepu Sep 28, 2020

This hook returns an object. But in its mock, mockResolvedValueOnce has been used, which looks like it resolves a value. How does it comply here?

function useNetInfo(configuration?: Partial<Types.NetInfoConfiguration> | undefined): Types.NetInfoState

A React Hook which updates when the connection state changes.
@returns — The connection state.


module.exports = RNCNetInfoMock;

0 comments on commit a24ebb1

Please sign in to comment.