Pressability: Eliminate Unit Test Timing Flakiness

Summary:
Currently, it is possible in one of the "minimum press duration" unit tests for certain instructions to take longer than expected, skewing the return value of `Date.now()` by at least 10ms.

This changes the unit test to mock `Date.now()` more accurately so that the test is no longer flakey.

Changelog: [Internal]

Reviewed By: jacdebug

Differential Revision: D39804152

fbshipit-source-id: ab62fd1921bd015d969da9595bd3267c38c6e59c
This commit is contained in:
Tim Yung 2022-09-26 08:49:51 -07:00 коммит произвёл Facebook GitHub Bot
Родитель bf55a3a392
Коммит 8edf4e9e3a
1 изменённых файлов: 7 добавлений и 1 удалений

Просмотреть файл

@ -233,6 +233,7 @@ const createMockPressEvent = (
describe('Pressability', () => {
beforeEach(() => {
jest.resetModules();
jest.restoreAllMocks();
jest.spyOn(Date, 'now');
jest.spyOn(HoverState, 'isHoverEnabled');
});
@ -667,15 +668,20 @@ describe('Pressability', () => {
handlers.onResponderMove(createMockPressEvent('onResponderMove'));
jest.runOnlyPendingTimers();
expect(config.onPressIn).toBeCalled();
// WORKAROUND: Jest does not advance `Date.now()`.
const touchActivateTime = 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'));
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', () => {