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

Components: Refactor Guide PageControl tests to @testing-library/react #43148

Merged
merged 2 commits into from
Aug 11, 2022
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
31 changes: 14 additions & 17 deletions packages/components/src/guide/test/page-control.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
/**
* External dependencies
*/
import { shallow } from 'enzyme';

/**
* WordPress dependencies
*/
import { Button } from '@wordpress/components';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

/**
* Internal dependencies
Expand All @@ -15,29 +11,30 @@ import PageControl from '../page-control';

describe( 'PageControl', () => {
it( 'renders an empty list when there are no pages', () => {
const wrapper = shallow(
<PageControl currentPage={ 0 } numberOfPages={ 0 } />
);
expect( wrapper.find( Button ) ).toHaveLength( 0 );
render( <PageControl currentPage={ 0 } numberOfPages={ 0 } /> );
expect( screen.queryByRole( 'button' ) ).not.toBeInTheDocument();
} );

it( 'renders a button for each page', () => {
const wrapper = shallow(
<PageControl currentPage={ 0 } numberOfPages={ 5 } />
);
expect( wrapper.find( Button ) ).toHaveLength( 5 );
render( <PageControl currentPage={ 0 } numberOfPages={ 5 } /> );
expect( screen.getAllByRole( 'button' ) ).toHaveLength( 5 );
} );

it( 'sets the current page when a button is clicked', () => {
it( 'sets the current page when a button is clicked', async () => {
const user = userEvent.setup( {
advanceTimers: jest.advanceTimersByTime,
} );
const setCurrentPage = jest.fn();
const wrapper = shallow(
render(
<PageControl
currentPage={ 0 }
numberOfPages={ 2 }
setCurrentPage={ setCurrentPage }
/>
);
wrapper.find( Button ).at( 1 ).simulate( 'click' );

await user.click( screen.getAllByRole( 'button' )[ 1 ] );

expect( setCurrentPage ).toHaveBeenCalledWith( 1 );
} );
} );