Skip to content

Commit b7c6a24

Browse files
committed
feat: Cleaned up navigation context logic
1 parent 4e3ae09 commit b7c6a24

5 files changed

Lines changed: 23 additions & 68 deletions

File tree

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { useNavigation, useRoute } from '@react-navigation/native';
2-
31
import { NavigationActionsProps } from '@bluebase/components';
4-
import { navigationToActionObject } from '../../helpers';
2+
import { useNavigation } from '@bluebase/core';
53

64
/**
75
* NavigationActions (Legacy)
@@ -14,7 +12,6 @@ export const NavigationActions: React.ComponentType<NavigationActionsProps> = ({
1412
children,
1513
}: NavigationActionsProps) => {
1614
const navigation = useNavigation();
17-
const route = useRoute();
1815

19-
return children(navigationToActionObject(navigation, route)) as any;
16+
return children(navigation) as any;
2017
};
Lines changed: 15 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,28 @@
1-
const mockedNavigation: any = {
2-
addListener: jest.fn(),
3-
closeDrawer: jest.fn(),
4-
dangerouslyGetParent: jest.fn(),
5-
dismiss: jest.fn(),
6-
dispatch: jest.fn(),
7-
getParam: jest.fn(),
8-
goBack: jest.fn(),
9-
isFocused: jest.fn(),
10-
navigate: jest.fn(),
11-
openDrawer: jest.fn(),
12-
pop: jest.fn(),
13-
popToTop: jest.fn(),
14-
push: jest.fn(),
15-
replace: jest.fn(),
16-
setParams: jest.fn(),
17-
toggleDrawer: jest.fn(),
18-
};
19-
20-
const mockedRoute = {
21-
index: 0,
22-
isTransitioning: false,
23-
key: 'kjdkj',
24-
name: 'Home',
25-
params: { foo: 'bar' },
26-
path: '/',
27-
routes: [
28-
{
29-
index: 1,
30-
isTransitioning: false,
31-
key: 'jsdfl',
32-
path: '/settings',
33-
routeName: 'Settings',
34-
routes: [],
35-
},
36-
],
37-
};
38-
39-
jest.mock('@react-navigation/native', () => {
40-
const actual = jest.requireActual('@react-navigation/native');
41-
return {
42-
...actual,
43-
44-
useNavigation: () => mockedNavigation,
45-
useRoute: () => mockedRoute,
46-
};
47-
});
48-
491
import { NavigationActions } from '../NavigationActions';
2+
import { NavigationContext } from '@bluebase/core';
503
import React from 'react';
514
import { mount } from 'enzyme';
525

536
describe('NavigationActions tests', () => {
547
it('should check props', async () => {
558
const children = jest.fn().mockReturnValue(null);
56-
57-
mount(<NavigationActions>{children}</NavigationActions>);
9+
const nav: any = {
10+
state: {
11+
name: 'Home',
12+
params: { foo: 'bar' },
13+
path: '/',
14+
},
15+
};
16+
17+
mount(
18+
<NavigationContext.Provider value={nav}>
19+
<NavigationActions>{children}</NavigationActions>
20+
</NavigationContext.Provider>
21+
);
5822

5923
expect(children).toHaveBeenCalledTimes(1);
6024

6125
const result = children.mock.calls[0][0];
62-
expect(result.state.key).toBe('kjdkj');
63-
expect(result.state.routeName).toBe('Home');
64-
expect(result.state.params).toMatchObject({ foo: 'bar' });
65-
expect(result.source).toBeTruthy();
26+
expect(result).toMatchObject(nav);
6627
});
6728
});

src/components/NavigationProvider/NavigationProvider.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import { navigationToActionObject } from '../../helpers';
99
* and injects it into the context
1010
* @param param
1111
*/
12-
export const NavigationProvider = ({ children }: { children: React.ReactNode }) => {
12+
export const NavigationProvider = (props: { children: React.ReactNode }) => {
13+
const { children } = props;
1314
const navigation = useNavigation();
1415
const route = useRoute();
1516

src/components/ScreenView/ScreenView.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import { useNavigation, useRoute } from '@react-navigation/native';
2-
31
import React from 'react';
42
import { RouteConfig } from '@bluebase/components';
5-
import { navigationToActionObject } from '../../helpers';
3+
import { useNavigation } from '@bluebase/core';
64

75
export interface ScreenViewProps {
86
children?: React.ReactNode;
@@ -16,9 +14,7 @@ export const ScreenView: React.ComponentType<ScreenViewProps> = ({
1614
ScreenComponent,
1715
...rest
1816
}: ScreenViewProps & any) => {
19-
const state = useRoute();
20-
const navObj = useNavigation();
21-
const navigation = navigationToActionObject(navObj, state);
17+
const navigation = useNavigation();
2218

2319
return (
2420
<ScreenComponent {...rest} navigation={navigation}>

src/helpers/createNavigatorScreenComponent.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const createNavigatorScreenComponent = (route: RouteConfig, BB: BlueBase)
2222

2323
if (screen) {
2424
const WrappedNavigator = (props: any) => (
25-
<NavigationProvider>
25+
<NavigationProvider {...props}>
2626
<ScreenComponent {...props} route={route} ScreenComponent={ScreenComponent}>
2727
{navigatorNode}
2828
</ScreenComponent>
@@ -36,7 +36,7 @@ export const createNavigatorScreenComponent = (route: RouteConfig, BB: BlueBase)
3636
}
3737

3838
const Screen = (props: any) => (
39-
<NavigationProvider>
39+
<NavigationProvider {...props}>
4040
<ScreenComponent {...props} route={route} ScreenComponent={ScreenComponent} />
4141
</NavigationProvider>
4242
);

0 commit comments

Comments
 (0)