I'm trying to test that navigate
function is called. I've mocked the component with a jest, but due to the async / promise function it doesn't call the function in time for the test
Component
import React, { useEffect, useState } from 'react';
...
import { Dialog } from '../../../../common/components/Dialog';
const Component = () => {
...
const handleSave= async (value) => {
const { id } = await newRecord(value);
navigate(APP_ROUTES.SOMELOCATION(id));
};
return (
<button onClick={() => handleSave(someValue)} />
);
};
Test
import { navigate } form '@reach/router';
jest.mock('@reach/router');
it('should call navigate when button is clicked', () => {
wrapper.find('button').simulate('click');
expect(navigate).toHaveBeenCalled();
});
It does however work if you do:
setTimeout(() => { expect(navigate).toHaveBeenCalled() }, 100)