Navigation & Redux setup providing errors
See original GitHub issueI am trying to write a test for an app using react-navigation and I am running into issues of the route and params being read correctly.
I am getting an error of
TypeError: Cannot read property ‘params’ of undefined
on const [leadId] = useState(route.params.leadId);
my components looks like
export default function AComponent() {
const route = useRoute();
const navigation = useNavigation();
const dispatch = useDispatch();
const [leadId] = useState(route.params.leadId);
...rest
}
I have tried following but I received Warning: React.createElement: type is invalid when wrapping the component.
My test looks like
import React from 'react';
import { Provider } from 'react-redux';
import { NavigationContainer } from '@react-navigation/native';
import { render, fireEvent, cleanup } from 'react-native-testing-library';
import configureMockStore from 'redux-mock-store';
import AComponent from 'components/contact/AComponent';
const mockStore = configureMockStore([]);
describe('<AComponent />', () => {
let getByTestId, store;
beforeEach(() => {
store = mockStore({});
({ getByTestId } = render(
<Provider store={store}>
<NavigationContainer>
<AComponent />
</NavigationContainer>
</Provider>
));
});
});
my mock is
jest.mock('@react-navigation/native', () => {
return {
useNavigation: () => ({ goBack: jest.fn() }),
useRoute: jest.fn(),
};
});
I am not sure if I am wrapping the components incorrectly, or if I am missing something else.
Any ideas or help would be greatly appreciated.
Thanks.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
error with react-navigation when using redux Provider
I implement redux in a react-native app where I use react-navigation. Before trying to implement redux, react-navigation was working.
Read more >Troubleshooting | React Navigation
This section attempts to outline issues that users frequently encounter when first getting accustomed to using React Navigation. These issues may or may...
Read more >React Router with Redux: Understanding navigation state
Use React Router to declaratively navigate within your React and Redux applications and maintain state across your app's navigation ...
Read more >Redux Fundamentals, Part 8: Modern Redux with Redux Toolkit
The official Fundamentals tutorial for Redux: learn the modern way to write Redux logic.
Read more >Let's build Deliveroo 2.0 with REACT NATIVE ... - YouTube
( Navigation, Redux, Tailwind CSS & Sanity.io).
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
@hisham-blackbuck please don’t mock the libraries. It’s hardly ever necessary. Instead use them in your test code as you’d use them in the app. It often comes with some boilerplate, but it’s not really new code, you already use it similarly in your app. When mocking, you’ll need to maintain a more complex setup that’s prone to errors because of implementation details changing in the library. Don’t do it please.
You can see some examples on how to test things with redux in the docs.