forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename hover props in experimental event API and write unit tests (fa…
…cebook#15283) * Rename hover props in experimental event API and write unit tests
- Loading branch information
Showing
2 changed files
with
117 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
packages/react-events/src/__tests__/Hover-test.internal.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @emails react-core | ||
*/ | ||
|
||
'use strict'; | ||
|
||
let React; | ||
let ReactFeatureFlags; | ||
let ReactDOM; | ||
let Hover; | ||
|
||
describe('Hover event responder', () => { | ||
let container; | ||
|
||
beforeEach(() => { | ||
jest.resetModules(); | ||
ReactFeatureFlags = require('shared/ReactFeatureFlags'); | ||
ReactFeatureFlags.enableEventAPI = true; | ||
React = require('react'); | ||
ReactDOM = require('react-dom'); | ||
Hover = require('react-events/hover'); | ||
|
||
container = document.createElement('div'); | ||
document.body.appendChild(container); | ||
}); | ||
|
||
afterEach(() => { | ||
document.body.removeChild(container); | ||
container = null; | ||
}); | ||
|
||
it('should support onHover', () => { | ||
let divRef = React.createRef(); | ||
let events = []; | ||
|
||
function handleOnHover(e) { | ||
if (e) { | ||
events.push('hover in'); | ||
} else { | ||
events.push('hover out'); | ||
} | ||
} | ||
|
||
function Component() { | ||
return ( | ||
<Hover onHoverChange={handleOnHover}> | ||
<div ref={divRef}>Hover me!</div> | ||
</Hover> | ||
); | ||
} | ||
|
||
ReactDOM.render(<Component />, container); | ||
|
||
const mouseOverEvent = document.createEvent('Event'); | ||
mouseOverEvent.initEvent('mouseover', true, true); | ||
divRef.current.dispatchEvent(mouseOverEvent); | ||
|
||
const mouseOutEvent = document.createEvent('Event'); | ||
mouseOutEvent.initEvent('mouseout', true, true); | ||
divRef.current.dispatchEvent(mouseOutEvent); | ||
|
||
expect(events).toEqual(['hover in', 'hover out']); | ||
}); | ||
|
||
it('should support onHoverStart and onHoverEnd', () => { | ||
let divRef = React.createRef(); | ||
let events = []; | ||
|
||
function handleOnHoverStart() { | ||
events.push('onHoverStart'); | ||
} | ||
|
||
function handleOnHoverEnd() { | ||
events.push('onHoverEnd'); | ||
} | ||
|
||
function Component() { | ||
return ( | ||
<Hover onHoverStart={handleOnHoverStart} onHoverEnd={handleOnHoverEnd}> | ||
<div ref={divRef}>Hover me!</div> | ||
</Hover> | ||
); | ||
} | ||
|
||
ReactDOM.render(<Component />, container); | ||
|
||
const mouseOverEvent = document.createEvent('Event'); | ||
mouseOverEvent.initEvent('mouseover', true, true); | ||
divRef.current.dispatchEvent(mouseOverEvent); | ||
|
||
const mouseOutEvent = document.createEvent('Event'); | ||
mouseOutEvent.initEvent('mouseout', true, true); | ||
divRef.current.dispatchEvent(mouseOutEvent); | ||
|
||
expect(events).toEqual(['onHoverStart', 'onHoverEnd']); | ||
}); | ||
}); |