|
| 1 | +/** |
| 2 | + * Tests for PerpsWebSocketHealthToast.context |
| 3 | + */ |
| 4 | + |
| 5 | +import React from 'react'; |
| 6 | +import { Text } from 'react-native'; |
| 7 | +import { renderHook, act } from '@testing-library/react-hooks'; |
| 8 | +import { render } from '@testing-library/react-native'; |
| 9 | +import { |
| 10 | + WebSocketHealthToastProvider, |
| 11 | + useWebSocketHealthToastContext, |
| 12 | + WebSocketHealthToastContext, |
| 13 | +} from './PerpsWebSocketHealthToast.context'; |
| 14 | +import { WebSocketConnectionState } from '../../controllers/types'; |
| 15 | + |
| 16 | +describe('PerpsWebSocketHealthToast.context', () => { |
| 17 | + describe('WebSocketHealthToastProvider', () => { |
| 18 | + it('renders children correctly', () => { |
| 19 | + const { getByText } = render( |
| 20 | + <WebSocketHealthToastProvider> |
| 21 | + <Text>Test Child</Text> |
| 22 | + </WebSocketHealthToastProvider>, |
| 23 | + ); |
| 24 | + |
| 25 | + expect(getByText('Test Child')).toBeTruthy(); |
| 26 | + }); |
| 27 | + }); |
| 28 | + |
| 29 | + describe('useWebSocketHealthToastContext', () => { |
| 30 | + const wrapper = ({ children }: { children: React.ReactNode }) => ( |
| 31 | + <WebSocketHealthToastProvider>{children}</WebSocketHealthToastProvider> |
| 32 | + ); |
| 33 | + |
| 34 | + it('has correct initial state', () => { |
| 35 | + const { result } = renderHook(() => useWebSocketHealthToastContext(), { |
| 36 | + wrapper, |
| 37 | + }); |
| 38 | + |
| 39 | + expect(result.current.state).toEqual({ |
| 40 | + isVisible: false, |
| 41 | + connectionState: WebSocketConnectionState.DISCONNECTED, |
| 42 | + reconnectionAttempt: 0, |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + describe('show()', () => { |
| 47 | + it('updates visibility and connection state', () => { |
| 48 | + const { result } = renderHook(() => useWebSocketHealthToastContext(), { |
| 49 | + wrapper, |
| 50 | + }); |
| 51 | + |
| 52 | + act(() => { |
| 53 | + result.current.show(WebSocketConnectionState.CONNECTING, 1); |
| 54 | + }); |
| 55 | + |
| 56 | + expect(result.current.state).toEqual({ |
| 57 | + isVisible: true, |
| 58 | + connectionState: WebSocketConnectionState.CONNECTING, |
| 59 | + reconnectionAttempt: 1, |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + it('updates reconnection attempt number', () => { |
| 64 | + const { result } = renderHook(() => useWebSocketHealthToastContext(), { |
| 65 | + wrapper, |
| 66 | + }); |
| 67 | + |
| 68 | + act(() => { |
| 69 | + result.current.show(WebSocketConnectionState.CONNECTING, 5); |
| 70 | + }); |
| 71 | + |
| 72 | + expect(result.current.state.reconnectionAttempt).toBe(5); |
| 73 | + }); |
| 74 | + |
| 75 | + it('defaults reconnectionAttempt to 0 when not provided', () => { |
| 76 | + const { result } = renderHook(() => useWebSocketHealthToastContext(), { |
| 77 | + wrapper, |
| 78 | + }); |
| 79 | + |
| 80 | + act(() => { |
| 81 | + result.current.show(WebSocketConnectionState.CONNECTED); |
| 82 | + }); |
| 83 | + |
| 84 | + expect(result.current.state.reconnectionAttempt).toBe(0); |
| 85 | + }); |
| 86 | + |
| 87 | + it('handles DISCONNECTED state', () => { |
| 88 | + const { result } = renderHook(() => useWebSocketHealthToastContext(), { |
| 89 | + wrapper, |
| 90 | + }); |
| 91 | + |
| 92 | + act(() => { |
| 93 | + result.current.show(WebSocketConnectionState.DISCONNECTED, 3); |
| 94 | + }); |
| 95 | + |
| 96 | + expect(result.current.state).toEqual({ |
| 97 | + isVisible: true, |
| 98 | + connectionState: WebSocketConnectionState.DISCONNECTED, |
| 99 | + reconnectionAttempt: 3, |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + it('handles CONNECTED state', () => { |
| 104 | + const { result } = renderHook(() => useWebSocketHealthToastContext(), { |
| 105 | + wrapper, |
| 106 | + }); |
| 107 | + |
| 108 | + act(() => { |
| 109 | + result.current.show(WebSocketConnectionState.CONNECTED, 0); |
| 110 | + }); |
| 111 | + |
| 112 | + expect(result.current.state).toEqual({ |
| 113 | + isVisible: true, |
| 114 | + connectionState: WebSocketConnectionState.CONNECTED, |
| 115 | + reconnectionAttempt: 0, |
| 116 | + }); |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + describe('hide()', () => { |
| 121 | + it('sets visibility to false', () => { |
| 122 | + const { result } = renderHook(() => useWebSocketHealthToastContext(), { |
| 123 | + wrapper, |
| 124 | + }); |
| 125 | + |
| 126 | + // First show the toast |
| 127 | + act(() => { |
| 128 | + result.current.show(WebSocketConnectionState.DISCONNECTED, 1); |
| 129 | + }); |
| 130 | + |
| 131 | + expect(result.current.state.isVisible).toBe(true); |
| 132 | + |
| 133 | + // Then hide it |
| 134 | + act(() => { |
| 135 | + result.current.hide(); |
| 136 | + }); |
| 137 | + |
| 138 | + expect(result.current.state.isVisible).toBe(false); |
| 139 | + }); |
| 140 | + |
| 141 | + it('preserves other state when hiding', () => { |
| 142 | + const { result } = renderHook(() => useWebSocketHealthToastContext(), { |
| 143 | + wrapper, |
| 144 | + }); |
| 145 | + |
| 146 | + // Show with specific state |
| 147 | + act(() => { |
| 148 | + result.current.show(WebSocketConnectionState.CONNECTING, 2); |
| 149 | + }); |
| 150 | + |
| 151 | + // Hide |
| 152 | + act(() => { |
| 153 | + result.current.hide(); |
| 154 | + }); |
| 155 | + |
| 156 | + // Other state properties should be preserved |
| 157 | + expect(result.current.state.connectionState).toBe( |
| 158 | + WebSocketConnectionState.CONNECTING, |
| 159 | + ); |
| 160 | + expect(result.current.state.reconnectionAttempt).toBe(2); |
| 161 | + expect(result.current.state.isVisible).toBe(false); |
| 162 | + }); |
| 163 | + }); |
| 164 | + |
| 165 | + describe('setOnRetry()', () => { |
| 166 | + it('registers retry callback', () => { |
| 167 | + const { result } = renderHook(() => useWebSocketHealthToastContext(), { |
| 168 | + wrapper, |
| 169 | + }); |
| 170 | + |
| 171 | + const mockCallback = jest.fn(); |
| 172 | + |
| 173 | + act(() => { |
| 174 | + result.current.setOnRetry(mockCallback); |
| 175 | + }); |
| 176 | + |
| 177 | + expect(result.current.onRetry).toBe(mockCallback); |
| 178 | + }); |
| 179 | + |
| 180 | + it('allows onRetry callback to be called', () => { |
| 181 | + const { result } = renderHook(() => useWebSocketHealthToastContext(), { |
| 182 | + wrapper, |
| 183 | + }); |
| 184 | + |
| 185 | + const mockCallback = jest.fn(); |
| 186 | + |
| 187 | + act(() => { |
| 188 | + result.current.setOnRetry(mockCallback); |
| 189 | + }); |
| 190 | + |
| 191 | + // Call the registered callback |
| 192 | + act(() => { |
| 193 | + result.current.onRetry?.(); |
| 194 | + }); |
| 195 | + |
| 196 | + expect(mockCallback).toHaveBeenCalled(); |
| 197 | + }); |
| 198 | + |
| 199 | + it('allows updating the retry callback', () => { |
| 200 | + const { result } = renderHook(() => useWebSocketHealthToastContext(), { |
| 201 | + wrapper, |
| 202 | + }); |
| 203 | + |
| 204 | + const firstCallback = jest.fn(); |
| 205 | + const secondCallback = jest.fn(); |
| 206 | + |
| 207 | + act(() => { |
| 208 | + result.current.setOnRetry(firstCallback); |
| 209 | + }); |
| 210 | + |
| 211 | + act(() => { |
| 212 | + result.current.setOnRetry(secondCallback); |
| 213 | + }); |
| 214 | + |
| 215 | + expect(result.current.onRetry).toBe(secondCallback); |
| 216 | + }); |
| 217 | + }); |
| 218 | + |
| 219 | + describe('onRetry', () => { |
| 220 | + it('is undefined initially', () => { |
| 221 | + const { result } = renderHook(() => useWebSocketHealthToastContext(), { |
| 222 | + wrapper, |
| 223 | + }); |
| 224 | + |
| 225 | + expect(result.current.onRetry).toBeUndefined(); |
| 226 | + }); |
| 227 | + |
| 228 | + it('is accessible from context after setOnRetry', () => { |
| 229 | + const { result } = renderHook(() => useWebSocketHealthToastContext(), { |
| 230 | + wrapper, |
| 231 | + }); |
| 232 | + |
| 233 | + const mockCallback = jest.fn(); |
| 234 | + |
| 235 | + act(() => { |
| 236 | + result.current.setOnRetry(mockCallback); |
| 237 | + }); |
| 238 | + |
| 239 | + expect(result.current.onRetry).toBeDefined(); |
| 240 | + expect(typeof result.current.onRetry).toBe('function'); |
| 241 | + }); |
| 242 | + }); |
| 243 | + }); |
| 244 | + |
| 245 | + describe('Default context values', () => { |
| 246 | + it('has default noop functions in context', () => { |
| 247 | + // Test using context directly without provider |
| 248 | + const { result } = renderHook(() => |
| 249 | + React.useContext(WebSocketHealthToastContext), |
| 250 | + ); |
| 251 | + |
| 252 | + // Default values should exist and not throw |
| 253 | + expect(result.current.state).toEqual({ |
| 254 | + isVisible: false, |
| 255 | + connectionState: WebSocketConnectionState.DISCONNECTED, |
| 256 | + reconnectionAttempt: 0, |
| 257 | + }); |
| 258 | + |
| 259 | + // Default functions should be no-ops |
| 260 | + expect(() => { |
| 261 | + result.current.show(WebSocketConnectionState.CONNECTED); |
| 262 | + result.current.hide(); |
| 263 | + result.current.setOnRetry(() => undefined); |
| 264 | + }).not.toThrow(); |
| 265 | + |
| 266 | + // onRetry should be undefined by default |
| 267 | + expect(result.current.onRetry).toBeUndefined(); |
| 268 | + }); |
| 269 | + }); |
| 270 | +}); |
0 commit comments