Jest has an amazing mocking feature. You can both create your mocks inside the __mocks__ directory adjacent to mocked file or create them programmatically.
Let's create the services/__mocks__/api.js file for our mock.
Let's export the spy function we would use in our test:
export const getMockDataSpy = jest.fn();
Now, let's create the actual mock:
export const getMockData = () => {
return new Promise(resolve =>
getMockDataSpy(() => resolve(mockMessages)))
}
Since our getMockData is returning a promise, we will return a new Promise and inside it--we will call getMockDataSpy passing a callback function and resolve.
Now, in our ChatScreen test, we will; need to do two things. First, enable our mock using jest.mock('../../services/api'); this will tell Jest to include our mock file in every place where there is an import...