clauderic / clauderic/react-sortable-hoc
Unable to pass in Unit Test using Jest or React Testing Library
- Dominant language
- JavaScript
- Stars
- 10.9k
- Forks
- 959
- PR merge metrics
- No merged PRs in 30d
Description
I am currently using react-testing library and jest for my unit test and unable to mock the move functionality. Below is what I've tried implementing but no luck. Everything passes in the test, but if I log the event in my component nothing logs when running my test and I am still getting lines uncovered in my report.
```
...
it('should sort links', async () => {
const mockFunction = jest.fn();
const modList = [
{
type: 'body',
id: 1,
title: 'One',
description: 'https://www.apple.com',
imageUrl: 'https://www.apple.com/logo.png',
},
{
type: 'body',
id: 2,
title: 'Two',
description: 'https://www.apple.com',
imageUrl: 'https://www.apple.com/logo.png',
},
];
const SortItem = () =>
const { container } = render(
,
);
const getTableCells = () => Array.from(container.querySelectorAll('.mywork-widget-drag-handler'));
const createBubbledEvent = (type, props = {}) => {
const event = new Event(type, { bubbles: true });
Object.assign(event, props);
return event;
};
const tableCells = getTableCells();
const startingNode = tableCells[0];
//const endingNode = tableCells[1];
startingNode.dispatchEvent(createBubbledEvent('mousedown', { left: 0, top: 0 }));
startingNode.dispatchEvent(createBubbledEvent('mousemove', { left: 0, top: '100px' }));
startingNode.dispatchEvent(createBubbledEvent('mouseup', { left: 0, top: '150px' }));
});
```
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by reproducing the Jest and React Testing Library example around WidgetList, SortItem, and the .mywork-widget-drag-handler elements. Inspect how the component handles the dispatched mousedown, mousemove, and mouseup events, then verify that the mocked onSortEnd callback is reached and the relevant lines are covered.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, react
- Domain
- frontend, testing
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100