Skip to content

Commit

Permalink
Switch tests away from enzyme.mount (#7796)
Browse files Browse the repository at this point in the history
This switches all tests in `components/higher-order/with-context/test/index.js` from using enzyme.mount to `React.TestRenderer`.  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).
  • Loading branch information
nerrad authored and gziolo committed Jul 9, 2018
1 parent 6ebdc6f commit a5eb369
Showing 1 changed file with 34 additions and 11 deletions.
45 changes: 34 additions & 11 deletions components/higher-order/with-context/test/index.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,59 @@
/**
* External dependencies
*/
import { mount } from 'enzyme';
import renderer from 'react-test-renderer';
import PropTypes from 'prop-types';

/**
* Internal dependencies
*/
import withContext from '../';

/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';

class PassContext extends Component {
getChildContext() {
return this.props.value;
}

render() {
return this.props.children;
}
}

PassContext.childContextTypes = {
value: PropTypes.any,
settings: PropTypes.any,
};

describe( 'withContext', () => {
it( 'should return a new component which has context', () => {
const Component = withContext( 'value' )()( ( { value } ) => <div>{ value }</div> );
const wrapper = mount(
<Component />,
{ context: { value: 'ok' } }
const TestComponent = withContext( 'value' )()( ( { value } ) => <div>{ value }</div> );
const wrapper = renderer.create(
<PassContext value={ { value: 'ok' } }>
<TestComponent />
</PassContext>
);

expect( wrapper.text() ).toBe( 'ok' );
expect( wrapper.root.findByType( 'div' ).children[ 0 ] ).toBe( 'ok' );
} );

it( 'should allow specifying a context getter mapping', () => {
const Component = withContext( 'settings' )(
const TestComponent = withContext( 'settings' )(
( settings ) => ( { remap: settings.value } )
)(
( { ignore, remap } ) => <div>{ ignore }{ remap }</div>
);

const wrapper = mount(
<Component />,
{ context: { settings: { ignore: 'ignore', value: 'ok' } } }
const wrapper = renderer.create(
<PassContext value={ { settings: { ignore: 'ignore', value: 'ok' } } } >
<TestComponent />
</PassContext>
);

expect( wrapper.text() ).toBe( 'ok' );
expect( wrapper.root.findByType( 'div' ).children[ 0 ] ).toBe( 'ok' );
} );
} );

0 comments on commit a5eb369

Please sign in to comment.