|
| 1 | +import { act, renderHook } from '@testing-library/react'; |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 3 | + |
| 4 | +import { subscribeKeyboardHeight } from '../../utils/keyboardHeight/subscribeKeyboardHeight.ts'; |
| 5 | + |
| 6 | +import { useKeyboardHeight } from './useKeyboardHeight.ts'; |
| 7 | + |
| 8 | +vi.mock('@webview-kit/core', () => ({ |
| 9 | + subscribeKeyboardHeight: vi.fn(), |
| 10 | +})); |
| 11 | + |
| 12 | +const mockSubscribeKeyboardHeight = vi.mocked(subscribeKeyboardHeight); |
| 13 | + |
| 14 | +describe('useKeyboardHeight', () => { |
| 15 | + let mockUnsubscribe: ReturnType<typeof vi.fn>; |
| 16 | + |
| 17 | + beforeEach(() => { |
| 18 | + mockUnsubscribe = vi.fn(); |
| 19 | + mockSubscribeKeyboardHeight.mockReturnValue({ unsubscribe: mockUnsubscribe }); |
| 20 | + }); |
| 21 | + |
| 22 | + afterEach(() => { |
| 23 | + vi.clearAllMocks(); |
| 24 | + }); |
| 25 | + |
| 26 | + describe('initial state', () => { |
| 27 | + it('should return 0 as initial keyboard height', () => { |
| 28 | + const { result } = renderHook(() => useKeyboardHeight()); |
| 29 | + |
| 30 | + expect(result.current).toBe(0); |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + describe('subscription behavior', () => { |
| 35 | + it('should call subscribeKeyboardHeight with immediate: true by default', () => { |
| 36 | + renderHook(() => useKeyboardHeight()); |
| 37 | + |
| 38 | + expect(mockSubscribeKeyboardHeight).toHaveBeenCalledWith({ |
| 39 | + callback: expect.any(Function), |
| 40 | + immediate: true, |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should call subscribeKeyboardHeight with immediate: false when specified', () => { |
| 45 | + renderHook(() => useKeyboardHeight({ immediate: false })); |
| 46 | + |
| 47 | + expect(mockSubscribeKeyboardHeight).toHaveBeenCalledWith({ |
| 48 | + callback: expect.any(Function), |
| 49 | + immediate: false, |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should update keyboard height when callback is invoked', () => { |
| 54 | + mockSubscribeKeyboardHeight.mockImplementation(({ callback }) => { |
| 55 | + callback(300); |
| 56 | + return { unsubscribe: mockUnsubscribe }; |
| 57 | + }); |
| 58 | + |
| 59 | + const { result } = renderHook(() => useKeyboardHeight()); |
| 60 | + |
| 61 | + expect(result.current).toBe(300); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + describe('cleanup behavior', () => { |
| 66 | + it('should call unsubscribe on unmount', () => { |
| 67 | + const { unmount } = renderHook(() => useKeyboardHeight()); |
| 68 | + |
| 69 | + expect(mockUnsubscribe).not.toHaveBeenCalled(); |
| 70 | + |
| 71 | + unmount(); |
| 72 | + |
| 73 | + expect(mockUnsubscribe).toHaveBeenCalledTimes(1); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should resubscribe when immediate option changes', () => { |
| 77 | + const { rerender } = renderHook(({ immediate }) => useKeyboardHeight({ immediate }), { |
| 78 | + initialProps: { immediate: true }, |
| 79 | + }); |
| 80 | + |
| 81 | + expect(mockSubscribeKeyboardHeight).toHaveBeenCalledTimes(1); |
| 82 | + |
| 83 | + rerender({ immediate: false }); |
| 84 | + |
| 85 | + expect(mockUnsubscribe).toHaveBeenCalledTimes(1); |
| 86 | + expect(mockSubscribeKeyboardHeight).toHaveBeenCalledTimes(2); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + describe('keyboard height updates', () => { |
| 91 | + it('should track keyboard height changes', () => { |
| 92 | + let capturedCallback: ((height: number) => void) | null = null; |
| 93 | + |
| 94 | + mockSubscribeKeyboardHeight.mockImplementation(({ callback }) => { |
| 95 | + capturedCallback = callback; |
| 96 | + return { unsubscribe: mockUnsubscribe }; |
| 97 | + }); |
| 98 | + |
| 99 | + const { result } = renderHook(() => useKeyboardHeight()); |
| 100 | + |
| 101 | + expect(result.current).toBe(0); |
| 102 | + |
| 103 | + act(() => { |
| 104 | + capturedCallback?.(250); |
| 105 | + }); |
| 106 | + |
| 107 | + expect(result.current).toBe(250); |
| 108 | + |
| 109 | + act(() => { |
| 110 | + capturedCallback?.(0); |
| 111 | + }); |
| 112 | + |
| 113 | + expect(result.current).toBe(0); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should handle various keyboard heights', () => { |
| 117 | + const heights = [0, 100, 250, 350, 500]; |
| 118 | + |
| 119 | + for (const expectedHeight of heights) { |
| 120 | + mockSubscribeKeyboardHeight.mockImplementation(({ callback }) => { |
| 121 | + callback(expectedHeight); |
| 122 | + return { unsubscribe: mockUnsubscribe }; |
| 123 | + }); |
| 124 | + |
| 125 | + const { result } = renderHook(() => useKeyboardHeight()); |
| 126 | + |
| 127 | + expect(result.current).toBe(expectedHeight); |
| 128 | + } |
| 129 | + }); |
| 130 | + }); |
| 131 | + |
| 132 | + describe('use cases', () => { |
| 133 | + it('should provide keyboard height for bottom padding adjustment', () => { |
| 134 | + mockSubscribeKeyboardHeight.mockImplementation(({ callback }) => { |
| 135 | + callback(300); |
| 136 | + return { unsubscribe: mockUnsubscribe }; |
| 137 | + }); |
| 138 | + |
| 139 | + const { result } = renderHook(() => useKeyboardHeight()); |
| 140 | + |
| 141 | + const paddingBottom = `${result.current}px`; |
| 142 | + expect(paddingBottom).toBe('300px'); |
| 143 | + }); |
| 144 | + |
| 145 | + it('should detect keyboard visibility', () => { |
| 146 | + mockSubscribeKeyboardHeight.mockImplementation(({ callback }) => { |
| 147 | + callback(300); |
| 148 | + return { unsubscribe: mockUnsubscribe }; |
| 149 | + }); |
| 150 | + |
| 151 | + const { result } = renderHook(() => useKeyboardHeight()); |
| 152 | + |
| 153 | + const isKeyboardVisible = result.current > 0; |
| 154 | + expect(isKeyboardVisible).toBe(true); |
| 155 | + }); |
| 156 | + |
| 157 | + it('should detect keyboard hidden state', () => { |
| 158 | + mockSubscribeKeyboardHeight.mockImplementation(({ callback }) => { |
| 159 | + callback(0); |
| 160 | + return { unsubscribe: mockUnsubscribe }; |
| 161 | + }); |
| 162 | + |
| 163 | + const { result } = renderHook(() => useKeyboardHeight()); |
| 164 | + |
| 165 | + const isKeyboardVisible = result.current > 0; |
| 166 | + expect(isKeyboardVisible).toBe(false); |
| 167 | + }); |
| 168 | + }); |
| 169 | + |
| 170 | + describe('multiple instances', () => { |
| 171 | + it('should allow multiple independent hook instances', () => { |
| 172 | + let callback1: ((height: number) => void) | null = null; |
| 173 | + let callback2: ((height: number) => void) | null = null; |
| 174 | + |
| 175 | + mockSubscribeKeyboardHeight |
| 176 | + .mockImplementationOnce(({ callback }) => { |
| 177 | + callback1 = callback; |
| 178 | + return { unsubscribe: mockUnsubscribe }; |
| 179 | + }) |
| 180 | + .mockImplementationOnce(({ callback }) => { |
| 181 | + callback2 = callback; |
| 182 | + return { unsubscribe: mockUnsubscribe }; |
| 183 | + }); |
| 184 | + |
| 185 | + const { result: result1 } = renderHook(() => useKeyboardHeight()); |
| 186 | + const { result: result2 } = renderHook(() => useKeyboardHeight()); |
| 187 | + |
| 188 | + act(() => { |
| 189 | + callback1?.(200); |
| 190 | + callback2?.(200); |
| 191 | + }); |
| 192 | + |
| 193 | + expect(result1.current).toBe(200); |
| 194 | + expect(result2.current).toBe(200); |
| 195 | + }); |
| 196 | + }); |
| 197 | +}); |
0 commit comments