import { CommonActions } from '@react-navigation/native' import parametrize from 'js-parametrize' import { marbles } from 'rxjs-marbles/jest' import { appSlice } from '../../modules/App' import { actions as fromActions } from './index' import * as SUT from './routing.epics' import routingService from './routing.service' jest.mock('@react-navigation/native', () => { const actualNav = jest.requireActual('@react-navigation/native') return { ...actualNav, useNavigation: () => ({ navigate: jest.fn(), dispatch: jest.fn(), }), CommonActions: { navigate: jest.fn().mockImplementation(x => ({ type: 'NAVIGATE', payload: { name: x, }, })), }, } }) describe('routingEpics', () => { afterAll(() => { jest.unmock('@react-navigation/native') }) describe('navigateEpic', () => { it( 'should dispatch a navigation action to react navigation and return a RoutingInitialized Action with the relevant params', marbles(m => { // given ... we have mocked our navigator and set it const navigator = { dispatch: jest.fn(), } routingService.setTopLevelNavigator(navigator as any) const values: any = { a: CommonActions.navigate('test', {}), b: fromActions.navigationInitiated('test', {}), } const action$ = m.cold('---a', values) const expected = '---b' // when ... we call our navigateEpic const destination$ = SUT.navigateEpic(action$) // then ... the action should be returned as expected m.expect(destination$).toBeObservable(expected, values) }), ) }) describe('routingEpic', () => { parametrize( [[appSlice.actions.initAppFailure.toString(), 'InitError']], (actionType: string, expectedRouteName: string) => { it( 'should map action to route as expected and navigate', marbles(m => { const values: any = { a: { type: actionType }, b: CommonActions.navigate(expectedRouteName, {}), } const action$ = m.cold('---a', values) const expected = '---b' // when ... we handle app routing const destination$ = SUT.routingEpic(action$) // then ... should map action to route as expected and navigate m.expect(destination$).toBeObservable(expected, values) }), ) }, ) }) })