|
| 1 | +import { renderHook } from '@testing-library/react'; |
| 2 | +import React, { useRef } from 'react'; |
| 3 | +import { AppState } from 'react-native'; |
| 4 | + |
| 5 | +import { AutoEnvAttributes, debounce } from '@launchdarkly/js-client-sdk-common'; |
| 6 | +import { logger } from '@launchdarkly/private-js-mocks'; |
| 7 | + |
| 8 | +import EventSource from '../fromExternal/react-native-sse'; |
| 9 | +import ReactNativeLDClient from '../ReactNativeLDClient'; |
| 10 | +import useAppState from './useAppState'; |
| 11 | + |
| 12 | +jest.mock('@launchdarkly/js-client-sdk-common', () => { |
| 13 | + const actual = jest.requireActual('@launchdarkly/js-client-sdk-common'); |
| 14 | + return { |
| 15 | + ...actual, |
| 16 | + debounce: jest.fn(), |
| 17 | + }; |
| 18 | +}); |
| 19 | + |
| 20 | +describe('useAppState', () => { |
| 21 | + const eventSourceOpen = 1; |
| 22 | + const eventSourceClosed = 2; |
| 23 | + |
| 24 | + let appStateSpy: jest.SpyInstance; |
| 25 | + let ldc: ReactNativeLDClient; |
| 26 | + let mockEventSource: Partial<EventSource>; |
| 27 | + |
| 28 | + beforeEach(() => { |
| 29 | + (debounce as jest.Mock).mockImplementation((f) => f); |
| 30 | + appStateSpy = jest.spyOn(AppState, 'addEventListener').mockReturnValue({ remove: jest.fn() }); |
| 31 | + jest.spyOn(React, 'useRef').mockReturnValue({ |
| 32 | + current: 'active', |
| 33 | + }); |
| 34 | + |
| 35 | + ldc = new ReactNativeLDClient('mob-test-key', AutoEnvAttributes.Enabled, { logger }); |
| 36 | + |
| 37 | + mockEventSource = { |
| 38 | + getStatus: jest.fn(() => eventSourceOpen), |
| 39 | + OPEN: eventSourceOpen, |
| 40 | + CLOSED: eventSourceClosed, |
| 41 | + }; |
| 42 | + // @ts-ignore |
| 43 | + ldc.platform.requests = { eventSource: mockEventSource }; |
| 44 | + // @ts-ignore |
| 45 | + ldc.streamer = { start: jest.fn().mockName('start'), stop: jest.fn().mockName('stop') }; |
| 46 | + }); |
| 47 | + |
| 48 | + afterEach(() => { |
| 49 | + jest.resetAllMocks(); |
| 50 | + }); |
| 51 | + |
| 52 | + test('stops streamer in background', () => { |
| 53 | + renderHook(() => useAppState(ldc)); |
| 54 | + const onChange = appStateSpy.mock.calls[0][1]; |
| 55 | + |
| 56 | + onChange('background'); |
| 57 | + |
| 58 | + expect(ldc.streamer?.stop).toHaveBeenCalledTimes(1); |
| 59 | + }); |
| 60 | + |
| 61 | + test('starts streamer transitioning from background to active', () => { |
| 62 | + (useRef as jest.Mock).mockReturnValue({ current: 'background' }); |
| 63 | + (mockEventSource.getStatus as jest.Mock).mockReturnValue(eventSourceClosed); |
| 64 | + |
| 65 | + renderHook(() => useAppState(ldc)); |
| 66 | + const onChange = appStateSpy.mock.calls[0][1]; |
| 67 | + |
| 68 | + onChange('active'); |
| 69 | + |
| 70 | + expect(ldc.streamer?.start).toHaveBeenCalledTimes(1); |
| 71 | + expect(ldc.streamer?.stop).not.toHaveBeenCalled(); |
| 72 | + }); |
| 73 | + |
| 74 | + test('starts streamer transitioning from inactive to active', () => { |
| 75 | + (useRef as jest.Mock).mockReturnValue({ current: 'inactive' }); |
| 76 | + (mockEventSource.getStatus as jest.Mock).mockReturnValue(eventSourceClosed); |
| 77 | + |
| 78 | + renderHook(() => useAppState(ldc)); |
| 79 | + const onChange = appStateSpy.mock.calls[0][1]; |
| 80 | + |
| 81 | + onChange('active'); |
| 82 | + |
| 83 | + expect(ldc.streamer?.start).toHaveBeenCalledTimes(1); |
| 84 | + expect(ldc.streamer?.stop).not.toHaveBeenCalled(); |
| 85 | + }); |
| 86 | + |
| 87 | + test('does not start streamer in foreground because event source is already open', () => { |
| 88 | + (useRef as jest.Mock).mockReturnValue({ current: 'background' }); |
| 89 | + (mockEventSource.getStatus as jest.Mock).mockReturnValue(eventSourceOpen); |
| 90 | + |
| 91 | + renderHook(() => useAppState(ldc)); |
| 92 | + const onChange = appStateSpy.mock.calls[0][1]; |
| 93 | + |
| 94 | + onChange('active'); |
| 95 | + |
| 96 | + expect(ldc.streamer?.start).not.toHaveBeenCalled(); |
| 97 | + expect(ldc.streamer?.stop).not.toHaveBeenCalled(); |
| 98 | + expect(ldc.logger.debug).toHaveBeenCalledWith(expect.stringMatching(/already open/)); |
| 99 | + }); |
| 100 | + |
| 101 | + test('active state unchanged no action needed', () => { |
| 102 | + (useRef as jest.Mock).mockReturnValue({ current: 'active' }); |
| 103 | + (mockEventSource.getStatus as jest.Mock).mockReturnValue(eventSourceClosed); |
| 104 | + |
| 105 | + renderHook(() => useAppState(ldc)); |
| 106 | + const onChange = appStateSpy.mock.calls[0][1]; |
| 107 | + |
| 108 | + onChange('active'); |
| 109 | + |
| 110 | + expect(ldc.streamer?.start).not.toHaveBeenCalled(); |
| 111 | + expect(ldc.streamer?.stop).not.toHaveBeenCalled(); |
| 112 | + expect(ldc.logger.debug).toHaveBeenCalledWith(expect.stringMatching(/no action needed/i)); |
| 113 | + }); |
| 114 | +}); |
0 commit comments