Skip to content

Commit

Permalink
Add event-drag test
Browse files Browse the repository at this point in the history
  • Loading branch information
pdpino committed May 17, 2022
1 parent 7983968 commit a72489e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"homepage": "https://github.com/hoangnm/react-native-week-view#readme",
"scripts": {
"lint": "eslint ./src *.js",
"test": "jest ./src"
"test": "jest"
},
"dependencies": {
"memoize-one": "^5.1.1",
Expand Down
1 change: 1 addition & 0 deletions src/Event/Event.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ const Event = ({

const dragGesture = Gesture.Pan()
.enabled(isDragEnabled)
.withTestId('dragGesture')
.onTouchesDown(() => {
isDragging.value = true;
})
Expand Down
40 changes: 40 additions & 0 deletions src/__tests__/Event.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import 'react-native';
import React from 'react';
import {
fireGestureHandler,
getByGestureTestId,
} from 'react-native-gesture-handler/jest-utils';
import { render } from '@testing-library/react-native';
import Event from '../Event/Event';

describe('onDrag handler', () => {
const buildDragGesture = () => [
// {}, // implicit BEGIN state
{ translationX: 3, translationY: 21 }, // ACTIVE state
{ translationX: 20, translationY: -3 }, // ACTIVE state
{ translationX: 7, translationY: 52 }, // --> last ACTIVE position
// {}, // implicit END state
];

it('Calls onDrag with new position', () => {
const mockEvent = {
id: 1,
description: 'some description',
color: 'red',
startDate: new Date(2021, 1, 3, 12, 0),
endDate: new Date(2021, 1, 3, 12, 2),
};
const onDragMock = jest.fn(() => null);
const position = { top: 10, left: 0, width: 40, height: 50 };

render(<Event event={mockEvent} onDrag={onDragMock} position={position} />);
fireGestureHandler(getByGestureTestId('dragGesture'), buildDragGesture());

expect(onDragMock).toHaveBeenCalledTimes(1);
expect(onDragMock).toHaveBeenCalledWith(
mockEvent,
position.left + position.width / 2 + 7,
position.top + 52,
);
});
});

1 comment on commit a72489e

@rdewolff
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice test!

Please sign in to comment.