|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @flow strict-local |
| 8 | + * @format |
| 9 | + */ |
| 10 | + |
| 11 | +import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment'; |
| 12 | + |
| 13 | +import View from '../../Components/View/View'; |
| 14 | +import AppState, {type AppStateStatus} from '../AppState'; |
| 15 | +import useAppState from '../useAppState'; |
| 16 | +import * as Fantom from '@react-native/fantom'; |
| 17 | +import * as React from 'react'; |
| 18 | + |
| 19 | +describe('useAppState', () => { |
| 20 | + const originalAddEventListener = AppState.addEventListener; |
| 21 | + const originalCurrentState = AppState.currentState; |
| 22 | + |
| 23 | + let observed: AppStateStatus | void; |
| 24 | + |
| 25 | + afterEach(() => { |
| 26 | + // $FlowFixMe[cannot-write] |
| 27 | + AppState.addEventListener = originalAddEventListener; |
| 28 | + AppState.currentState = originalCurrentState; |
| 29 | + observed = undefined; |
| 30 | + }); |
| 31 | + |
| 32 | + function mockSubscription() { |
| 33 | + const handlers: Array<(AppStateStatus) => void> = []; |
| 34 | + const remove = jest.fn(); |
| 35 | + const addEventListener = jest.fn( |
| 36 | + (type: string, handler: AppStateStatus => void) => { |
| 37 | + handlers.push(handler); |
| 38 | + return {remove}; |
| 39 | + }, |
| 40 | + ); |
| 41 | + // $FlowFixMe[cannot-write] |
| 42 | + AppState.addEventListener = addEventListener; |
| 43 | + return {addEventListener, handlers, remove}; |
| 44 | + } |
| 45 | + |
| 46 | + function renderHook() { |
| 47 | + function TestComponent() { |
| 48 | + observed = useAppState(); |
| 49 | + return <View />; |
| 50 | + } |
| 51 | + |
| 52 | + const root = Fantom.createRoot(); |
| 53 | + Fantom.runTask(() => { |
| 54 | + root.render(<TestComponent />); |
| 55 | + }); |
| 56 | + return root; |
| 57 | + } |
| 58 | + |
| 59 | + for (const state of ['active', 'background', 'inactive', 'extension']) { |
| 60 | + it(`returns '${state}' when AppState reports '${state}'`, () => { |
| 61 | + mockSubscription(); |
| 62 | + AppState.currentState = state; |
| 63 | + |
| 64 | + const root = renderHook(); |
| 65 | + |
| 66 | + expect(observed).toBe(state); |
| 67 | + |
| 68 | + root.destroy(); |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + it("returns 'unknown' when the current state is not yet known", () => { |
| 73 | + mockSubscription(); |
| 74 | + AppState.currentState = null; |
| 75 | + |
| 76 | + const root = renderHook(); |
| 77 | + |
| 78 | + expect(observed).toBe('unknown'); |
| 79 | + |
| 80 | + root.destroy(); |
| 81 | + }); |
| 82 | + |
| 83 | + it('subscribes to changes and re-renders with the new state', () => { |
| 84 | + const {addEventListener, handlers} = mockSubscription(); |
| 85 | + AppState.currentState = 'active'; |
| 86 | + |
| 87 | + const root = renderHook(); |
| 88 | + |
| 89 | + expect(addEventListener.mock.calls.length).toBe(1); |
| 90 | + expect(addEventListener.mock.calls[0][0]).toBe('change'); |
| 91 | + expect(typeof addEventListener.mock.calls[0][1]).toBe('function'); |
| 92 | + expect(observed).toBe('active'); |
| 93 | + |
| 94 | + AppState.currentState = 'background'; |
| 95 | + Fantom.runTask(() => { |
| 96 | + handlers.forEach(handler => handler('background')); |
| 97 | + }); |
| 98 | + expect(observed).toBe('background'); |
| 99 | + |
| 100 | + root.destroy(); |
| 101 | + }); |
| 102 | + |
| 103 | + it('unsubscribes when the component unmounts', () => { |
| 104 | + const {remove} = mockSubscription(); |
| 105 | + AppState.currentState = 'active'; |
| 106 | + |
| 107 | + const root = renderHook(); |
| 108 | + expect(observed).toBe('active'); |
| 109 | + |
| 110 | + root.destroy(); |
| 111 | + Fantom.runWorkLoop(); |
| 112 | + |
| 113 | + expect(remove).toHaveBeenCalledTimes(1); |
| 114 | + }); |
| 115 | + |
| 116 | + it('throws when called outside of a component', () => { |
| 117 | + let error; |
| 118 | + try { |
| 119 | + // $FlowFixMe[react-rule-hook] |
| 120 | + useAppState(); |
| 121 | + } catch (e) { |
| 122 | + error = e; |
| 123 | + } |
| 124 | + |
| 125 | + expect(error).toBeInstanceOf(Error); |
| 126 | + }); |
| 127 | +}); |
0 commit comments