Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
* @format
*/

// TODO(legacy-fake-timers): Fix these tests to work with modern timers.
jest.useFakeTimers({legacyFakeTimers: true});

import type {GestureResponderEvent} from '../../Types/CoreEventTypes';
import type {PressabilityConfig} from '../Pressability';

Expand Down Expand Up @@ -232,11 +229,14 @@ const createMockPressEvent = (
};
};

const CONFIGURED_DEFAULT_MIN_PRESS_DURATION = 130;
const ONE_MILLISECOND_BEFORE_CONFIGURED_LONG_PRESS_DURATION = 500 - 1;

describe('Pressability', () => {
beforeEach(() => {
jest.useFakeTimers();
jest.resetModules();
jest.restoreAllMocks();
jest.spyOn(Date, 'now');
jest.spyOn(HoverState, 'isHoverEnabled');
});

Expand Down Expand Up @@ -427,7 +427,9 @@ describe('Pressability', () => {
);
// $FlowExpectedError[not-a-function]
handlers.onMouseLeave(createMockMouseEvent('onMouseLeave'));
jest.advanceTimersByTime(499);
jest.advanceTimersByTime(
ONE_MILLISECOND_BEFORE_CONFIGURED_LONG_PRESS_DURATION,
);
expect(config.onHoverOut).not.toBeCalled();
jest.advanceTimersByTime(1);
expect(config.onHoverOut).toBeCalled();
Expand Down Expand Up @@ -461,7 +463,9 @@ describe('Pressability', () => {
handlers.onResponderGrant(createMockPressEvent('onResponderGrant'));
handlers.onResponderMove(createMockPressEvent('onResponderMove'));

jest.advanceTimersByTime(499);
jest.advanceTimersByTime(
ONE_MILLISECOND_BEFORE_CONFIGURED_LONG_PRESS_DURATION,
);
expect(config.onLongPress).not.toBeCalled();
jest.advanceTimersByTime(1);
expect(config.onLongPress).toBeCalled();
Expand All @@ -476,7 +480,9 @@ describe('Pressability', () => {
handlers.onResponderGrant(createMockPressEvent('onResponderGrant'));
handlers.onResponderMove(createMockPressEvent('onResponderMove'));

jest.advanceTimersByTime(499);
jest.advanceTimersByTime(
ONE_MILLISECOND_BEFORE_CONFIGURED_LONG_PRESS_DURATION,
);
expect(config.onLongPress).not.toBeCalled();
jest.advanceTimersByTime(1);
expect(config.onLongPress).toBeCalled();
Expand All @@ -489,7 +495,9 @@ describe('Pressability', () => {
handlers.onResponderGrant(createMockPressEvent('onResponderGrant'));
handlers.onResponderMove(createMockPressEvent('onResponderMove'));

jest.advanceTimersByTime(499);
jest.advanceTimersByTime(
ONE_MILLISECOND_BEFORE_CONFIGURED_LONG_PRESS_DURATION,
);
handlers.onResponderRelease(createMockPressEvent('onResponderRelease'));
jest.advanceTimersByTime(1);

Expand Down Expand Up @@ -525,7 +533,7 @@ describe('Pressability', () => {
}),
);

jest.advanceTimersByTime(130);
jest.advanceTimersByTime(CONFIGURED_DEFAULT_MIN_PRESS_DURATION);
handlers.onResponderMove(
// NOTE: Delta from (0, 0) is ~9.9 < 10.
createMockPressEvent({
Expand Down Expand Up @@ -616,25 +624,21 @@ describe('Pressability', () => {

describe('onPress', () => {
it('is called even when `measure` does not finish', () => {
// Disable onLongPress. Since we run all timers, we otherwise end up
// interpreting these events as a long press.
const {config, handlers} = createMockPressability({
onLongPress: undefined,
});
const {config, handlers} = createMockPressability();

handlers.onStartShouldSetResponder();
handlers.onResponderGrant(createMockPressEvent('onResponderGrant'));

expect(UIManager.measure).toBeCalled();

handlers.onResponderMove(createMockPressEvent('onResponderMove'));
jest.runOnlyPendingTimers();
jest.advanceTimersByTime(0);
expect(config.onPressIn).toBeCalled();

handlers.onResponderRelease(createMockPressEvent('onResponderRelease'));

expect(config.onPress).toBeCalled();
jest.runOnlyPendingTimers();
jest.advanceTimersByTime(CONFIGURED_DEFAULT_MIN_PRESS_DURATION);
expect(config.onPressOut).toBeCalled();
});
});
Expand All @@ -646,7 +650,7 @@ describe('Pressability', () => {
handlers.onStartShouldSetResponder();
handlers.onResponderGrant(createMockPressEvent('onResponderGrant'));

jest.runOnlyPendingTimers();
jest.advanceTimersByTime(0);
expect(config.onPressIn).toBeCalled();
});

Expand Down Expand Up @@ -703,24 +707,24 @@ describe('Pressability', () => {
handlers.onResponderRelease(createMockPressEvent('onResponderRelease'));

expect(config.onPressOut).not.toBeCalled();
jest.runOnlyPendingTimers();
jest.advanceTimersByTime(CONFIGURED_DEFAULT_MIN_PRESS_DURATION);
expect(config.onPressOut).toBeCalled();
});

it('is called after `onResponderRelease` after `delayPressIn`', () => {
it('is called after `onResponderRelease` after a configured `delayPressIn`', () => {
const {config, handlers} = createMockPressability({
delayPressIn: Number.EPSILON,
delayPressIn: 500,
});

handlers.onStartShouldSetResponder();
handlers.onResponderGrant(createMockPressEvent('onResponderGrant'));
handlers.onResponderMove(createMockPressEvent('onResponderMove'));
jest.runOnlyPendingTimers();
jest.advanceTimersByTime(500);
expect(config.onPressIn).toBeCalled();
handlers.onResponderRelease(createMockPressEvent('onResponderRelease'));

expect(config.onPressOut).not.toBeCalled();
jest.runOnlyPendingTimers();
jest.advanceTimersByTime(CONFIGURED_DEFAULT_MIN_PRESS_DURATION);
expect(config.onPressOut).toBeCalled();
});

Expand All @@ -737,24 +741,26 @@ describe('Pressability', () => {
);

expect(config.onPressOut).not.toBeCalled();
jest.runOnlyPendingTimers();
jest.advanceTimersByTime(CONFIGURED_DEFAULT_MIN_PRESS_DURATION);
expect(config.onPressOut).not.toBeCalled();
});

it('is not called after `onResponderTerminate` after `delayPressIn`', () => {
const {config, handlers} = createMockPressability();
const {config, handlers} = createMockPressability({
delayPressIn: 100,
});

handlers.onStartShouldSetResponder();
handlers.onResponderGrant(createMockPressEvent('onResponderGrant'));
handlers.onResponderMove(createMockPressEvent('onResponderMove'));
jest.runOnlyPendingTimers();
jest.advanceTimersByTime(100);
expect(config.onPressIn).toBeCalled();
handlers.onResponderTerminate(
createMockPressEvent('onResponderTerminate'),
);

expect(config.onPressOut).not.toBeCalled();
jest.runOnlyPendingTimers();
jest.advanceTimersByTime(CONFIGURED_DEFAULT_MIN_PRESS_DURATION);
expect(config.onPressOut).toBeCalled();
});

Expand All @@ -767,7 +773,7 @@ describe('Pressability', () => {
handlers.onStartShouldSetResponder();
handlers.onResponderGrant(createMockPressEvent('onResponderGrant'));
handlers.onResponderMove(createMockPressEvent('onResponderMove'));
jest.runOnlyPendingTimers();
jest.advanceTimersByTime(1);
expect(config.onPressIn).toBeCalled();
handlers.onResponderRelease(createMockPressEvent('onResponderRelease'));

Expand All @@ -784,22 +790,14 @@ describe('Pressability', () => {
handlers.onStartShouldSetResponder();
handlers.onResponderGrant(createMockPressEvent('onResponderGrant'));
handlers.onResponderMove(createMockPressEvent('onResponderMove'));
jest.runOnlyPendingTimers();
jest.advanceTimersByTime(0);
expect(config.onPressIn).toBeCalled();

// WORKAROUND: Jest does not advance `Date.now()`.
expect(Date.now).toHaveBeenCalledTimes(1);
const touchActivateTime = Date.now.mock.results[0].value;
jest.advanceTimersByTime(120);
Date.now.mockReturnValue(touchActivateTime + 120);
handlers.onResponderRelease(createMockPressEvent('onResponderRelease'));

handlers.onResponderRelease(createMockPressEvent('onResponderRelease'));
expect(config.onPressOut).not.toBeCalled();
jest.advanceTimersByTime(10);
Date.now.mockReturnValue(touchActivateTime + 130);
expect(config.onPressOut).toBeCalled();

Date.now.mockRestore();
});

it('is called synchronously if minimum press duration is 0ms', () => {
Expand All @@ -810,7 +808,7 @@ describe('Pressability', () => {
handlers.onStartShouldSetResponder();
handlers.onResponderGrant(createMockPressEvent('onResponderGrant'));
handlers.onResponderMove(createMockPressEvent('onResponderMove'));
jest.runOnlyPendingTimers();
jest.advanceTimersByTime(0);
expect(config.onPressIn).toBeCalled();
handlers.onResponderRelease(createMockPressEvent('onResponderRelease'));

Expand Down Expand Up @@ -851,7 +849,7 @@ describe('Pressability', () => {

expect(config.onPressIn).toBeCalled();
expect(config.onPress).toBeCalled();
jest.runOnlyPendingTimers();
jest.advanceTimersByTime(CONFIGURED_DEFAULT_MIN_PRESS_DURATION);
expect(config.onPressOut).toBeCalled();
});

Expand Down Expand Up @@ -891,7 +889,7 @@ describe('Pressability', () => {
handlers.onResponderRelease(createMockPressEvent('onResponderRelease'));

expect(config.onPress).toBeCalled();
jest.runOnlyPendingTimers();
jest.advanceTimersByTime(CONFIGURED_DEFAULT_MIN_PRESS_DURATION);
expect(config.onPressOut).toBeCalled();
});
});
Expand All @@ -915,12 +913,12 @@ describe('Pressability', () => {
pageY: mockRegion.height * 2,
}),
);
jest.runOnlyPendingTimers();
jest.advanceTimersByTime(0);
expect(config.onPressIn).toBeCalled();

handlers.onResponderRelease(createMockPressEvent('onResponderRelease'));
expect(config.onPress).not.toBeCalled();
jest.runOnlyPendingTimers();
jest.advanceTimersByTime(CONFIGURED_DEFAULT_MIN_PRESS_DURATION);
expect(config.onPressOut).toBeCalled();
});

Expand All @@ -942,12 +940,13 @@ describe('Pressability', () => {
pageY: mockRegion.height * 2,
}),
);
jest.runOnlyPendingTimers();
jest.advanceTimersByTime(500);
expect(config.onPressIn).not.toBeCalled();

handlers.onResponderRelease(createMockPressEvent('onResponderRelease'));

expect(config.onPress).not.toBeCalled();
jest.advanceTimersByTime(CONFIGURED_DEFAULT_MIN_PRESS_DURATION);
expect(config.onPressOut).not.toBeCalled();
});

Expand Down Expand Up @@ -978,9 +977,13 @@ describe('Pressability', () => {
handlers.onResponderRelease(createMockPressEvent('onResponderRelease'));

expect(config.onPress).toBeCalled();
jest.runOnlyPendingTimers();
jest.advanceTimersByTime(630); // 1000 - 500 (onPressIn activation, already advanced before) + DEFAULT_MIN_PRESS_DURATION
expect(config.onPressOut).toBeCalled();
});
});
});

afterEach(() => {
jest.useRealTimers();
});
});
Loading