Skip to content

Commit

Permalink
add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
marklawlor committed Mar 14, 2024
1 parent c19d914 commit b599c91
Showing 1 changed file with 124 additions and 3 deletions.
127 changes: 124 additions & 3 deletions packages/expo-router/src/__tests__/push.test.ios.tsx
@@ -1,5 +1,5 @@
import React from 'react';
import { Text } from 'react-native';
import { Text, View } from 'react-native';

import { store } from '../global-state/router-store';
import { useLocalSearchParams } from '../hooks';
Expand All @@ -9,13 +9,13 @@ import Tabs from '../layouts/Tabs';
import { act, renderRouter, screen, testRouter } from '../testing-library';
import { Slot } from '../views/Navigator';

it.only('stacks should always push a new route', () => {
it('stacks should always push a new route', () => {
renderRouter({
index: () => null,
'(group)/_layout': () => <Stack />,
'(group)/user/[id]/_layout': () => <Stack />,
'(group)/user/[id]/index': function User() {
return <Text testID={JSON.stringify(useLocalSearchParams())} />;
return <View testID={JSON.stringify(useLocalSearchParams())} />;
},
'(group)/post/[id]/_layout': () => <Stack />,
'(group)/post/[id]/index': () => null,
Expand Down Expand Up @@ -312,3 +312,124 @@ it('works in a nested layout Stack->Tab->Stack', () => {
type: 'stack',
});
});

it('targets the correct Stack when pushing to a nested layout', () => {
renderRouter(
{
_layout: () => <Stack />,
a: () => null,
b: () => null,
'one/_layout': () => <Stack />,
'one/index': () => <View testID="one" />,
'one/page': () => <View testID="one/page" />,
'one/two/_layout': () => <Stack />,
'one/two/index': () => <View testID="one/two" />,
'one/two/page': () => <View testID="one/two/page" />,
},
{
initialUrl: '/a',
}
);

act(() => router.push('/b')); // Should at at index 1 on the root stack

act(() => router.push('/one')); // Should at at index 2 on the root stack
expect(screen.getByTestId('one')).toBeOnTheScreen();

act(() => router.push('/one/page')); // Should at at index 1, nested inside index 2 on the root stack
expect(screen.getByTestId('one/page')).toBeOnTheScreen();

act(() => router.push('/one/two')); // Should at at index 2, nested inside index 2 on the root stack
expect(screen.getByTestId('one/two')).toBeOnTheScreen();

act(() => router.push('/one/two/page')); // Should at at index 1, nested inside index 2, inside index 2 on the root stack
expect(screen.getByTestId('one/two/page')).toBeOnTheScreen();

act(() => router.push('/a')); // Should push to the root stack

expect(store.rootStateSnapshot()).toStrictEqual({
index: 3,
key: expect.any(String),
routeNames: ['a', 'b', 'one', '_sitemap', '+not-found'],
routes: [
{
key: expect.any(String),
name: 'a',
params: undefined,
path: '/a',
},
{
key: expect.any(String),
name: 'b',
params: {},
path: undefined,
},
{
key: expect.any(String),
name: 'one',
params: {
params: {},
screen: 'index',
},
path: undefined,
state: {
index: 2,
key: expect.any(String),
routeNames: ['index', 'two', 'page'],
routes: [
{
key: expect.any(String),
name: 'index',
params: {},
},
{
key: expect.any(String),
name: 'page',
params: {},
path: undefined,
},
{
key: expect.any(String),
name: 'two',
params: {
params: {},
screen: 'index',
},
path: undefined,
state: {
index: 1,
key: expect.any(String),
routeNames: ['index', 'page'],
routes: [
{
key: expect.any(String),
name: 'index',
params: {},
},
{
key: expect.any(String),
name: 'page',
params: {},
path: undefined,
},
],
stale: false,
type: 'stack',
},
},
],
stale: false,
type: 'stack',
},
},
{
key: expect.any(String),
name: 'a',
params: {},
path: undefined,
},
],
stale: false,
type: 'stack',
});
});

0 comments on commit b599c91

Please sign in to comment.