|
| 1 | +import {act, renderHook} from '@testing-library/react-native'; |
| 2 | +import useInboxTabSpanLifecycle from '@hooks/useInboxTabSpanLifecycle'; |
| 3 | +import CONST from '@src/CONST'; |
| 4 | + |
| 5 | +type FocusCallback = () => (() => void) | void; |
| 6 | +type FakeSpan = {id: string}; |
| 7 | + |
| 8 | +const SPAN = CONST.TELEMETRY.SPAN_NAVIGATE_TO_INBOX_TAB; |
| 9 | + |
| 10 | +const mockUseFocusEffect = jest.fn<void, [FocusCallback]>(); |
| 11 | +const mockGetSpan = jest.fn<FakeSpan | undefined, [string]>(); |
| 12 | +const mockEndSpan = jest.fn<void, [string]>(); |
| 13 | +const mockCancelSpan = jest.fn<void, [string]>(); |
| 14 | + |
| 15 | +// The hook only consumes useFocusEffect from this module. We capture the focus callback |
| 16 | +// (via mockUseFocusEffect.mock.calls) so each test can drive focus/blur explicitly instead |
| 17 | +// of standing up a navigation container. |
| 18 | +jest.mock('@react-navigation/native', () => ({ |
| 19 | + useFocusEffect: (callback: FocusCallback) => { |
| 20 | + mockUseFocusEffect(callback); |
| 21 | + }, |
| 22 | +})); |
| 23 | + |
| 24 | +jest.mock('@libs/telemetry/activeSpans', () => ({ |
| 25 | + getSpan: (spanId: string) => mockGetSpan(spanId), |
| 26 | + endSpan: (spanId: string) => mockEndSpan(spanId), |
| 27 | + cancelSpan: (spanId: string) => mockCancelSpan(spanId), |
| 28 | +})); |
| 29 | + |
| 30 | +const spanAtMount: FakeSpan = {id: 'span-at-mount'}; |
| 31 | +const newerSpan: FakeSpan = {id: 'newer-span'}; |
| 32 | + |
| 33 | +/** The currently active span returned by getSpan. Defaults to the one present when the hook mounts. */ |
| 34 | +let currentActiveSpan: FakeSpan | undefined; |
| 35 | + |
| 36 | +/** Returns the latest focus callback the hook registered with useFocusEffect. */ |
| 37 | +function getFocusCallback(): FocusCallback { |
| 38 | + const callback = mockUseFocusEffect.mock.calls.at(-1)?.[0]; |
| 39 | + if (!callback) { |
| 40 | + throw new Error('useFocusEffect was never called by the hook'); |
| 41 | + } |
| 42 | + return callback; |
| 43 | +} |
| 44 | + |
| 45 | +describe('useInboxTabSpanLifecycle', () => { |
| 46 | + beforeEach(() => { |
| 47 | + jest.clearAllMocks(); |
| 48 | + currentActiveSpan = spanAtMount; |
| 49 | + mockGetSpan.mockImplementation(() => currentActiveSpan); |
| 50 | + }); |
| 51 | + |
| 52 | + it('ends the span when the sidebar lays out for the first time (normal path)', () => { |
| 53 | + const {result} = renderHook(() => useInboxTabSpanLifecycle()); |
| 54 | + |
| 55 | + act(() => { |
| 56 | + result.current(); |
| 57 | + }); |
| 58 | + |
| 59 | + expect(mockEndSpan).toHaveBeenCalledTimes(1); |
| 60 | + expect(mockEndSpan).toHaveBeenCalledWith(SPAN); |
| 61 | + expect(mockCancelSpan).not.toHaveBeenCalled(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('does not end the span on focus before the first layout (cold path)', () => { |
| 65 | + renderHook(() => useInboxTabSpanLifecycle()); |
| 66 | + |
| 67 | + act(() => { |
| 68 | + getFocusCallback()(); |
| 69 | + }); |
| 70 | + |
| 71 | + expect(mockEndSpan).not.toHaveBeenCalled(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('ends the span on re-focus after layout (warm react-freeze path)', () => { |
| 75 | + const {result} = renderHook(() => useInboxTabSpanLifecycle()); |
| 76 | + |
| 77 | + // First layout completes, then the user leaves and returns: react-freeze keeps the |
| 78 | + // layout cached so onLayout will not fire again, and focus must end the span instead. |
| 79 | + act(() => { |
| 80 | + result.current(); |
| 81 | + }); |
| 82 | + mockEndSpan.mockClear(); |
| 83 | + |
| 84 | + act(() => { |
| 85 | + getFocusCallback()(); |
| 86 | + }); |
| 87 | + |
| 88 | + expect(mockEndSpan).toHaveBeenCalledTimes(1); |
| 89 | + expect(mockEndSpan).toHaveBeenCalledWith(SPAN); |
| 90 | + }); |
| 91 | + |
| 92 | + it('cancels the span on blur when layout never completed (orphan cleanup)', () => { |
| 93 | + renderHook(() => useInboxTabSpanLifecycle()); |
| 94 | + |
| 95 | + const blurCleanup = getFocusCallback()(); |
| 96 | + |
| 97 | + expect(typeof blurCleanup).toBe('function'); |
| 98 | + act(() => { |
| 99 | + blurCleanup?.(); |
| 100 | + }); |
| 101 | + |
| 102 | + expect(mockCancelSpan).toHaveBeenCalledTimes(1); |
| 103 | + expect(mockCancelSpan).toHaveBeenCalledWith(SPAN); |
| 104 | + }); |
| 105 | + |
| 106 | + it('cancels the span on unmount when layout never completed and the active span is unchanged', () => { |
| 107 | + const {unmount} = renderHook(() => useInboxTabSpanLifecycle()); |
| 108 | + |
| 109 | + unmount(); |
| 110 | + |
| 111 | + expect(mockCancelSpan).toHaveBeenCalledTimes(1); |
| 112 | + expect(mockCancelSpan).toHaveBeenCalledWith(SPAN); |
| 113 | + }); |
| 114 | + |
| 115 | + it('does not cancel on unmount when a newer span has started (subsequent tab click)', () => { |
| 116 | + const {unmount} = renderHook(() => useInboxTabSpanLifecycle()); |
| 117 | + |
| 118 | + // A later tab click started a fresh span before this instance unmounted. The unmount |
| 119 | + // cleanup must not cancel that newer span. |
| 120 | + currentActiveSpan = newerSpan; |
| 121 | + unmount(); |
| 122 | + |
| 123 | + expect(mockCancelSpan).not.toHaveBeenCalled(); |
| 124 | + }); |
| 125 | + |
| 126 | + it('does not cancel on unmount after layout has completed', () => { |
| 127 | + const {result, unmount} = renderHook(() => useInboxTabSpanLifecycle()); |
| 128 | + |
| 129 | + act(() => { |
| 130 | + result.current(); |
| 131 | + }); |
| 132 | + mockCancelSpan.mockClear(); |
| 133 | + |
| 134 | + unmount(); |
| 135 | + |
| 136 | + expect(mockCancelSpan).not.toHaveBeenCalled(); |
| 137 | + }); |
| 138 | +}); |
0 commit comments