-
Notifications
You must be signed in to change notification settings - Fork 51k
Rewrite SelectEventPlugin-test to test behavior using Public API (#11299) #11676
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c280f65
Merge pull request #1 from facebook/master
skiritsis fc957a9
Rewrite SelectEventPlugin-test to test behavior using Public API
skiritsis e4fbaa4
Minor refactor
skiritsis ae31bef
Make sure that we test that "focus" event is ignored
skiritsis 8d7fff5
Rewrote the other test to use Public API as well
skiritsis e1856a0
Tweak the test
gaearon 9c7bdb6
Remove -internal suffix
gaearon 275b2f4
Oops
gaearon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -11,81 +11,81 @@ | |
|
|
||
| var React; | ||
| var ReactDOM; | ||
| var ReactDOMComponentTree; | ||
| var ReactTestUtils; | ||
| var SelectEventPlugin; | ||
|
|
||
| describe('SelectEventPlugin', () => { | ||
| function extract(node, topLevelEvent) { | ||
| return SelectEventPlugin.extractEvents( | ||
| topLevelEvent, | ||
| ReactDOMComponentTree.getInstanceFromNode(node), | ||
| {target: node}, | ||
| node, | ||
| ); | ||
| } | ||
| var container; | ||
|
|
||
| beforeEach(() => { | ||
| React = require('react'); | ||
| ReactDOM = require('react-dom'); | ||
| ReactTestUtils = require('react-dom/test-utils'); | ||
| // TODO: can we express this test with only public API? | ||
| ReactDOMComponentTree = require('../../client/ReactDOMComponentTree'); | ||
| SelectEventPlugin = require('../SelectEventPlugin').default; | ||
| }); | ||
|
|
||
| it('should skip extraction if no listeners are present', () => { | ||
| class WithoutSelect extends React.Component { | ||
| render() { | ||
| return <input type="text" />; | ||
| } | ||
| } | ||
|
|
||
| var rendered = ReactTestUtils.renderIntoDocument(<WithoutSelect />); | ||
| var node = ReactDOM.findDOMNode(rendered); | ||
| node.focus(); | ||
|
|
||
| // It seems that .focus() isn't triggering this event in our test | ||
| // environment so we need to ensure it gets set for this test to be valid. | ||
| var fakeNativeEvent = function() {}; | ||
| fakeNativeEvent.target = node; | ||
| ReactTestUtils.simulateNativeEventOnNode('topFocus', node, fakeNativeEvent); | ||
|
|
||
| var mousedown = extract(node, 'topMouseDown'); | ||
| expect(mousedown).toBe(null); | ||
|
|
||
| var mouseup = extract(node, 'topMouseUp'); | ||
| expect(mouseup).toBe(null); | ||
| container = document.createElement('div'); | ||
| document.body.appendChild(container); | ||
| }); | ||
|
|
||
| it('should extract if an `onSelect` listener is present', () => { | ||
| class WithSelect extends React.Component { | ||
| render() { | ||
| return <input type="text" onSelect={this.props.onSelect} />; | ||
| } | ||
| } | ||
| afterEach(() => { | ||
| document.body.removeChild(container); | ||
| container = null; | ||
| }); | ||
|
|
||
| var cb = jest.fn(); | ||
| it('should verify that `onSelect` fails to fire when no `topMouseUp` has yet occurred to unset the flag', () => { | ||
| var select = jest.fn(); | ||
| var onSelect = event => select(event.currentTarget); | ||
|
|
||
| var rendered = ReactTestUtils.renderIntoDocument( | ||
| <WithSelect onSelect={cb} />, | ||
| var node = ReactDOM.render( | ||
| <input type="text" onSelect={onSelect} />, | ||
| container, | ||
| ); | ||
| var node = ReactDOM.findDOMNode(rendered); | ||
|
|
||
| node.selectionStart = 0; | ||
| node.selectionEnd = 0; | ||
| node.focus(); | ||
|
|
||
| var focus = extract(node, 'topFocus'); | ||
| expect(focus).toBe(null); | ||
| var nativeEvent = new MouseEvent('focus', { | ||
| bubbles: true, | ||
| cancelable: true, | ||
| }); | ||
| node.dispatchEvent(nativeEvent); | ||
|
|
||
| nativeEvent = new MouseEvent('mousedown', { | ||
| bubbles: true, | ||
| cancelable: true, | ||
| }); | ||
| node.dispatchEvent(nativeEvent); | ||
|
|
||
| nativeEvent = new MouseEvent('keyup', {bubbles: true, cancelable: true}); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: keyup is not a mouse event. |
||
| node.dispatchEvent(nativeEvent); | ||
| expect(select.mock.calls.length).toBe(0); | ||
| }); | ||
|
|
||
| var mousedown = extract(node, 'topMouseDown'); | ||
| expect(mousedown).toBe(null); | ||
| it('should verify that `onSelect` fires when the listener is present', () => { | ||
| var select = jest.fn(); | ||
| var onSelect = event => { | ||
| expect(typeof event).toBe('object'); | ||
| expect(event.type).toBe('select'); | ||
| expect(event.target).toBe(node); | ||
| select(event.currentTarget); | ||
| }; | ||
|
|
||
| var node = ReactDOM.render( | ||
| <input type="text" onSelect={onSelect} />, | ||
| container, | ||
| ); | ||
| node.focus(); | ||
|
|
||
| var mouseup = extract(node, 'topMouseUp'); | ||
| expect(mouseup).not.toBe(null); | ||
| expect(typeof mouseup).toBe('object'); | ||
| expect(mouseup.type).toBe('select'); | ||
| expect(mouseup.target).toBe(node); | ||
| var nativeEvent = new MouseEvent('focus', { | ||
| bubbles: true, | ||
| cancelable: true, | ||
| }); | ||
| node.dispatchEvent(nativeEvent); | ||
| expect(select.mock.calls.length).toBe(0); | ||
|
|
||
| nativeEvent = new MouseEvent('mousedown', { | ||
| bubbles: true, | ||
| cancelable: true, | ||
| }); | ||
| node.dispatchEvent(nativeEvent); | ||
| expect(select.mock.calls.length).toBe(0); | ||
|
|
||
| nativeEvent = new MouseEvent('mouseup', {bubbles: true, cancelable: true}); | ||
| node.dispatchEvent(nativeEvent); | ||
| expect(select.mock.calls.length).toBe(1); | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to be testing that a
focusDOM event would have been ignored at this point. Shouldn't the new test also verify this?