question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Navigation & Redux setup providing errors

See original GitHub issue

I 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:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
thymikeecommented, May 31, 2021

@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.

0reactions
AugustinLFcommented, Apr 28, 2022

You can see some examples on how to test things with redux in the docs.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found