|
| 1 | +import {act, renderHook} from '@ver0/react-hooks-testing'; |
| 2 | +import {beforeEach, describe, expect, it} from 'vitest'; |
| 3 | +import {useBattery} from '../index.js'; |
| 4 | +import type {BatteryManagerMock} from '../util/testing/setup/battery.test.js'; |
| 5 | +import {getBatteryMock, mockBattery, resetBatteryMock} from '../util/testing/setup/battery.test.js'; |
| 6 | +import {expectCallArgs, expectResultValue} from '../util/testing/test-helpers.js'; |
| 7 | + |
| 8 | +/** Flushes the microtask queue so the hook's pending getBattery() settles. */ |
| 9 | +const flushBattery = async () => { |
| 10 | + await act(async () => { |
| 11 | + await Promise.resolve(); |
| 12 | + }); |
| 13 | +}; |
| 14 | + |
| 15 | +describe('useBattery', () => { |
| 16 | + beforeEach(() => { |
| 17 | + resetBatteryMock(); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should be defined', () => { |
| 21 | + expect(useBattery).toBeDefined(); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should render', async () => { |
| 25 | + const {result} = await renderHook(() => useBattery()); |
| 26 | + expectResultValue(result); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should return an object of certain structure', async () => { |
| 30 | + const {result} = await renderHook(() => useBattery()); |
| 31 | + const value = expectResultValue(result); |
| 32 | + |
| 33 | + expect(Object.keys(value).toSorted()).toEqual([ |
| 34 | + 'charging', |
| 35 | + 'chargingTime', |
| 36 | + 'dischargingTime', |
| 37 | + 'fetched', |
| 38 | + 'isSupported', |
| 39 | + 'level', |
| 40 | + ]); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should return isSupported: true when API is available', async () => { |
| 44 | + const {result} = await renderHook(() => useBattery()); |
| 45 | + const value = expectResultValue(result); |
| 46 | + expect(value.isSupported).toBe(true); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should fetch battery state when API is supported', async () => { |
| 50 | + const {result} = await renderHook(() => useBattery()); |
| 51 | + |
| 52 | + await flushBattery(); |
| 53 | + |
| 54 | + const value = expectResultValue(result); |
| 55 | + expect(value.fetched).toBe(true); |
| 56 | + expect(value.charging).toBe(true); |
| 57 | + expect(value.chargingTime).toBe(3600); |
| 58 | + expect(value.dischargingTime).toBe(Infinity); |
| 59 | + expect(value.level).toBe(0.75); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should subscribe to battery events', async () => { |
| 63 | + await renderHook(() => useBattery()); |
| 64 | + |
| 65 | + await flushBattery(); |
| 66 | + |
| 67 | + expect(mockBattery.addEventListener).toHaveBeenCalledWith('chargingchange', expect.any(Function)); |
| 68 | + expect(mockBattery.addEventListener).toHaveBeenCalledWith('chargingtimechange', expect.any(Function)); |
| 69 | + expect(mockBattery.addEventListener).toHaveBeenCalledWith('dischargingtimechange', expect.any(Function)); |
| 70 | + expect(mockBattery.addEventListener).toHaveBeenCalledWith('levelchange', expect.any(Function)); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should unsubscribe the very handlers it registered on unmount', async () => { |
| 74 | + const {unmount} = await renderHook(() => useBattery()); |
| 75 | + |
| 76 | + await flushBattery(); |
| 77 | + |
| 78 | + // Compared by reference, so removing a different function than the one |
| 79 | + // registered -- a listener leak -- fails here. |
| 80 | + const registered = [...mockBattery.addEventListener.mock.calls]; |
| 81 | + expect(registered).toHaveLength(4); |
| 82 | + |
| 83 | + await unmount(); |
| 84 | + |
| 85 | + expect(mockBattery.removeEventListener.mock.calls).toEqual(registered); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should update state when battery events fire', async () => { |
| 89 | + const {result} = await renderHook(() => useBattery()); |
| 90 | + |
| 91 | + await flushBattery(); |
| 92 | + |
| 93 | + let value = expectResultValue(result); |
| 94 | + expect(value.level).toBe(0.75); |
| 95 | + |
| 96 | + // Simulate battery level change |
| 97 | + mockBattery.level = 0.5; |
| 98 | + |
| 99 | + const [, levelChangeHandler] = expectCallArgs(mockBattery.addEventListener, 3); |
| 100 | + |
| 101 | + await act(async () => { |
| 102 | + levelChangeHandler(); |
| 103 | + }); |
| 104 | + |
| 105 | + value = expectResultValue(result); |
| 106 | + expect(value.level).toBe(0.5); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should not subscribe when unmounted before getBattery() resolves', async () => { |
| 110 | + let resolveBattery: (battery: BatteryManagerMock) => void = () => undefined; |
| 111 | + |
| 112 | + getBatteryMock.mockImplementation( |
| 113 | + async () => |
| 114 | + new Promise<BatteryManagerMock>((resolve) => { |
| 115 | + resolveBattery = resolve; |
| 116 | + }), |
| 117 | + ); |
| 118 | + |
| 119 | + const {unmount} = await renderHook(() => useBattery()); |
| 120 | + await unmount(); |
| 121 | + |
| 122 | + resolveBattery(mockBattery); |
| 123 | + await flushBattery(); |
| 124 | + |
| 125 | + expect(mockBattery.addEventListener).not.toHaveBeenCalled(); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should report unfetched state when getBattery() rejects', async () => { |
| 129 | + getBatteryMock.mockImplementation(async () => { |
| 130 | + throw new Error('Battery API blocked by permissions policy'); |
| 131 | + }); |
| 132 | + |
| 133 | + const {result} = await renderHook(() => useBattery()); |
| 134 | + |
| 135 | + await flushBattery(); |
| 136 | + |
| 137 | + const value = expectResultValue(result); |
| 138 | + expect(value.fetched).toBe(false); |
| 139 | + expect(value.isSupported).toBe(true); |
| 140 | + expect(value.charging).toBeUndefined(); |
| 141 | + expect(value.level).toBeUndefined(); |
| 142 | + expect(mockBattery.addEventListener).not.toHaveBeenCalled(); |
| 143 | + }); |
| 144 | +}); |
0 commit comments