|
| 1 | +/** |
| 2 | + * Copyright 2013-2015, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + * |
| 9 | + * @emails react-core |
| 10 | + */ |
| 11 | + |
| 12 | +'use strict'; |
| 13 | + |
| 14 | +var mockModules = require('mock-modules'); |
| 15 | +mockModules.mock('getActiveElement'); |
| 16 | + |
| 17 | +var EventConstants; |
| 18 | +var React; |
| 19 | +var ReactMount; |
| 20 | +var ReactTestUtils; |
| 21 | +var SelectEventPlugin; |
| 22 | + |
| 23 | +var getActiveElement; |
| 24 | + |
| 25 | +var topLevelTypes; |
| 26 | + |
| 27 | +describe('SelectEventPlugin', function() { |
| 28 | + function extract(node, topLevelEvent) { |
| 29 | + return SelectEventPlugin.extractEvents( |
| 30 | + topLevelEvent, |
| 31 | + node, |
| 32 | + ReactMount.getID(node), |
| 33 | + {target: node} |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + beforeEach(function() { |
| 38 | + mockModules.dumpCache(); |
| 39 | + |
| 40 | + EventConstants = require('EventConstants'); |
| 41 | + React = require('React'); |
| 42 | + ReactMount = require('ReactMount'); |
| 43 | + ReactTestUtils = require('ReactTestUtils'); |
| 44 | + SelectEventPlugin = require('SelectEventPlugin'); |
| 45 | + |
| 46 | + getActiveElement = require('getActiveElement'); |
| 47 | + |
| 48 | + topLevelTypes = EventConstants.topLevelTypes; |
| 49 | + }); |
| 50 | + |
| 51 | + it('should skip extraction if no listeners are present', function() { |
| 52 | + var WithoutSelect = React.createClass({ |
| 53 | + render: function() { |
| 54 | + return <input type="text" />; |
| 55 | + } |
| 56 | + }); |
| 57 | + |
| 58 | + var rendered = ReactTestUtils.renderIntoDocument(<WithoutSelect />); |
| 59 | + var node = React.findDOMNode(rendered); |
| 60 | + getActiveElement.mockReturnValue(node); |
| 61 | + |
| 62 | + var mousedown = extract(node, topLevelTypes.topMouseDown); |
| 63 | + expect(mousedown).toBe(null); |
| 64 | + |
| 65 | + var mouseup = extract(node, topLevelTypes.topMouseUp); |
| 66 | + expect(mouseup).toBe(null); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should extract if an `onSelect` listener is present', function() { |
| 70 | + var mocks = require('mocks'); |
| 71 | + |
| 72 | + var WithSelect = React.createClass({ |
| 73 | + render: function() { |
| 74 | + return <input type="text" onSelect={this.props.onSelect} />; |
| 75 | + } |
| 76 | + }); |
| 77 | + |
| 78 | + var cb = mocks.getMockFunction(); |
| 79 | + |
| 80 | + var rendered = ReactTestUtils.renderIntoDocument( |
| 81 | + <WithSelect onSelect={cb} /> |
| 82 | + ); |
| 83 | + var node = React.findDOMNode(rendered); |
| 84 | + |
| 85 | + node.selectionStart = 0; |
| 86 | + node.selectionEnd = 0; |
| 87 | + getActiveElement.mockReturnValue(node); |
| 88 | + |
| 89 | + var focus = extract(node, topLevelTypes.topFocus); |
| 90 | + expect(focus).toBe(null); |
| 91 | + |
| 92 | + var mousedown = extract(node, topLevelTypes.topMouseDown); |
| 93 | + expect(mousedown).toBe(null); |
| 94 | + |
| 95 | + var mouseup = extract(node, topLevelTypes.topMouseUp); |
| 96 | + expect(mouseup).not.toBe(null); |
| 97 | + expect(typeof mouseup).toBe('object'); |
| 98 | + expect(mouseup.type).toBe('select'); |
| 99 | + expect(mouseup.target).toBe(node); |
| 100 | + }); |
| 101 | +}); |
0 commit comments