Test fails when using react-navigation container and react-redux provider together
See original GitHub issueHi! I have a weird annoying issue while I try to wrap my component (to render), inside redux provider + navigation container, following the examples such as the one in this Github repo, as for the other example from redux-toolkit repo.
👉 If I combine <NavigationContainer/>
with <Provider/>
as follows:
const ComponentToRender = () => (
<Provider store={mockStore}>
<NavigationContainer>
<MyScreenToRender />
</NavigationContainer>
</Provider>
)
I get the following errors:
ERROR 1 🐞:
console.error
Warning: An update to MyScreenToRender inside a test was not wrapped in act(...).
When testing, code that causes React state updates should be wrapped into act(...):
act(() => {
/* fire events that update state */
});
/* assert on the output */
This ensures that you're testing the behavior the user would see in the browser. Learn more at https://reactjs.org/link/wrap-tests-with-act
ERROR 2 🐞:
WARN - IDE integration: Couldn't find a navigation context. Have you wrapped your app with 'NavigationContainer'? See https://reactnavigation.org/docs/getting-started for setup instructions.
Error: Couldn't find a navigation context. Have you wrapped your app with 'NavigationContainer'? See https://reactnavigation.org/docs/getting-started for setup instructions.
at Object.get getIsInitial [as getIsInitial] (...\node_modules\@react-navigation\core\lib\commonjs\NavigationStateContext.tsx:37:11)
This is my test file code :
import {
screen,
waitFor,
render,
} from "@testing-library/react-native";
import { mockStore } from "@app/tests/mock-redux/mockStore";
import { NavigationContainer } from "@react-navigation/native";
import { Provider } from "react-redux";
import apiInstance from "../../api/myApi";
import MyComponentToRender from "../../view";
jest.mock('react-i18next', () => ({
useTranslation: () => {
return {
t: (str:string) => str,
i18n: {
changeLanguage: () => new Promise(() => {}),
},
};
},
}));
jest.mock('react-native-reanimated', () => {
const Reanimated = require('react-native-reanimated/mock');
Reanimated.default.call = () => {};
return Reanimated;
});
// Reset RTK API state after each test case
afterEach(() => {
// following a recommendation from Redux team
// the RTKQ state, must be reset visit: https://stackoverflow.com/a/69310703/10000150
mockStore.dispatch(apiInstance.util.resetApiState())
})
// TODO
describe('list screen tests', () => {
// scenario one : initialize screen
it('list initialization', async () => {
const ToRender = () => (
<Provider store={mockStore}>
<NavigationContainer>
<MyComponentToRender />
</NavigationContainer>
</Provider>
)
render(<ToRender />);
const screenGlobalLoading = screen.getByText(/loading/, {exact: false});
expect(screenGlobalLoading).toEqual("Loaading...");
await waitFor(() => {
expect(screenGlobalLoading).toBeFalsy();
});
});
});
I use the following techs:
Tech | Version |
---|---|
Expo | 45.0.0 |
React Native | v0.68.2 |
Redux-Toolkit | v1.8.2 |
React Redux | v8.0.2 |
RN Testing Library | v10.1.1 |
react-navigation/native | 6.0.2 |
I can give you further information if needed, please help 🙏
_Originally posted by @firas1220 in https://github.com/callstack/react-native-testing-library/discussions/1100#discussioncomment-3620401_
Issue Analytics
- State:
- Created a year ago
- Comments:10 (4 by maintainers)
Top Results From Across the Web
Error when testing · Issue #9023 · react-navigation ... - GitHub
The Transaction containers it re-render when I update the props using Redux. import React, { PureComponent } from 'react'; ...
Read more >useDispatch() Error: Could not find react-redux context value ...
I wrapped my components with Provider but I still get the error, only if I implement the useDispatch() hook. The app worked fine,...
Read more >A stately guide to React Navigation with Redux - Medium
I'm going to breeze over the part where I create a basic app with a StackNavigator , a few routes with corresponding screen...
Read more >Redux integration | React Navigation
It is extremely easy to use Redux in an app with React Navigation. It's basically no different than without React Navigation.
Read more >Continuous integration for React applications using Jest and ...
Your next step is adding the tests. Testing React components. If you check the package.json file, you will notice you already have a...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Try using the one we have in the readme: https://discord.gg/QbGezWe
There’s something in the works regarding these warnings (they’re warnings, not errors, feel free to ignore them really): https://github.com/callstack/react-native-testing-library/pull/1131
Hey @firas1220. I’d like to make it clear, that this is not really a help forum, but an issue tracker. We have some dedicated capacity to look at the issues, but it’s limited. Please be patient and try reaching out on StackOverflow or on Discord https://discord.com/channels/426714625279524876/497332049187962892 to increase your chances of getting help 😃