Skip to content

Commit

Permalink
Replace render method with Ariakit's in custom select control (v1).
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniGuardiola committed Jul 25, 2024
1 parent aebd132 commit 0c64d63
Showing 1 changed file with 27 additions and 24 deletions.
51 changes: 27 additions & 24 deletions packages/components/src/custom-select-control/test/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/**
* External dependencies
*/
import { render, screen } from '@testing-library/react';
import { screen } from '@testing-library/react';
import { click, press, sleep, type, waitFor } from '@ariakit/test';
import { render } from '@ariakit/test/react';

/**
* WordPress dependencies
Expand Down Expand Up @@ -85,7 +86,7 @@ const ControlledCustomSelectControl = ( {

it( 'Should apply external controlled updates', async () => {
const mockOnChange = jest.fn();
const { rerender } = render(
await render(
<UncontrolledCustomSelectControl
{ ...props }
value={ props.options[ 0 ] }
Expand All @@ -101,7 +102,9 @@ it( 'Should apply external controlled updates', async () => {

expect( mockOnChange ).not.toHaveBeenCalled();

rerender(
// TODO: waiting on re-render support from Ariakit
// see: https://github.com/ariakit/ariakit/issues/3939#issuecomment-2251231903
await render(
<UncontrolledCustomSelectControl
{ ...props }
value={ props.options[ 1 ] }
Expand All @@ -124,7 +127,7 @@ describe.each( [

it( 'Should select the first option when no explicit initial value is passed without firing onChange', async () => {
const mockOnChange = jest.fn();
render( <Component { ...props } onChange={ mockOnChange } /> );
await render( <Component { ...props } onChange={ mockOnChange } /> );

expect(
screen.getByRole( 'combobox', {
Expand All @@ -140,7 +143,7 @@ describe.each( [

it( 'Should pick the initially selected option if the value prop is passed without firing onChange', async () => {
const mockOnChange = jest.fn();
render(
await render(
<Component
{ ...props }
onChange={ mockOnChange }
Expand All @@ -161,7 +164,7 @@ describe.each( [
} );

it( 'Should replace the initial selection when a new item is selected', async () => {
render( <Component { ...props } /> );
await render( <Component { ...props } /> );

const currentSelectedItem = screen.getByRole( 'combobox', {
expanded: false,
Expand Down Expand Up @@ -189,7 +192,7 @@ describe.each( [
} );

it( 'Should keep current selection if dropdown is closed without changing selection', async () => {
render( <Component { ...props } /> );
await render( <Component { ...props } /> );

const currentSelectedItem = screen.getByRole( 'combobox', {
expanded: false,
Expand Down Expand Up @@ -217,7 +220,7 @@ describe.each( [
} );

it( 'Should apply class only to options that have a className defined', async () => {
render( <Component { ...props } /> );
await render( <Component { ...props } /> );

await click(
screen.getByRole( 'combobox', {
Expand Down Expand Up @@ -251,7 +254,7 @@ describe.each( [
} );

it( 'Should apply styles only to options that have styles defined', async () => {
render( <Component { ...props } /> );
await render( <Component { ...props } /> );

await click(
screen.getByRole( 'combobox', {
Expand Down Expand Up @@ -285,7 +288,7 @@ describe.each( [
} );

it( 'does not show selected hint by default', async () => {
render(
await render(
<Component
{ ...props }
label="Custom select"
Expand All @@ -306,7 +309,7 @@ describe.each( [
} );

it( 'shows selected hint when showSelectedHint is set', async () => {
render(
await render(
<Component
{ ...props }
label="Custom select"
Expand All @@ -331,7 +334,7 @@ describe.each( [
} );

it( 'shows selected hint in list of options when added, regardless of showSelectedHint prop', async () => {
render(
await render(
<Component
{ ...props }
label="Custom select"
Expand All @@ -355,7 +358,7 @@ describe.each( [
it( 'Should return object onChange', async () => {
const mockOnChange = jest.fn();

render( <Component { ...props } onChange={ mockOnChange } /> );
await render( <Component { ...props } onChange={ mockOnChange } /> );

await click(
screen.getByRole( 'combobox', {
Expand Down Expand Up @@ -385,7 +388,7 @@ describe.each( [
it( 'Should return selectedItem object when specified onChange', async () => {
const mockOnChange = jest.fn();

render( <Component { ...props } onChange={ mockOnChange } /> );
await render( <Component { ...props } onChange={ mockOnChange } /> );

await sleep();
await press.Tab();
Expand All @@ -412,7 +415,7 @@ describe.each( [
it( "Should pass arbitrary props to onChange's selectedItem, but apply only style and className to DOM elements", async () => {
const onChangeMock = jest.fn();

render( <Component { ...props } onChange={ onChangeMock } /> );
await render( <Component { ...props } onChange={ onChangeMock } /> );

const currentSelectedItem = screen.getByRole( 'combobox', {
expanded: false,
Expand Down Expand Up @@ -449,8 +452,8 @@ describe.each( [
);
} );

it( 'Should label the component correctly even when the label is not visible', () => {
render( <Component { ...props } hideLabelFromVision /> );
it( 'Should label the component correctly even when the label is not visible', async () => {
await render( <Component { ...props } hideLabelFromVision /> );

expect(
screen.getByRole( 'combobox', {
Expand All @@ -463,7 +466,7 @@ describe.each( [
it( 'Captures the keypress event and does not let it propagate', async () => {
const onKeyDown = jest.fn();

render(
await render(
<div
// This role="none" is required to prevent an eslint warning about accessibility.
role="none"
Expand All @@ -487,7 +490,7 @@ describe.each( [
} );

it( 'Should be able to change selection using keyboard', async () => {
render( <Component { ...props } /> );
await render( <Component { ...props } /> );

const currentSelectedItem = screen.getByRole( 'combobox', {
expanded: false,
Expand All @@ -513,7 +516,7 @@ describe.each( [
} );

it( 'Should be able to type characters to select matching options', async () => {
render( <Component { ...props } /> );
await render( <Component { ...props } /> );

const currentSelectedItem = screen.getByRole( 'combobox', {
expanded: false,
Expand All @@ -534,7 +537,7 @@ describe.each( [
} );

it( 'Can change selection with a focused input and closed dropdown if typed characters match an option', async () => {
render( <Component { ...props } /> );
await render( <Component { ...props } /> );

const currentSelectedItem = screen.getByRole( 'combobox', {
expanded: false,
Expand Down Expand Up @@ -564,7 +567,7 @@ describe.each( [
} );

it( 'Can change selection with a focused input and closed dropdown while pressing arrow keys', async () => {
render( <Component { ...props } /> );
await render( <Component { ...props } /> );

const currentSelectedItem = screen.getByRole( 'combobox', {
expanded: false,
Expand All @@ -591,7 +594,7 @@ describe.each( [
} );

it( 'Should have correct aria-selected value for selections', async () => {
render( <Component { ...props } /> );
await render( <Component { ...props } /> );

const currentSelectedItem = screen.getByRole( 'combobox', {
expanded: false,
Expand Down Expand Up @@ -646,7 +649,7 @@ describe.each( [
const onFocusMock = jest.fn();
const onBlurMock = jest.fn();

render(
await render(
<>
<Component
{ ...props }
Expand Down

0 comments on commit 0c64d63

Please sign in to comment.