From 7e629aa9fdf9f20cb4c1bf082293710b42625157 Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Thu, 4 Aug 2022 16:06:42 +0300 Subject: [PATCH 1/4] Components: Refactor Button tests to @testing-library/react --- packages/components/src/button/test/index.js | 304 ++++++++++--------- 1 file changed, 168 insertions(+), 136 deletions(-) diff --git a/packages/components/src/button/test/index.js b/packages/components/src/button/test/index.js index 9d2b9f5383ceab..b0248f48d0388c 100644 --- a/packages/components/src/button/test/index.js +++ b/packages/components/src/button/test/index.js @@ -1,8 +1,9 @@ /** * External dependencies */ -import { shallow } from 'enzyme'; +import { render, screen } from '@testing-library/react'; import TestUtils from 'react-dom/test-utils'; + /** * WordPress dependencies */ @@ -12,172 +13,197 @@ import { plusCircle } from '@wordpress/icons'; /** * Internal dependencies */ -import ButtonWithForwardedRef, { Button } from '../'; -import { VisuallyHidden } from '../../visually-hidden'; +import Button from '../'; + +jest.mock( '../../icon', () => () =>
); +jest.mock( '../../tooltip', () => ( { text, children } ) => ( +
+ { children } +
+) ); +jest.mock( '../../visually-hidden', () => ( { + __esModule: true, + VisuallyHidden: ( { children } ) => ( +
{ children }
+ ), +} ) ); describe( 'Button', () => { describe( 'basic rendering', () => { it( 'should render a button element with only one class', () => { - const button = shallow( - ).find( 'Tooltip' ); + ); - expect( buttonTooltip.prop( 'text' ) ).toBe( 'Description text' ); + expect( screen.getByTestId( 'test-tooltip' ) ).toHaveAttribute( + 'title', + 'Description text' + ); } ); it( 'should allow tooltip disable', () => { - const iconButton = shallow( + const button = render( - ).find( 'button' ); - expect( iconButton.name() ).toBe( 'button' ); + ).container.firstChild; + + expect( button.tagName ).toBe( 'BUTTON' ); } ); it( 'should force showing the tooltip even if icon and children defined', () => { - const iconButton = shallow( + const tooltip = render( - ).find( 'Tooltip' ); - expect( iconButton.name() ).toBe( 'Tooltip' ); + ).container.firstChild; + + expect( tooltip ).toBe( screen.getByTestId( 'test-tooltip' ) ); } ); } ); describe( 'with href property', () => { it( 'should render a link instead of a button with href prop', () => { - const button = shallow( - - ).container.firstChild; + ); + const button = screen.getByRole( 'button' ); expect( button.tagName ).toBe( 'BUTTON' ); } ); @@ -301,28 +305,32 @@ describe( 'Button', () => { describe( 'deprecated props', () => { it( 'should not break when the legacy isPrimary prop is passed', () => { - const button = render( ); - expect( screen.getByTestId( 'test-tooltip' ) ).toHaveAttribute( - 'title', - 'Description text' - ); + expect( + screen.getByRole( 'button', { + description: 'Description text', + } ) + ).toBeVisible(); } ); - it( 'should allow tooltip disable', () => { + it( 'should allow tooltip disable', async () => { render( ); - const button = screen.getByRole( 'button' ); - expect( button.tagName ).toBe( 'BUTTON' ); + expect( screen.queryByText( 'WordPress' ) ).not.toBeInTheDocument(); + + await screen.getByRole( 'button', { name: 'WordPress' } ).focus(); + + expect( screen.queryByText( 'WordPress' ) ).not.toBeInTheDocument(); } ); - it( 'should force showing the tooltip even if icon and children defined', () => { - const tooltip = render( + it( 'should force showing the tooltip even if icon and children defined', async () => { + render( - ).container.firstChild; + ); + + expect( screen.queryByText( 'WordPress' ) ).not.toBeInTheDocument(); - expect( tooltip ).toBe( screen.getByTestId( 'test-tooltip' ) ); + await screen.getByRole( 'button', { name: 'WordPress' } ).focus(); + + expect( screen.getByText( 'WordPress' ) ).toBeVisible(); } ); } ); describe( 'with href property', () => { it( 'should render a link instead of a button with href prop', () => { - const link = render(