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

CheckboxControl: Add unit tests #41165

Merged
merged 6 commits into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 4 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Internal

- `CheckboxControl`: Add unit tests ([#41165](https://github.com/WordPress/gutenberg/pull/41165)).

## 19.11.0 (2022-05-18)

### Enhancements
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`CheckboxControl Basic rendering should render the indeterminate icon when in the indeterminate state 1`] = `
Snapshot Diff:
- First value
+ Second value

@@ -8,17 +8,31 @@
<span
class="components-checkbox-control__input-container"
>
<input
class="components-checkbox-control__input"
- id="inspector-checkbox-control-5"
+ id="inspector-checkbox-control-6"
type="checkbox"
value="1"
+ />
+ <svg
+ aria-hidden="true"
+ class="components-checkbox-control__indeterminate"
+ focusable="false"
+ height="24"
+ role="presentation"
+ viewBox="0 0 24 24"
+ width="24"
+ xmlns="http://www.w3.org/2000/svg"
+ >
+ <path
+ d="M7 11.5h10V13H7z"
/>
+ </svg>
</span>
<label
class="components-checkbox-control__label"
- for="inspector-checkbox-control-5"
+ for="inspector-checkbox-control-6"
/>
</div>
</div>
</div>
`;
109 changes: 109 additions & 0 deletions packages/components/src/checkbox-control/test/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
* External dependencies
*/
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { noop } from 'lodash';

/**
* WordPress dependencies
*/
import { useState } from '@wordpress/element';

/**
* Internal dependencies
*/
import BaseCheckboxControl from '..';
import type { CheckboxControlProps } from '../types';

const getInput = () => screen.getByRole( 'checkbox' ) as HTMLInputElement;

const CheckboxControl = ( props: Omit< CheckboxControlProps, 'onChange' > ) => {
return <BaseCheckboxControl onChange={ noop } { ...props } />;
};

const ControlledCheckboxControl = ( { onChange }: CheckboxControlProps ) => {
const [ isChecked, setChecked ] = useState( false );
return (
<BaseCheckboxControl
checked={ isChecked }
onChange={ ( value ) => {
setChecked( value );
onChange( value );
} }
/>
);
};

describe( 'CheckboxControl', () => {
describe( 'Basic rendering', () => {
it( 'should render', () => {
render( <CheckboxControl /> );
expect( getInput() ).not.toBeNull();
} );

it( 'should render an unchecked `checkbox` by default', () => {
render( <CheckboxControl /> );
expect( getInput() ).toHaveProperty( 'checked', false );
} );

it( 'should render an checked `checkbox` when `checked={ true }`', () => {
render( <CheckboxControl checked /> );
expect( getInput() ).toHaveProperty( 'checked', true );
} );

it( 'should render label', () => {
render( <CheckboxControl label="Hello" /> );

const label = screen.getByText( 'Hello' );
expect( label ).toBeTruthy();
ciampo marked this conversation as resolved.
Show resolved Hide resolved
} );

it( 'should render a checkbox in an indeterminate state', () => {
render( <CheckboxControl indeterminate /> );
expect( getInput() ).toHaveProperty( 'indeterminate', true );
} );

it( 'should render the indeterminate icon when in the indeterminate state', () => {
const { container: containerDefault } = render(
<CheckboxControl />
);

const { container: containerIndeterminate } = render(
<CheckboxControl indeterminate />
);

// Expect the diff snapshot to be mostly about the indeterminate icon
expect( containerDefault ).toMatchDiffSnapshot(
containerIndeterminate
);
} );
} );

describe( 'Value', () => {
it( 'should flip the checked property when clicked', async () => {
const user = userEvent.setup( {
advanceTimers: jest.advanceTimersByTime,
} );

let state = false;
const setState = jest.fn(
( nextState: boolean ) => ( state = nextState )
);

render( <ControlledCheckboxControl onChange={ setState } /> );

const input = getInput();

await user.click( input );
expect( input ).toHaveProperty( 'checked', true );
expect( state ).toBe( true );

await user.click( input );
expect( input ).toHaveProperty( 'checked', false );
expect( state ).toBe( false );

expect( setState ).toHaveBeenCalledTimes( 2 );
} );
} );
} );