Skip to content

Commit

Permalink
Merge branch 'main' into fix/new-md-styles
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 committed Mar 3, 2024
2 parents c5def64 + 83921e9 commit 8ded88e
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 8 deletions.
3 changes: 0 additions & 3 deletions packages/core/src/BaseNavigationContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,6 @@ export const BaseNavigationContainer = React.forwardRef(
getCurrentRoute,
getCurrentOptions,
isReady,
setParams: () => {
throw new Error('Cannot call setParams outside a screen');
},
setOptions: () => {
throw new Error('Cannot call setOptions outside a screen');
},
Expand Down
4 changes: 0 additions & 4 deletions packages/core/src/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -779,10 +779,6 @@ export type NavigationContainerRef<ParamList extends {}> =
* Stub function for setOptions on navigation object for use with useNavigation.
*/
setOptions(): never;
/**
* Stub function for setParams on navigation object for use with useNavigation.
*/
setParams(): never;
/**
* Stub function for getParent on navigation object for use with useNavigation.
*/
Expand Down
148 changes: 148 additions & 0 deletions packages/native/src/__tests__/createStaticNavigation.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import {
createNavigationContainerRef,
createNavigatorFactory,
type ParamListBase,
StackRouter,
TabRouter,
useNavigationBuilder,
} from '@react-navigation/core';
import { act, render, waitFor } from '@testing-library/react-native';
import * as React from 'react';

import { window } from '../__stubs__/window';
import { createStaticNavigation } from '../createStaticNavigation';

Object.assign(global, window);

// We want to use the web version of useLinking
// eslint-disable-next-line import/extensions
jest.mock('../useLinking', () => require('../useLinking.tsx'));

it('integrates with the history API', async () => {
const createStackNavigator = createNavigatorFactory((props: any) => {
const { state, descriptors, NavigationContent } = useNavigationBuilder(
StackRouter,
props
);

return (
<NavigationContent>
{state.routes.map((route, i) => (
<div key={route.key} aria-current={state.index === i || undefined}>
{descriptors[route.key].render()}
</div>
))}
</NavigationContent>
);
});

const createTabNavigator = createNavigatorFactory((props: any) => {
const { state, descriptors, NavigationContent } = useNavigationBuilder(
TabRouter,
props
);

return (
<NavigationContent>
{state.routes.map((route, i) => (
<div key={route.key} aria-current={state.index === i || undefined}>
{descriptors[route.key].render()}
</div>
))}
</NavigationContent>
);
});

const TestScreen = ({ route }: any): any =>
`${route.name} ${JSON.stringify(route.params)}`;

const Stack = createStackNavigator({
initialRouteName: 'Feed',
screens: {
Profile: {
screen: TestScreen,
linking: ':user',
},
Settings: {
screen: TestScreen,
linking: 'edit',
},
Updates: {
screen: TestScreen,
linking: 'updates',
},
Feed: {
screen: TestScreen,
linking: 'feed',
},
},
});

const Tab = createTabNavigator({
screens: {
Home: Stack,
Chat: {
screen: TestScreen,
linking: 'chat',
},
},
});

const Navigation = createStaticNavigation(Tab);

const navigation = createNavigationContainerRef<ParamListBase>();

render(
<Navigation
ref={navigation}
linking={{
prefixes: [],
}}
/>
);

expect(window.location.pathname).toBe('/feed');

act(() => navigation.current?.navigate('Profile', { user: 'jane' }));

await waitFor(() => expect(window.location.pathname).toBe('/jane'));

act(() => navigation.current?.navigate('Updates'));

await waitFor(() => expect(window.location.pathname).toBe('/updates'));

act(() => navigation.current?.goBack());

await waitFor(() => expect(window.location.pathname).toBe('/jane'));

act(() => {
window.history.back();
});

await waitFor(() => expect(window.location.pathname).toBe('/feed'));

act(() => {
window.history.forward();
});

await waitFor(() => expect(window.location.pathname).toBe('/jane'));

act(() => navigation.current?.navigate('Settings'));

await waitFor(() => expect(window.location.pathname).toBe('/edit'));

act(() => {
window.history.go(-2);
});

await waitFor(() => expect(window.location.pathname).toBe('/feed'));

act(() => navigation.current?.navigate('Settings'));
act(() => navigation.current?.navigate('Chat'));

await waitFor(() => expect(window.location.pathname).toBe('/chat'));

act(() => navigation.current?.navigate('Home'));

await waitFor(() => expect(window.location.pathname).toBe('/edit'));
});
7 changes: 6 additions & 1 deletion packages/native/src/createStaticNavigation.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
createComponentForStaticNavigation,
createPathConfigForStaticNavigation,
type NavigationContainerRef,
type ParamListBase,
type StaticNavigation,
} from '@react-navigation/core';
Expand Down Expand Up @@ -34,10 +35,14 @@ export function createStaticNavigation(tree: StaticNavigation<any, any, any>) {
: {},
};

function Navigation({ linking, ...rest }: Props) {
function Navigation(
{ linking, ...rest }: Props,
ref: React.Ref<NavigationContainerRef<ParamListBase>>
) {
return (
<NavigationContainer
{...rest}
ref={ref}
linking={linking ? { ...linking, config: linkingConfig } : undefined}
>
<Component />
Expand Down

0 comments on commit 8ded88e

Please sign in to comment.