diff --git a/blocks/inspector-controls/toggle-control/index.js b/blocks/inspector-controls/toggle-control/index.js index 7cb76a834ad20..1410293584e5f 100644 --- a/blocks/inspector-controls/toggle-control/index.js +++ b/blocks/inspector-controls/toggle-control/index.js @@ -1,6 +1,7 @@ /** * WordPress dependencies */ +import { Component } from '@wordpress/element'; import { withInstanceId, FormToggle } from '@wordpress/components'; /** @@ -9,19 +10,44 @@ import { withInstanceId, FormToggle } from '@wordpress/components'; import BaseControl from './../base-control'; import './style.scss'; -function ToggleControl( { label, checked, help, instanceId, onChange } ) { - const id = 'inspector-toggle-control-' + instanceId; +class ToggleControl extends Component { + constructor() { + super( ...arguments ); - return ( - - - - ); + help={ help } + className="blocks-toggle-control" + > + + + ); + } } export default withInstanceId( ToggleControl ); diff --git a/blocks/inspector-controls/toggle-control/test/index.js b/blocks/inspector-controls/toggle-control/test/index.js new file mode 100644 index 0000000000000..718e6ed68c149 --- /dev/null +++ b/blocks/inspector-controls/toggle-control/test/index.js @@ -0,0 +1,43 @@ +/** + * External dependencies + */ +import { mount } from 'enzyme'; + +/** + * Internal dependencies + */ +import ToggleControl from '../'; + +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( ); + + wrapper.find( 'input' ).simulate( 'change', { target: { checked: true } } ); + + expect( onChange ).toHaveBeenCalledWith( true ); + } ); + + describe( 'help', () => { + 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( ); + + const input = wrapper.find( 'input' ); + + expect( input.prop( '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( ); + + const input = wrapper.find( 'input' ); + + expect( input.prop( 'aria-describedby' ) ).toMatch( /^inspector-toggle-control-.*__help$/ ); + } ); + } ); +} );