|
| 1 | +import {act, renderHook} from '@ver0/react-hooks-testing'; |
| 2 | +import type {vi} from 'vitest'; |
| 3 | +import {describe, expect, it, beforeEach} from 'vitest'; |
| 4 | +import {useBattery} from '../index.js'; |
| 5 | +import {expectResultValue} from '../util/testing/test-helpers.js'; |
| 6 | + |
| 7 | +type MockFn = ReturnType<typeof vi.fn>; |
| 8 | + |
| 9 | +type BatteryManager = { |
| 10 | + charging: boolean; |
| 11 | + chargingTime: number; |
| 12 | + dischargingTime: number; |
| 13 | + level: number; |
| 14 | + addEventListener: MockFn; |
| 15 | + removeEventListener: MockFn; |
| 16 | +}; |
| 17 | + |
| 18 | +describe('useBattery', () => { |
| 19 | + // Use the global getBattery mock that's already set up in the test environment |
| 20 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion |
| 21 | + const getBatteryMock = globalThis.navigator.getBattery as MockFn; |
| 22 | + |
| 23 | + // Access the mock battery object from the getBattery mock |
| 24 | + let mockBattery: BatteryManager; |
| 25 | + |
| 26 | + beforeEach(async () => { |
| 27 | + getBatteryMock.mockClear(); |
| 28 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment |
| 29 | + mockBattery = await getBatteryMock(); |
| 30 | + mockBattery.addEventListener.mockClear(); |
| 31 | + mockBattery.removeEventListener.mockClear(); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should be defined', () => { |
| 35 | + expect(useBattery).toBeDefined(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should render', async () => { |
| 39 | + const {result} = await renderHook(() => useBattery()); |
| 40 | + expectResultValue(result); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should return an object of certain structure', async () => { |
| 44 | + const {result} = await renderHook(() => useBattery()); |
| 45 | + const value = expectResultValue(result); |
| 46 | + |
| 47 | + expect(typeof value).toBe('object'); |
| 48 | + expect(Object.keys(value)).toEqual([ |
| 49 | + 'isSupported', |
| 50 | + 'fetched', |
| 51 | + 'charging', |
| 52 | + 'chargingTime', |
| 53 | + 'dischargingTime', |
| 54 | + 'level', |
| 55 | + ]); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should return isSupported: true when API is available', async () => { |
| 59 | + const {result} = await renderHook(() => useBattery()); |
| 60 | + const value = expectResultValue(result); |
| 61 | + expect(value.isSupported).toBe(true); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should fetch battery state when API is supported', async () => { |
| 65 | + const {result} = await renderHook(() => useBattery()); |
| 66 | + |
| 67 | + await act(async () => { |
| 68 | + await Promise.resolve(); |
| 69 | + }); |
| 70 | + |
| 71 | + const value = expectResultValue(result); |
| 72 | + expect(value.fetched).toBe(true); |
| 73 | + expect(value.charging).toBe(true); |
| 74 | + expect(value.chargingTime).toBe(3600); |
| 75 | + expect(value.dischargingTime).toBe(Infinity); |
| 76 | + expect(value.level).toBe(0.75); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should subscribe to battery events', async () => { |
| 80 | + await renderHook(() => useBattery()); |
| 81 | + |
| 82 | + await act(async () => { |
| 83 | + await Promise.resolve(); |
| 84 | + }); |
| 85 | + |
| 86 | + expect(mockBattery.addEventListener).toHaveBeenCalledWith('chargingchange', expect.any(Function)); |
| 87 | + expect(mockBattery.addEventListener).toHaveBeenCalledWith('chargingtimechange', expect.any(Function)); |
| 88 | + expect(mockBattery.addEventListener).toHaveBeenCalledWith('dischargingtimechange', expect.any(Function)); |
| 89 | + expect(mockBattery.addEventListener).toHaveBeenCalledWith('levelchange', expect.any(Function)); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should unsubscribe from battery events on unmount', async () => { |
| 93 | + const {unmount} = await renderHook(() => useBattery()); |
| 94 | + |
| 95 | + await act(async () => { |
| 96 | + await Promise.resolve(); |
| 97 | + }); |
| 98 | + |
| 99 | + await unmount(); |
| 100 | + |
| 101 | + expect(mockBattery.removeEventListener).toHaveBeenCalledWith('chargingchange', expect.any(Function)); |
| 102 | + expect(mockBattery.removeEventListener).toHaveBeenCalledWith('chargingtimechange', expect.any(Function)); |
| 103 | + expect(mockBattery.removeEventListener).toHaveBeenCalledWith('dischargingtimechange', expect.any(Function)); |
| 104 | + expect(mockBattery.removeEventListener).toHaveBeenCalledWith('levelchange', expect.any(Function)); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should update state when battery events fire', async () => { |
| 108 | + const {result} = await renderHook(() => useBattery()); |
| 109 | + |
| 110 | + await act(async () => { |
| 111 | + await Promise.resolve(); |
| 112 | + }); |
| 113 | + |
| 114 | + let value = expectResultValue(result); |
| 115 | + expect(value.level).toBe(0.75); |
| 116 | + |
| 117 | + // Simulate battery level change |
| 118 | + mockBattery.level = 0.5; |
| 119 | + |
| 120 | + // Get the handler that was registered for levelchange |
| 121 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion |
| 122 | + const levelChangeHandler = mockBattery.addEventListener.mock.calls.find( |
| 123 | + (call) => call[0] === 'levelchange', |
| 124 | + )?.[1] as () => void; |
| 125 | + |
| 126 | + await act(async () => { |
| 127 | + levelChangeHandler(); |
| 128 | + }); |
| 129 | + |
| 130 | + value = expectResultValue(result); |
| 131 | + expect(value.level).toBe(0.5); |
| 132 | + }); |
| 133 | +}); |
0 commit comments