Skip to content

Commit 322a3fc

Browse files
nerradgziolo
authored andcommitted
Switch tests away from using enzyme.mount (#7829)
This switches all tests in `components/higher-order/with-state/test/index.js` from using enzyme.mount to `React.TestUtils`. This is because `enzyme` does not fully support React 16.3+ (and movement to do so is really slow). This will fix issues with breakage due to the enzyme incompatibility as components receive React 16.3+ features (such as `forwardRef` usage in #7557).
1 parent bf8054e commit 322a3fc

File tree

1 file changed

+22
-5
lines changed
  • components/higher-order/with-state/test

1 file changed

+22
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
/**
22
* External dependencies
33
*/
4-
import { mount } from 'enzyme';
4+
import TestUtils from 'react-dom/test-utils';
55

66
/**
77
* Internal dependencies
88
*/
99
import withState from '../';
1010

11+
/**
12+
* WordPress dependencies
13+
*/
14+
import { Component } from '@wordpress/element';
15+
16+
// this is needed because TestUtils does not accept a stateless component.
17+
// anything run through a HOC ends up as a stateless component.
18+
const getTestComponent = ( WrappedComponent ) => {
19+
class TestComponent extends Component {
20+
render() {
21+
return <WrappedComponent { ...this.props } />;
22+
}
23+
}
24+
return <TestComponent />;
25+
};
26+
1127
describe( 'withState', () => {
1228
it( 'should pass initial state and allow updates', () => {
1329
const EnhancedComponent = withState( { count: 0 } )( ( { count, setState } ) => (
@@ -16,10 +32,11 @@ describe( 'withState', () => {
1632
</button>
1733
) );
1834

19-
const wrapper = mount( <EnhancedComponent /> );
35+
const wrapper = TestUtils.renderIntoDocument( getTestComponent( EnhancedComponent ) );
36+
const buttonElement = () => TestUtils.findRenderedDOMComponentWithTag( wrapper, 'button' );
2037

21-
expect( wrapper.html() ).toBe( '<button>0</button>' );
22-
wrapper.find( 'button' ).simulate( 'click' );
23-
expect( wrapper.html() ).toBe( '<button>1</button>' );
38+
expect( buttonElement().outerHTML ).toBe( '<button>0</button>' );
39+
TestUtils.Simulate.click( buttonElement() );
40+
expect( buttonElement().outerHTML ).toBe( '<button>1</button>' );
2441
} );
2542
} );

0 commit comments

Comments
 (0)