|
3 | 3 | */
|
4 | 4 | import { mount } from 'enzyme';
|
5 | 5 | import { noop } from 'lodash';
|
| 6 | +import TestUtils from 'react-dom/test-utils'; |
6 | 7 |
|
7 | 8 | /**
|
8 | 9 | * WordPress dependencies
|
@@ -38,9 +39,10 @@ class FakeEditor extends Component {
|
38 | 39 |
|
39 | 40 | function makeAutocompleter( completers, {
|
40 | 41 | AutocompleteComponent = Autocomplete,
|
| 42 | + mountImplementation = mount, |
41 | 43 | onReplace = noop,
|
42 | 44 | } = {} ) {
|
43 |
| - return mount( |
| 45 | + return mountImplementation( |
44 | 46 | <AutocompleteComponent instanceId="1"
|
45 | 47 | completers={ completers }
|
46 | 48 | onReplace={ onReplace }
|
@@ -107,6 +109,35 @@ function simulateInput( wrapper, nodeList, cursorPosition ) {
|
107 | 109 | wrapper.update();
|
108 | 110 | }
|
109 | 111 |
|
| 112 | +/** |
| 113 | + * Same as simulateInput except configured for use with React.TestUtils |
| 114 | + * @param {*} wrapper Wrapper around react node |
| 115 | + * containing a FakeEditor. |
| 116 | + * @param {Array.<Node>} nodeList Array of dom nodes. |
| 117 | + * @param {Array.<number>} cursorPosition Array specifying the child indexes and |
| 118 | + * offset of the cursor. |
| 119 | + */ |
| 120 | +function simulateInputForUtils( wrapper, nodeList, cursorPosition ) { |
| 121 | + // update the editor content |
| 122 | + const fakeEditor = TestUtils.findRenderedDOMComponentWithClass( |
| 123 | + wrapper, |
| 124 | + 'fake-editor' |
| 125 | + ); |
| 126 | + fakeEditor.innerHTML = ''; |
| 127 | + nodeList.forEach( ( element ) => fakeEditor.appendChild( element ) ); |
| 128 | + if ( cursorPosition && cursorPosition.length >= 1 ) { |
| 129 | + fakeEditor.setAttribute( 'data-cursor', cursorPosition.join( ',' ) ); |
| 130 | + } else { |
| 131 | + fakeEditor.removeAttribute( 'data-cursor' ); |
| 132 | + } |
| 133 | + TestUtils.Simulate.input( |
| 134 | + fakeEditor, |
| 135 | + { |
| 136 | + target: fakeEditor, |
| 137 | + } |
| 138 | + ); |
| 139 | +} |
| 140 | + |
110 | 141 | /**
|
111 | 142 | * Fire a native keydown event on the fake editor in the wrapper.
|
112 | 143 | *
|
@@ -573,12 +604,25 @@ describe( 'Autocomplete', () => {
|
573 | 604 |
|
574 | 605 | it( 'closes by blur', () => {
|
575 | 606 | jest.spyOn( Autocomplete.prototype, 'handleFocusOutside' );
|
| 607 | + // required because TestUtils doesn't handle stateless components for some |
| 608 | + // reason. Without this, wrapper would end up with the value of null. |
| 609 | + class Enhanced extends Component { |
| 610 | + render() { |
| 611 | + return <EnhancedAutocomplete { ...this.props } />; |
| 612 | + } |
| 613 | + } |
576 | 614 |
|
577 | 615 | const wrapper = makeAutocompleter( [], {
|
578 |
| - AutocompleteComponent: EnhancedAutocomplete, |
| 616 | + AutocompleteComponent: Enhanced, |
| 617 | + mountImplementation: TestUtils.renderIntoDocument, |
579 | 618 | } );
|
580 |
| - simulateInput( wrapper, [ par( tx( '/' ) ) ] ); |
581 |
| - wrapper.find( '.fake-editor' ).simulate( 'blur' ); |
| 619 | + simulateInputForUtils( wrapper, [ par( tx( '/' ) ) ] ); |
| 620 | + TestUtils.Simulate.blur( |
| 621 | + TestUtils.findRenderedDOMComponentWithClass( |
| 622 | + wrapper, |
| 623 | + 'fake-editor' |
| 624 | + ) |
| 625 | + ); |
582 | 626 |
|
583 | 627 | jest.runAllTimers();
|
584 | 628 |
|
|
0 commit comments