Skip to content

Latest commit

 

History

History
80 lines (53 loc) · 2.26 KB

README.zh-CN.md

File metadata and controls

80 lines (53 loc) · 2.26 KB

jest-network-mock

用于 Jest 的 NetworkInformation API 模拟

English | 简体中文

npm GitHub Workflow Status Codecov

安装

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' })
})

API

enableMock

启用 navigator.connection 模拟

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

你可以使用 preset 设置 navigator.connection 的初始状态,默认使用 PRESET_4G

disableMock

禁用 navigator.connection 模拟

type disableMock = () => void

dispatch

修改 navigator.connection 状态并触发 change 事件

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

你也可以在这里使用预设

预设

我们提供了一些预设,你可以在调用 enableMockdispatch 时使用:

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

参考与感谢