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:
Родитель
bf55a3a392
Коммит
8edf4e9e3a
|
@ -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', () => {
|
||||
|
|
Загрузка…
Ссылка в новой задаче