I\'m writing a React Native app and I\'m using Jest to unit test my code.
I\'ve written a function than checks if there is an internet connection. I know want to wri
This is pretty simple, just mock the NetInfo
like so:
import {
NetInfo
} from "react-native";
import checkNetwork from "...";
import {
NO_NETWORK_CONNECTION
} from "...";
jest.mock('react-native', () => ({
NetInfo: {
isConnected: {
fetch: jest.fn()
}
}
}))
test('test offline', async () => {
NetInfo.isConnected.fetch.mockResolvedValueOnce(false)
expect(await checkNetwork()).toBe(NO_NETWORK_CONNECTION)
})
test('test online', async () => {
NetInfo.isConnected.fetch.mockResolvedValueOnce(true)
expect(await checkNetwork()).toBe(true)
})