Skip to content

Commit de59948

Browse files
chiciometa-codesync[bot]
authored andcommitted
Added missing tests for onHoverOut in Pressability (#55378)
Summary: This PR adds the missing tests for the `Pressability` class, specifically for the `onHoverOut` property. The tests simulate interactions similar to the existing `onHoverIn` tests, triggering `onMouseEnter` followed by `onMouseLeave` to verify that `onHoverOut` is called correctly. ## Changelog: [General] [Added] - Added tests for `Pressability` `onHoverOut` property Pull Request resolved: #55378 Test Plan: - Ran the full React Native test suite to ensure all tests pass. - Verified that the tests fail if the production code for `onHoverOut` is removed, confirming their effectiveness. Reviewed By: fabriziocucci Differential Revision: D91951355 Pulled By: Abbondanzo fbshipit-source-id: 40b0102743ed36228a51cee2b31e151803192549
1 parent 47c3ebe commit de59948

1 file changed

Lines changed: 103 additions & 1 deletion

File tree

packages/react-native/Libraries/Pressability/__tests__/Pressability-test.js

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,109 @@ describe('Pressability', () => {
349349
});
350350
});
351351

352-
// TODO: onHoverOut tests
352+
describe('onHoverOut', () => {
353+
let originalPlatform;
354+
355+
beforeEach(() => {
356+
originalPlatform = Platform.OS;
357+
/* $FlowFixMe[incompatible-type] Error found due to incomplete typing of
358+
* Platform.flow.js */
359+
Platform.OS = 'web';
360+
// $FlowExpectedError[prop-missing]
361+
HoverState.isHoverEnabled.mockReturnValue(true);
362+
});
363+
364+
afterEach(() => {
365+
/* $FlowFixMe[incompatible-type] Error found due to incomplete typing of
366+
* Platform.flow.js */
367+
Platform.OS = originalPlatform;
368+
});
369+
370+
it('is ignored on unsupported platforms`', () => {
371+
/* $FlowFixMe[incompatible-type] Error found due to incomplete typing of
372+
* Platform.flow.js */
373+
Platform.OS = 'ios';
374+
const {handlers} = createMockPressability();
375+
expect(handlers.onMouseLeave).toBeUndefined();
376+
});
377+
378+
it('is called after `onMouseLeave`, and after onHoverIn', () => {
379+
const {config, handlers} = createMockPressability();
380+
invariant(
381+
typeof handlers.onMouseEnter === 'function',
382+
'Expected to find "onMouseEnter" function',
383+
);
384+
// $FlowExpectedError[not-a-function]
385+
handlers.onMouseEnter(createMockMouseEvent('onMouseEnter'));
386+
invariant(
387+
typeof handlers.onMouseLeave === 'function',
388+
'Expected to find "onMouseLeave" function',
389+
);
390+
// $FlowExpectedError[not-a-function]
391+
handlers.onMouseLeave(createMockMouseEvent('onMouseLeave'));
392+
expect(config.onHoverOut).toBeCalled();
393+
});
394+
395+
it('is called with no delay by default', () => {
396+
const {config, handlers} = createMockPressability({
397+
delayHoverOut: null,
398+
});
399+
invariant(
400+
typeof handlers.onMouseEnter === 'function',
401+
'Expected to find "onMouseEnter" function',
402+
);
403+
// $FlowExpectedError[not-a-function]
404+
handlers.onMouseEnter(createMockMouseEvent('onMouseEnter'));
405+
invariant(
406+
typeof handlers.onMouseLeave === 'function',
407+
'Expected to find "onMouseLeave" function',
408+
);
409+
// $FlowExpectedError[not-a-function]
410+
handlers.onMouseLeave(createMockMouseEvent('onMouseLeave'));
411+
expect(config.onHoverOut).toBeCalled();
412+
});
413+
414+
it('is called after a configured delay', () => {
415+
const {config, handlers} = createMockPressability({
416+
delayHoverOut: 500,
417+
});
418+
invariant(
419+
typeof handlers.onMouseEnter === 'function',
420+
'Expected to find "onMouseEnter" function',
421+
);
422+
// $FlowExpectedError[not-a-function]
423+
handlers.onMouseEnter(createMockMouseEvent('onMouseEnter'));
424+
invariant(
425+
typeof handlers.onMouseLeave === 'function',
426+
'Expected to find "onMouseLeave" function',
427+
);
428+
// $FlowExpectedError[not-a-function]
429+
handlers.onMouseLeave(createMockMouseEvent('onMouseLeave'));
430+
jest.advanceTimersByTime(499);
431+
expect(config.onHoverOut).not.toBeCalled();
432+
jest.advanceTimersByTime(1);
433+
expect(config.onHoverOut).toBeCalled();
434+
});
435+
436+
it('is called synchronously if delay is 0ms', () => {
437+
const {config, handlers} = createMockPressability({
438+
delayHoverOut: 0,
439+
});
440+
invariant(
441+
typeof handlers.onMouseEnter === 'function',
442+
'Expected to find "onMouseEnter" function',
443+
);
444+
// $FlowExpectedError[not-a-function]
445+
handlers.onMouseEnter(createMockMouseEvent('onMouseEnter'));
446+
invariant(
447+
typeof handlers.onMouseLeave === 'function',
448+
'Expected to find "onMouseLeave" function',
449+
);
450+
// $FlowExpectedError[not-a-function]
451+
handlers.onMouseLeave(createMockMouseEvent('onMouseLeave'));
452+
expect(config.onHoverOut).toBeCalled();
453+
});
454+
});
353455

354456
describe('onLongPress', () => {
355457
it('is called if pressed for 500ms', () => {

0 commit comments

Comments
 (0)