diff --git a/packages/components/CHANGELOG.md b/packages/components/CHANGELOG.md index 4f26fe1e22dee..8a92bbc453439 100644 --- a/packages/components/CHANGELOG.md +++ b/packages/components/CHANGELOG.md @@ -29,6 +29,7 @@ ### Internal +- `Icon`: Refactor tests to `@testing-library/react` ([#44051](https://github.com/WordPress/gutenberg/pull/44051)). - Fix TypeScript types for `isValueDefined()` and `isValueEmpty()` utility functions ([#43983](https://github.com/WordPress/gutenberg/pull/43983)). - `RadioControl`: Clean up styles to use less custom CSS ([#43868](https://github.com/WordPress/gutenberg/pull/43868)). - Remove unused `normalizeArrowKey` utility function ([#43640](https://github.com/WordPress/gutenberg/pull/43640/)). @@ -38,7 +39,7 @@ - Refactor `FocalPointPicker` to function component ([#39168](https://github.com/WordPress/gutenberg/pull/39168)). - `Guide`: use `code` instead of `keyCode` for keyboard events ([#43604](https://github.com/WordPress/gutenberg/pull/43604/)). - `ToggleControl`: Convert to TypeScript and streamline CSS ([#43717](https://github.com/WordPress/gutenberg/pull/43717)). -- `FocalPointPicker`: Convert to TypeScript ([#43872](https://github.com/WordPress/gutenberg/pull/43872)). +- `FocalPointPicker`: Convert to TypeScript ([#43872](https://github.com/WordPress/gutenberg/pull/43872)). - `Navigation`: use `code` instead of `keyCode` for keyboard events ([#43644](https://github.com/WordPress/gutenberg/pull/43644/)). - `ComboboxControl`: Add unit tests ([#42403](https://github.com/WordPress/gutenberg/pull/42403)). - `NavigableContainer`: use `code` instead of `keyCode` for keyboard events, rewrite tests using RTL and `user-event` ([#43606](https://github.com/WordPress/gutenberg/pull/43606/)). diff --git a/packages/components/src/icon/test/index.js b/packages/components/src/icon/test/index.js index d1c5b7123c3d8..5fc897576db1c 100644 --- a/packages/components/src/icon/test/index.js +++ b/packages/components/src/icon/test/index.js @@ -1,71 +1,67 @@ /** * External dependencies */ -import { shallow } from 'enzyme'; - -/** - * WordPress dependencies - */ -import { Component } from '@wordpress/element'; +import { render, screen } from '@testing-library/react'; /** * Internal dependencies */ -import Dashicon from '../../dashicon'; import Icon from '../'; import { Path, SVG } from '../../'; describe( 'Icon', () => { + const testId = 'icon'; const className = 'example-class'; const svg = ( ); - const style = { fill: 'red' }; it( 'renders nothing when icon omitted', () => { - const wrapper = shallow( ); + render( ); - expect( wrapper.type() ).toBeNull(); + expect( screen.queryByTestId( testId ) ).not.toBeInTheDocument(); } ); it( 'renders a dashicon by slug', () => { - const wrapper = shallow( ); + render( ); - expect( wrapper.find( 'Dashicon' ).prop( 'icon' ) ).toBe( - 'format-image' + expect( screen.getByTestId( testId ) ).toHaveClass( + 'dashicons-format-image' ); } ); it( 'renders a function', () => { - const wrapper = shallow( } /> ); + render( } /> ); - expect( wrapper.name() ).toBe( 'span' ); + expect( screen.getByTestId( testId ) ).toBeVisible(); } ); it( 'renders an element', () => { - const wrapper = shallow( } /> ); + render( } /> ); - expect( wrapper.name() ).toBe( 'span' ); + expect( screen.getByTestId( testId ) ).toBeVisible(); } ); it( 'renders an svg element', () => { - const wrapper = shallow( ); + render( ); - expect( wrapper.name() ).toBe( 'SVG' ); + expect( screen.getByTestId( testId ) ).toBeVisible(); } ); it( 'renders an svg element with a default width and height of 24', () => { - const wrapper = shallow( ); + render( ); + const icon = screen.getByTestId( testId ); - expect( wrapper.prop( 'width' ) ).toBe( 24 ); - expect( wrapper.prop( 'height' ) ).toBe( 24 ); + expect( icon ).toHaveAttribute( 'width', '24' ); + expect( icon ).toHaveAttribute( 'height', '24' ); } ); it( 'renders an svg element and override its width and height', () => { - const wrapper = shallow( + render( @@ -74,73 +70,26 @@ describe( 'Icon', () => { size={ 32 } /> ); + const icon = screen.getByTestId( testId ); - expect( wrapper.prop( 'width' ) ).toBe( 32 ); - expect( wrapper.prop( 'height' ) ).toBe( 32 ); + expect( icon ).toHaveAttribute( 'width', '32' ); + expect( icon ).toHaveAttribute( 'height', '32' ); } ); it( 'renders an svg element and does not override width and height if already specified', () => { - const wrapper = shallow( ); + render( ); + const icon = screen.getByTestId( testId ); - expect( wrapper.prop( 'width' ) ).toBe( 32 ); - expect( wrapper.prop( 'height' ) ).toBe( 32 ); + expect( icon ).toHaveAttribute( 'width', '32' ); + expect( icon ).toHaveAttribute( 'height', '32' ); } ); it( 'renders a component', () => { - class MyComponent extends Component { - render() { - return ; - } - } - const wrapper = shallow( ); - - expect( wrapper.name() ).toBe( 'MyComponent' ); - } ); - - describe( 'props passing', () => { - class MyComponent extends Component { - render() { - return ; - } - } - - describe.each( [ - [ 'dashicon', { icon: 'format-image' } ], - [ 'dashicon element', { icon: } ], - [ 'element', { icon: } ], - [ 'svg element', { icon: svg } ], - [ 'component', { icon: MyComponent } ], - ] )( '%s', ( label, props ) => { - it( 'should pass through size', () => { - if ( label === 'svg element' ) { - // Custom logic for SVG elements tested separately. - // - // See: `renders an svg element and passes the size as its width and height` - return; - } - - if ( [ 'dashicon', 'dashicon element' ].includes( label ) ) { - // `size` prop isn't passed through, since dashicon doesn't accept it. - return; - } - - const wrapper = shallow( ); - - expect( wrapper.prop( 'size' ) ).toBe( 32 ); - } ); - - it( 'should pass through all other props', () => { - const wrapper = shallow( - - ); + const MyComponent = () => ( + + ); + render( ); - expect( wrapper.prop( 'style' ) ).toBe( style ); - expect( wrapper.prop( 'className' ) ).toBe( className ); - } ); - } ); + expect( screen.getByTestId( testId ) ).toHaveClass( className ); } ); } );