|
| 1 | +import getTopmostFullScreenRoute from '@libs/Navigation/helpers/getTopmostFullScreenRoute'; |
| 2 | +import NAVIGATORS from '@src/NAVIGATORS'; |
| 3 | + |
| 4 | +const mockGetRootState = jest.fn(); |
| 5 | + |
| 6 | +jest.mock('@libs/Navigation/Navigation', () => ({ |
| 7 | + navigationRef: { |
| 8 | + getRootState: () => mockGetRootState() as unknown, |
| 9 | + }, |
| 10 | +})); |
| 11 | + |
| 12 | +describe('getTopmostFullScreenRoute', () => { |
| 13 | + beforeEach(() => { |
| 14 | + mockGetRootState.mockReset(); |
| 15 | + }); |
| 16 | + |
| 17 | + it('returns undefined when there is no root state', () => { |
| 18 | + mockGetRootState.mockReturnValue(undefined); |
| 19 | + expect(getTopmostFullScreenRoute()).toBeUndefined(); |
| 20 | + }); |
| 21 | + |
| 22 | + it('returns undefined when there is no TAB_NAVIGATOR route in the root state', () => { |
| 23 | + mockGetRootState.mockReturnValue({ |
| 24 | + routes: [{name: NAVIGATORS.RIGHT_MODAL_NAVIGATOR}, {name: NAVIGATORS.SETTINGS_SPLIT_NAVIGATOR}], |
| 25 | + }); |
| 26 | + expect(getTopmostFullScreenRoute()).toBeUndefined(); |
| 27 | + }); |
| 28 | + |
| 29 | + it('returns undefined when the TAB_NAVIGATOR has no nested state yet', () => { |
| 30 | + mockGetRootState.mockReturnValue({ |
| 31 | + routes: [{name: NAVIGATORS.TAB_NAVIGATOR}], |
| 32 | + }); |
| 33 | + expect(getTopmostFullScreenRoute()).toBeUndefined(); |
| 34 | + }); |
| 35 | + |
| 36 | + it('returns the focused tab route based on state.index', () => { |
| 37 | + const reportsRoute = {name: NAVIGATORS.REPORTS_SPLIT_NAVIGATOR}; |
| 38 | + const searchRoute = {name: NAVIGATORS.SEARCH_FULLSCREEN_NAVIGATOR}; |
| 39 | + mockGetRootState.mockReturnValue({ |
| 40 | + routes: [ |
| 41 | + { |
| 42 | + name: NAVIGATORS.TAB_NAVIGATOR, |
| 43 | + state: { |
| 44 | + index: 1, |
| 45 | + routes: [reportsRoute, searchRoute], |
| 46 | + }, |
| 47 | + }, |
| 48 | + ], |
| 49 | + }); |
| 50 | + expect(getTopmostFullScreenRoute()).toBe(searchRoute); |
| 51 | + }); |
| 52 | + |
| 53 | + it('falls back to the first child route when state.index is missing', () => { |
| 54 | + const reportsRoute = {name: NAVIGATORS.REPORTS_SPLIT_NAVIGATOR}; |
| 55 | + const searchRoute = {name: NAVIGATORS.SEARCH_FULLSCREEN_NAVIGATOR}; |
| 56 | + mockGetRootState.mockReturnValue({ |
| 57 | + routes: [ |
| 58 | + { |
| 59 | + name: NAVIGATORS.TAB_NAVIGATOR, |
| 60 | + state: { |
| 61 | + routes: [reportsRoute, searchRoute], |
| 62 | + }, |
| 63 | + }, |
| 64 | + ], |
| 65 | + }); |
| 66 | + expect(getTopmostFullScreenRoute()).toBe(reportsRoute); |
| 67 | + }); |
| 68 | + |
| 69 | + it('returns the focused tab of the topmost TAB_NAVIGATOR when multiple exist', () => { |
| 70 | + const oldFocused = {name: NAVIGATORS.REPORTS_SPLIT_NAVIGATOR}; |
| 71 | + const newFocused = {name: NAVIGATORS.SEARCH_FULLSCREEN_NAVIGATOR}; |
| 72 | + mockGetRootState.mockReturnValue({ |
| 73 | + routes: [ |
| 74 | + { |
| 75 | + name: NAVIGATORS.TAB_NAVIGATOR, |
| 76 | + state: { |
| 77 | + index: 0, |
| 78 | + routes: [oldFocused], |
| 79 | + }, |
| 80 | + }, |
| 81 | + {name: NAVIGATORS.RIGHT_MODAL_NAVIGATOR}, |
| 82 | + { |
| 83 | + name: NAVIGATORS.TAB_NAVIGATOR, |
| 84 | + state: { |
| 85 | + index: 0, |
| 86 | + routes: [newFocused], |
| 87 | + }, |
| 88 | + }, |
| 89 | + ], |
| 90 | + }); |
| 91 | + expect(getTopmostFullScreenRoute()).toBe(newFocused); |
| 92 | + }); |
| 93 | +}); |
0 commit comments