Skip to content

Commit bf8054e

Browse files
nerradgziolo
authored andcommitted
Convert ‘it closes by blur’ test to use React.TestUtils ( editor/components/autocomplete/test/index.js) (#7782)
* Convert ‘it closes by blur’ test to use React.TestUtils * fix linting * Testing: Reduce code duplication in autocomplete tests
1 parent 0371e1e commit bf8054e

File tree

1 file changed

+48
-4
lines changed

1 file changed

+48
-4
lines changed

components/autocomplete/test/index.js

+48-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44
import { mount } from 'enzyme';
55
import { noop } from 'lodash';
6+
import TestUtils from 'react-dom/test-utils';
67

78
/**
89
* WordPress dependencies
@@ -38,9 +39,10 @@ class FakeEditor extends Component {
3839

3940
function makeAutocompleter( completers, {
4041
AutocompleteComponent = Autocomplete,
42+
mountImplementation = mount,
4143
onReplace = noop,
4244
} = {} ) {
43-
return mount(
45+
return mountImplementation(
4446
<AutocompleteComponent instanceId="1"
4547
completers={ completers }
4648
onReplace={ onReplace }
@@ -107,6 +109,35 @@ function simulateInput( wrapper, nodeList, cursorPosition ) {
107109
wrapper.update();
108110
}
109111

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+
110141
/**
111142
* Fire a native keydown event on the fake editor in the wrapper.
112143
*
@@ -573,12 +604,25 @@ describe( 'Autocomplete', () => {
573604

574605
it( 'closes by blur', () => {
575606
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+
}
576614

577615
const wrapper = makeAutocompleter( [], {
578-
AutocompleteComponent: EnhancedAutocomplete,
616+
AutocompleteComponent: Enhanced,
617+
mountImplementation: TestUtils.renderIntoDocument,
579618
} );
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+
);
582626

583627
jest.runAllTimers();
584628

0 commit comments

Comments
 (0)