mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Pressability test migrated to Jest moder timers (#55410)
Summary: This PR migrates the `Pressability` tests so that it uses Jest modern timers. First, I centralized Fake timers setup in the `beforeEach` and restored in the `afterEach` (to run test in isolation with respect to timers). I migrated all the calls previously used to fire all pending timers (`jest.runOnlyPendingTimers`) to `jest.advanceTimersByTime`, setting the correct timing need for the tests to verify the entire `Pressability` flows (including minimum press duration and long press delay configurations). This should improve the correctness of the tests (demonstrated also by the test plan below). I also tried to increased the readability of the tests by extracting some constants (related to the inner configured delay/press duration of the `Pressability` class). ## Changelog: [GENERAL] [CHANGED] - Migrated `Pressability` tests to Jest modern timers Pull Request resolved: https://github.com/facebook/react-native/pull/55410 Test Plan: - Ran `Pressability-test` and verify all tests cases passed - Ran the full React Native test suite to ensure all tests pass. - Verified test correctness by intentionally breaking production code: - Modified callback invocation logic (onPressIn, onPressOut, onLongPress) to verify that tests were failing - Changed timing constants (eg. `DEFAULT_MIN_PRESS_DURATION,` DEFAULT_LONG_PRESS_DELAY_MS`)to verify that tests correctly failed Reviewed By: NickGerleman Differential Revision: D92750937 Pulled By: Abbondanzo fbshipit-source-id: 6d01f45f289138c504ccbb53104b219dbaed0bae
This commit is contained in:
1 parent
faebee4338
commit
7dd2d255de
1 file changed
+47
-44
+47
-44
@@ -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';
|
||||
|
||||
@@ -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');
|
||||
});
|
||||
|
||||
@@ -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();
|
||||
@@ -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();
|
||||
@@ -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();
|
||||
@@ -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);
|
||||
|
||||
@@ -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({
|
||||
@@ -616,11 +624,7 @@ 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'));
|
||||
@@ -628,13 +632,13 @@ describe('Pressability', () => {
|
||||
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();
|
||||
});
|
||||
});
|
||||
@@ -646,7 +650,7 @@ describe('Pressability', () => {
|
||||
handlers.onStartShouldSetResponder();
|
||||
handlers.onResponderGrant(createMockPressEvent('onResponderGrant'));
|
||||
|
||||
jest.runOnlyPendingTimers();
|
||||
jest.advanceTimersByTime(0);
|
||||
expect(config.onPressIn).toBeCalled();
|
||||
});
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
|
||||
@@ -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'));
|
||||
|
||||
@@ -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', () => {
|
||||
@@ -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'));
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
});
|
||||
@@ -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();
|
||||
});
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user