Skip to content

Commit

Permalink
refactor: improve code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Syed-Ali-Abbas-Zaidi committed Aug 20, 2023
1 parent abf4bfc commit 9da5f81
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/utils/hoc.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React from 'react';
import { mount } from 'enzyme';

import { withLocation, withNavigate } from './hoc';

const mockedNavigator = jest.fn();

jest.mock('react-router-dom', () => ({
useNavigate: () => mockedNavigator,
useLocation: () => ({
pathname: '/current-location',
}),
}));

// eslint-disable-next-line react/prop-types
const MockComponent = ({ navigate, location }) => (
// eslint-disable-next-line react/button-has-type, react/prop-types
<button id="btn" onClick={() => navigate('/some-route')}>{location.pathname}</button>
);
const WrappedComponent = withNavigate(withLocation(MockComponent));

test('Provide Navigation to Component', () => {
const wrapper = mount(
<WrappedComponent />,
);
const btn = wrapper.find('#btn');
btn.simulate('click');

expect(mockedNavigator).toHaveBeenCalledWith('/some-route');
});

test('Provide Location object to Component', () => {
const wrapper = mount(
<WrappedComponent />,
);

expect(wrapper.find('#btn').text()).toContain('/current-location');
});

0 comments on commit 9da5f81

Please sign in to comment.