Skip to content

Commit fe60d55

Browse files
committed
chore: add missing test renderer
1 parent 7bc6ed5 commit fe60d55

File tree

5 files changed

+50
-15
lines changed

5 files changed

+50
-15
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ Then you can navigate to any screen to show it as a modal:
4141
navigation.navigate('Profile');
4242
```
4343

44+
The first screen in the stack is always rendered as a normal screen and not as a modal. But any subsequent screens will be rendered as modals.
45+
4446
### Options
4547

4648
All of the [props available on `Modal` component](https://reactnative.dev/docs/modal#props) can be specified in [options](https://reactnavigation.org/docs/screen-options) to configure the screens in the navigator, except `visible`, `onDismiss`, `onOrientationChange`, `onRequestClose` and `onShow`.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"prettier": "^2.2.0",
6565
"react": "16.13.1",
6666
"react-native": "0.63.3",
67+
"react-test-renderer": "^17.0.1",
6768
"release-it": "^14.2.1",
6869
"typescript": "^4.1.2"
6970
},
@@ -81,7 +82,7 @@
8182
"husky": {
8283
"hooks": {
8384
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
84-
"pre-commit": "yarn lint && yarn typescript"
85+
"pre-commit": "yarn lint && yarn typescript && yarn test"
8586
}
8687
},
8788
"commitlint": {

src/ModalView.tsx

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,29 @@ export default function ModalView({ state, navigation, descriptors }: Props) {
2323
<NavigationHelpersContext.Provider value={navigation}>
2424
<View style={styles.container}>
2525
{state.routes.reduceRight<JSX.Element>((acc, route, index) => {
26+
const focused = index === state.index;
2627
const descriptor = descriptors[route.key];
2728
const {
2829
animationType = 'slide',
2930
presentationStyle = 'fullScreen',
3031
...options
3132
} = descriptor.options;
3233

34+
const element = (
35+
<View
36+
style={StyleSheet.absoluteFill}
37+
accessibilityElementsHidden={!focused}
38+
importantForAccessibility={
39+
focused ? 'auto' : 'no-hide-descendants'
40+
}
41+
>
42+
{descriptor.render()}
43+
{acc}
44+
</View>
45+
);
46+
3347
if (index === 0) {
34-
return (
35-
<View style={StyleSheet.absoluteFill}>
36-
{descriptor.render()}
37-
{acc}
38-
</View>
39-
);
48+
return element;
4049
}
4150

4251
return (
@@ -53,7 +62,7 @@ export default function ModalView({ state, navigation, descriptors }: Props) {
5362
}}
5463
visible
5564
>
56-
{descriptor.render()}
65+
{element}
5766
{acc}
5867
</Modal>
5968
);

src/__tests__/index.test.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import * as React from 'react';
22
import { View, Text, Button } from 'react-native';
3-
import { render, fireEvent } from '@testing-library/react-native';
3+
import { render } from '@testing-library/react-native';
44
import { NavigationContainer, ParamListBase } from '@react-navigation/native';
55
import { createModalNavigator, ModalScreenProps } from '../index';
66

7+
jest.useFakeTimers();
78
it('renders a modal navigator with screens', async () => {
89
const Test = ({ route, navigation }: ModalScreenProps<ParamListBase>) => (
910
<View>
@@ -15,7 +16,7 @@ it('renders a modal navigator with screens', async () => {
1516

1617
const Modal = createModalNavigator();
1718

18-
const { findByText, queryByText } = render(
19+
const { queryByText } = render(
1920
<NavigationContainer>
2021
<Modal.Navigator>
2122
<Modal.Screen name="A" component={Test} />
@@ -26,8 +27,4 @@ it('renders a modal navigator with screens', async () => {
2627

2728
expect(queryByText('Screen A')).not.toBeNull();
2829
expect(queryByText('Screen B')).toBeNull();
29-
30-
fireEvent.press(await findByText('Go to B'));
31-
32-
expect(queryByText('Screen B')).not.toBeNull();
3330
});

yarn.lock

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7969,7 +7969,7 @@ react-is@^16.12.0, react-is@^16.13.0, react-is@^16.8.1, react-is@^16.8.4:
79697969
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"
79707970
integrity sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==
79717971

7972-
react-is@^17.0.1:
7972+
"react-is@^16.12.0 || ^17.0.0", react-is@^17.0.1:
79737973
version "17.0.1"
79747974
resolved "https://registry.yarnpkg.com/react-is/-/react-is-17.0.1.tgz#5b3531bd76a645a4c9fb6e693ed36419e3301339"
79757975
integrity sha512-NAnt2iGDXohE5LI7uBnLnqvLQMtzhkiAOLXTmv+qnF9Ky7xAPcX8Up/xWIhxvLVGJvuLiNc4xQLtuqDRzb4fSA==
@@ -8012,6 +8012,24 @@ react-refresh@^0.4.0:
80128012
resolved "https://registry.yarnpkg.com/react-refresh/-/react-refresh-0.4.3.tgz#966f1750c191672e76e16c2efa569150cc73ab53"
80138013
integrity sha512-Hwln1VNuGl/6bVwnd0Xdn1e84gT/8T9aYNL+HAKDArLCS7LWjwr7StE30IEYbIkx0Vi3vs+coQxe+SQDbGbbpA==
80148014

8015+
react-shallow-renderer@^16.13.1:
8016+
version "16.14.1"
8017+
resolved "https://registry.yarnpkg.com/react-shallow-renderer/-/react-shallow-renderer-16.14.1.tgz#bf0d02df8a519a558fd9b8215442efa5c840e124"
8018+
integrity sha512-rkIMcQi01/+kxiTE9D3fdS959U1g7gs+/rborw++42m1O9FAQiNI/UNRZExVUoAOprn4umcXf+pFRou8i4zuBg==
8019+
dependencies:
8020+
object-assign "^4.1.1"
8021+
react-is "^16.12.0 || ^17.0.0"
8022+
8023+
react-test-renderer@^17.0.1:
8024+
version "17.0.1"
8025+
resolved "https://registry.yarnpkg.com/react-test-renderer/-/react-test-renderer-17.0.1.tgz#3187e636c3063e6ae498aedf21ecf972721574c7"
8026+
integrity sha512-/dRae3mj6aObwkjCcxZPlxDFh73XZLgvwhhyON2haZGUEhiaY5EjfAdw+d/rQmlcFwdTpMXCSGVk374QbCTlrA==
8027+
dependencies:
8028+
object-assign "^4.1.1"
8029+
react-is "^17.0.1"
8030+
react-shallow-renderer "^16.13.1"
8031+
scheduler "^0.20.1"
8032+
80158033
80168034
version "16.13.1"
80178035
resolved "https://registry.yarnpkg.com/react/-/react-16.13.1.tgz#2e818822f1a9743122c063d6410d85c1e3afe48e"
@@ -8534,6 +8552,14 @@ [email protected]:
85348552
loose-envify "^1.1.0"
85358553
object-assign "^4.1.1"
85368554

8555+
scheduler@^0.20.1:
8556+
version "0.20.1"
8557+
resolved "https://registry.yarnpkg.com/scheduler/-/scheduler-0.20.1.tgz#da0b907e24026b01181ecbc75efdc7f27b5a000c"
8558+
integrity sha512-LKTe+2xNJBNxu/QhHvDR14wUXHRQbVY5ZOYpOGWRzhydZUqrLb2JBvLPY7cAqFmqrWuDED0Mjk7013SZiOz6Bw==
8559+
dependencies:
8560+
loose-envify "^1.1.0"
8561+
object-assign "^4.1.1"
8562+
85378563
semver-compare@^1.0.0:
85388564
version "1.0.0"
85398565
resolved "https://registry.yarnpkg.com/semver-compare/-/semver-compare-1.0.0.tgz#0dee216a1c941ab37e9efb1788f6afc5ff5537fc"

0 commit comments

Comments
 (0)