jest-network-mock

2.0.1 • Public • Published

jest-network-mock

NetworkInformation API mock for Jest

English | 简体中文

npm GitHub Workflow Status Codecov

Installation

pnpm add -D jest-network-mock

Example

import { enableMock, disableMock, dispatch } from 'jest-network-mock'

beforeEach(() => enableMock())
afterEach(() => disableMock())

test('example', done => {
  navigator.connection.addEventListener('change', () => {
    done()
  })

  dispatch({ effectiveType: '4g' })
})

API

enableMock

Enable mock for navigator.connection

type enableMock = (preset?: ConnectionInfo) => void

You can set navigator.connection initial state with preset, By default use PRESET_4G

disableMock

Disable mock for navigator.connection

type disableMock = () => void

dispatch

Change navigator.connection state and trigger change event

type dispatch = (state?: Partial<ConnectionInfo>) => boolean

You can also use preset here

Preset

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

Reference & Thanks

Readme

Keywords

Package Sidebar

Install

npm i jest-network-mock

Weekly Downloads

23

Version

2.0.1

License

MIT

Unpacked Size

13.3 kB

Total Files

8

Last publish

Collaborators

  • suiyun39