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

Improve LineHeightControl unit tests #61337

Merged
merged 1 commit into from
May 3, 2024
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
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/**
* External dependencies
*/
import { act, fireEvent, render, screen } from '@testing-library/react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

/**
* WordPress dependencies
*/
import { useState } from '@wordpress/element';
import { UP, DOWN } from '@wordpress/keycodes';

/**
* Internal dependencies
Expand All @@ -29,35 +29,37 @@ const ControlledLineHeightControl = () => {
};

describe( 'LineHeightControl', () => {
it( 'should immediately step up from the default value if up-arrowed from an unset state', () => {
it( 'should immediately step up from the default value if up-arrowed from an unset state', async () => {
const user = userEvent.setup();
render( <ControlledLineHeightControl /> );
const input = screen.getByRole( 'spinbutton' );
act( () => input.focus() );
fireEvent.keyDown( input, { keyCode: UP } );
await user.click( input );
await user.keyboard( '{ArrowUp}' );
expect( input ).toHaveValue( BASE_DEFAULT_VALUE + SPIN );
} );

it( 'should immediately step down from the default value if down-arrowed from an unset state', () => {
it( 'should immediately step down from the default value if down-arrowed from an unset state', async () => {
const user = userEvent.setup();
render( <ControlledLineHeightControl /> );
const input = screen.getByRole( 'spinbutton' );
act( () => input.focus() );
fireEvent.keyDown( input, { keyCode: DOWN } );
await user.click( input );
await user.keyboard( '{ArrowDown}' );
expect( input ).toHaveValue( BASE_DEFAULT_VALUE - SPIN );
} );

it( 'should immediately step up from the default value if spin button up was clicked from an unset state', () => {
it( 'should immediately step up from the default value if spin button up was clicked from an unset state', async () => {
const user = userEvent.setup();
render( <ControlledLineHeightControl /> );
const input = screen.getByRole( 'spinbutton' );
act( () => input.focus() );
fireEvent.change( input, { target: { value: 0.1 } } ); // simulates click on spin button up
await user.click( screen.getByRole( 'button', { name: 'Increment' } ) );
expect( input ).toHaveValue( BASE_DEFAULT_VALUE + SPIN );
} );

it( 'should immediately step down from the default value if spin button down was clicked from an unset state', () => {
it( 'should immediately step down from the default value if spin button down was clicked from an unset state', async () => {
const user = userEvent.setup();
render( <ControlledLineHeightControl /> );
const input = screen.getByRole( 'spinbutton' );
act( () => input.focus() );
fireEvent.change( input, { target: { value: 0 } } ); // simulates click on spin button down
await user.click( screen.getByRole( 'button', { name: 'Decrement' } ) );
expect( input ).toHaveValue( BASE_DEFAULT_VALUE - SPIN );
} );
} );
Loading