1

I'm rendering the below react component, Below is the code. I have defined both params and uselocation but I keep getting this error TypeError: Cannot read properties of undefined (reading 'match'). Does anyone know what's wrong in it?

jest.mock('react-router-dom', () => {
      return {
        matchPath: jest.fn().mockReturnValue({
          isExact: true,
          params: {
            sId: "aadb37fd-55e3-490b-9aeb-a577416136f7",
            subId: "ca7b321b-7544-42e7-bcaf-a5607e49469b",
          },
          path: "/he/sub/:sId/:subId",
          url: "/he/sub/aadb37fd-55e3-490b-9aeb-a577416136f7/ca7b321b-7544-42e7-bcaf-a5607e49469b",
        }),
        useParams: jest.fn().mockReturnValue({ sId: 'aadb37fd-55e3-490b-9aeb-a577416136f7', subId: "ca7b321b-7544-42e7-bcaf-a5607e49469b" }),
        useLocation: jest.fn().mockReturnValue({ sId: 'aadb37fd-55e3-490b-9aeb-a577416136f7', subId: "ca7b321b-7544-42e7-bcaf-a5607e49469b"  }),
      };
    });

describe("Submission Details", () => {
  beforeEach(() => {
    ReactRedux.useSelector = jest.fn().mockImplementation((callback) => callback(initialState));
    ReactRedux.useDispatch = jest.fn().mockImplementation(() => mockDispatch);
  });

const initialState = {
  router: {
    location: {
      pathname: "/he/sub/aadb37fd-55e3-490b-9aeb-a577416136f7/ca7b321b-7544-42e7-bcaf-a5607e49469b",
    },
  },
}

  it("renders form details",  () => {
    let preselectedTab = "form";
    render(<SDetails preselectedTab={preselectedTab} />);
})
shanky
  • 751
  • 1
  • 16
  • 46
  • look like you may be missing a required property named 'match' which react-router-dom expects – Eliav Louski Aug 04 '22 at 22:58
  • where i have to add it? – shanky Aug 04 '22 at 23:03
  • Again, don't test the implementation details, and don't mock `useSelector` and `useDispatch`. Test the behavior of the component as a black-box. Besides, provide a [mvce](https://stackoverflow.com/help/minimal-reproducible-example) – Lin Du Aug 05 '22 at 02:17

0 Answers0