NetworkInformation API mock for Jest
pnpm add -D jest-network-mock
import { enableMock, disableMock, dispatch } from 'jest-network-mock'
beforeEach(() => enableMock())
afterEach(() => disableMock())
test('example', done => {
navigator.connection.addEventListener('change', () => {
done()
})
dispatch({ effectiveType: '4g' })
})
Enable mock for navigator.connection
type enableMock = (preset?: ConnectionInfo) => void
You can set navigator.connection
initial state with preset
, By default use PRESET_4G
Disable mock for navigator.connection
type disableMock = () => void
Change navigator.connection
state and trigger change
event
type dispatch = (state?: Partial<ConnectionInfo>) => boolean
You can also use preset here
We have some presets for you, you can use it when call enableMock
and dispatch
:
PRESET_4G | PRESET_FAST_3G | PRESET_SLOW_3G | PRESET_OFFLINE | |
---|---|---|---|---|
downlink | 10 | 1.3 | 0.35 | 0 |
downlinkMax | 20 | 2.6 | 0.7 | 0 |
effectiveType | 4g |
3g |
3g |
4g |
rtt | 250 | 500 | 1850 | 0 |
saveData | false | false | false | false |
type | wifi |
wifi |
wifi |
none |