Search code examples
react-nativereduxjestjs

How to mock object of data?


I'm using React Native with Expo, Jest for snapshot testing and Redux Toolkit with query. I want to create a snapshot but always getting an error TypeError. I think problem is in my mocks, I don't know how to mock an entire object of data. Example below:

jest.mock('../../services', () => ({
  useGetOfficeDataQuery: jest.fn().mockReturnValue('Biuro księgowe')
}));


it('renders correctly', () => {
  const snap = renderer.create(<ContactScreen />).toJSON();
  expect(snap).toMatchSnapshot();
});

But in this mock I need to mock entire object like this below:

{
    "id": "xxxxxxxxxxxxxxxxxxxxxxxx",
    "name": "Xxxxxxxxxxxx",
    "nip": "xxxxxxxxxx",
    "phone": null,
    "city": "xxxxx",
    "zip_code": "xx-xxx",
    "street": "xxxxxxxxxxxxxxxxx",
    "house_number": "xxxxxxx",
    "room_number": "xx",
    "email": "xxxxxxxxx",
    "email_correspondence": "xxxxxxxxxxxxxxx"
}

Solution

  • Okay I figured it out, to mock an object I needed to wrap this in { } and write it like a json, code example below:

    jest.mock('../../services', () => ({
      useGetOfficeDataQuery: jest.fn().mockReturnValue({
        id: "xxxxxxxxxxxxxxxxxxxxx",
        name: "xxxxxxxxxx",
      }),
    }));