jest-battery-mock

2.0.1 • Public • Published

jest-battery-mock

BatteryManager API mock for Jest

English | 简体中文

npm GitHub Workflow Status Codecov

Installation

pnpm add -D jest-battery-mock

Example

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

Enable mock for navigator.getBattery

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

You can set BatteryManager initial state with preset, By default use PRESET_COMMON

disableMock

Disable mock for navigator.getBattery

type disableMock = () => void

dispatch

Change BatteryManager state and trigger event, trigger the corresponding event only when the state changes

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

You can also use preset here

Preset

We have some presets for you, you can use it when call enableMock and dispatch:

PRESET_COMMON PRESET_USE_BATTERY PRESET_LOW_POWER
charging true false false
chargingTime 0 Infinity Infinity
dischargingTime Infinity Infinity 0
level 1 0.8 0.1

Reference & Thanks

Dependents (0)

Package Sidebar

Install

npm i jest-battery-mock

Weekly Downloads

8

Version

2.0.1

License

MIT

Unpacked Size

14.3 kB

Total Files

8

Last publish

Collaborators

  • suiyun39