Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch tests away from using enzyme.mount (components/toggle-control/test/index.js) #7797

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions components/toggle-control/test/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
/**
* External dependencies
*/
import { mount } from 'enzyme';
import renderer from 'react-test-renderer';

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

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

// this is needed because TestUtils does not accept a stateless component.
// anything run through a HOC ends up as a stateless component.
const getTestComponent = ( WrappedComponent, props ) => {
class TestComponent extends Component {
render() {
return <WrappedComponent { ...this.props } />;
}
}
return <TestComponent { ...props } />;
};

describe( 'ToggleControl', () => {
it( 'triggers change callback with numeric value', () => {
// Mount: With shallow, cannot find input child of BaseControl
const onChange = jest.fn();
const wrapper = mount( <ToggleControl onChange={ onChange } /> );

wrapper.find( 'input' ).simulate( 'change', { target: { checked: true } } );
const wrapper = renderer.create(
getTestComponent( ToggleControl, { onChange } )
);
wrapper.root.findByType( 'input' ).props.onChange( { target: { checked: true } } );

expect( onChange ).toHaveBeenCalledWith( true );
} );
Expand All @@ -23,21 +40,25 @@ describe( 'ToggleControl', () => {
it( 'does not render input with describedby if no help prop', () => {
// Mount: With shallow, cannot find input child of BaseControl
const onChange = jest.fn();
const wrapper = mount( <ToggleControl onChange={ onChange } /> );
const wrapper = renderer.create(
getTestComponent( ToggleControl, { onChange } )
);

const input = wrapper.find( 'input' );
const input = wrapper.root.findByType( 'input' );

expect( input.prop( 'aria-describedby' ) ).toBeUndefined();
expect( input.props[ 'aria-describedby' ] ).toBeUndefined();
} );

it( 'renders input with describedby if help prop', () => {
// Mount: With shallow, cannot find input child of BaseControl
const onChange = jest.fn();
const wrapper = mount( <ToggleControl help onChange={ onChange } /> );
const wrapper = renderer.create(
getTestComponent( ToggleControl, { help: true, onChange } )
);

const input = wrapper.find( 'input' );
const input = wrapper.root.findByType( 'input' );

expect( input.prop( 'aria-describedby' ) ).toMatch( /^inspector-toggle-control-.*__help$/ );
expect( input.props[ 'aria-describedby' ] ).toMatch( /^inspector-toggle-control-.*__help$/ );
} );
} );
} );