2.0.1 • Published 1 year ago

jest-battery-mock v2.0.1

Weekly downloads
4
License
MIT
Repository
github
Last release
1 year ago

jest-battery-mock

用于 Jest 的 BatteryManager API 模拟

English | 简体中文

npm GitHub Workflow Status Codecov

安装

pnpm add -D jest-battery-mock

示例

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

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

test('example', async () => {
  const batteryManager = await navigator.getBattery()
  const callback = jest.fn()
  
  batteryManager.addEventListener('chargingchange', callback)
  dispatch({ charging: false })
  expect(callback).toBeCalledTimes(1)
})

API

enableMock

启用 navigator.getBattery 模拟

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

你可以使用 preset 设置 BatteryManager 的初始状态,默认使用 PRESET_COMMON

disableMock

禁用 navigator.getBattery 模拟

type disableMock = () => void

dispatch

修改 BatteryManager 的状态并触发事件,只有状态发生变化时才会触发对应事件

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

你也可以在这里使用预设

预设

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

PRESET_COMMONPRESET_USE_BATTERYPRESET_LOW_POWER
chargingtruefalsefalse
chargingTime0InfinityInfinity
dischargingTimeInfinityInfinity0
level10.80.1

参考与感谢

2.0.1

1 year ago

2.0.0

2 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago