React: testing if a method of a custom hook has been invoked from a component

Viewed 219

I have created a custom hook called useBrowserNotification:

const useBrowserNotification = () => {
    const [notificationStatus, setNotificationStatus] = useState(null);
    const sendNotification = (args) => {
        //some code goes here
    }

    return { notificationStatus, sendNotification};
}

export default useBrowserNotification;

In one of my components I called the sendNotification method:

export default MyComponent() {
    const componentMethod = useCallback((res) => {
         //some code goes here
         sendNotification(res);
    }

    return(...)
}

Now, in the MyComponent.test.js I am trying to check if the sendNotification has been triggered or not:

import { useBrowserNotification } from '../../hooks';

describe('Browser Notification', () => {
        const hooks = { useBrowserNotification };
        const mockedUseBrowserNotification = jest.spyOn(hooks, 'useBrowserNotification');
        const mockedSendNotification = jest.fn();

        mockedUseBrowserNotification.mockImplementation(() => (
            {
                sendNotification: mockedSendNotification
            }
        ));

        it('should invoke `sendNotification`', async (done) => {
            jest.spyOn(SomeService, 'fetchSomething')
            .mockResolvedValueOnce(someDummyReturnedValue);

            render(<ArticleLandingPage />);

            await waitFor(() => expect(SomeService.fetchSomething).toHaveBeenCalledTimes(1));
            expect(mockedSendNotification).toHaveBeenCalled();
            done();
        });
    })

But now, in the console, I am getting the following error:

expect(jest.fn()).toHaveBeenCalled()

Expected number of calls: >= 1
Received number of calls:    0

  216 |             await waitFor(() => expect(ArticleService.fetchArticleJobs).toHaveBeenCalledTimes(1));
  217 |             await waitFor(() => expect(ArticleService.fetchArticleJobs).toHaveBeenCalledTimes(2));
> 218 |             expect(mockedSendNotification).toHaveBeenCalled();
      |                                            ^
  219 |             done();
  220 |         })
  221 |
0 Answers
Related