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

Block Editor: Refactor DefaultBlockAppender tests to @testing-library/react #43990

Merged
merged 1 commit into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
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,73 +1,55 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`DefaultBlockAppender should append a default block when input focused 1`] = `
<div
className="block-editor-default-block-appender has-visible-prompt"
data-root-client-id=""
>
<p
aria-label="Add default block"
className="block-editor-default-block-appender__content"
onClick={[Function]}
onFocus={[Function]}
onKeyDown={[Function]}
role="button"
tabIndex="0"
<div>
<div
class="block-editor-default-block-appender has-visible-prompt"
data-root-client-id=""
>
Type / to choose a block
</p>
<WithSelect(WithDispatch(IfCondition(Inserter)))
__experimentalIsQuick={true}
isAppender={true}
position="bottom right"
/>
<p
aria-label="Add default block"
class="block-editor-default-block-appender__content"
role="button"
tabindex="0"
>
Type / to choose a block
</p>
</div>
</div>
`;

exports[`DefaultBlockAppender should match snapshot 1`] = `
<div
className="block-editor-default-block-appender has-visible-prompt"
data-root-client-id=""
>
<p
aria-label="Add default block"
className="block-editor-default-block-appender__content"
onClick={[Function]}
onFocus={[Function]}
onKeyDown={[Function]}
role="button"
tabIndex="0"
<div>
<div
class="block-editor-default-block-appender has-visible-prompt"
data-root-client-id=""
>
Type / to choose a block
</p>
<WithSelect(WithDispatch(IfCondition(Inserter)))
__experimentalIsQuick={true}
isAppender={true}
position="bottom right"
/>
<p
aria-label="Add default block"
class="block-editor-default-block-appender__content"
role="button"
tabindex="0"
>
Type / to choose a block
</p>
</div>
</div>
`;

exports[`DefaultBlockAppender should optionally show without prompt 1`] = `
<div
className="block-editor-default-block-appender"
data-root-client-id=""
>
<p
aria-label="Add default block"
className="block-editor-default-block-appender__content"
onClick={[Function]}
onFocus={[Function]}
onKeyDown={[Function]}
role="button"
tabIndex="0"
<div>
<div
class="block-editor-default-block-appender"
data-root-client-id=""
>

</p>
<WithSelect(WithDispatch(IfCondition(Inserter)))
__experimentalIsQuick={true}
isAppender={true}
position="bottom right"
/>
<p
aria-label="Add default block"
class="block-editor-default-block-appender__content"
role="button"
tabindex="0"
>

</p>
</div>
</div>
`;
Original file line number Diff line number Diff line change
@@ -1,50 +1,64 @@
/**
* External dependencies
*/
import { shallow } from 'enzyme';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

/**
* Internal dependencies
*/
import { DefaultBlockAppender, ZWNBSP } from '../';

describe( 'DefaultBlockAppender', () => {
const expectOnAppendCalled = ( onAppend ) => {
expect( onAppend ).toHaveBeenCalledTimes( 1 );
expect( onAppend ).toHaveBeenCalledWith();
};

it( 'should match snapshot', () => {
const onAppend = jest.fn();
const wrapper = shallow(

const { container } = render(
<DefaultBlockAppender onAppend={ onAppend } showPrompt />
);

expect( wrapper ).toMatchSnapshot();
expect( container ).toMatchSnapshot();
} );

it( 'should append a default block when input focused', () => {
it( 'should append a default block when input focused', async () => {
const user = userEvent.setup( {
advanceTimers: jest.advanceTimersByTime,
} );
const onAppend = jest.fn();
const wrapper = shallow(

const { container } = render(
<DefaultBlockAppender onAppend={ onAppend } showPrompt />
);

wrapper.find( 'p' ).simulate( 'click' );
await user.click(
screen.getByRole( 'button', { name: 'Add default block' } )
);

expect( wrapper ).toMatchSnapshot();
expect( container ).toMatchSnapshot();

expectOnAppendCalled( onAppend );
// Called once for focusing and once for clicking.
expect( onAppend ).toHaveBeenCalledTimes( 2 );
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
expect( onAppend ).toHaveBeenCalledWith();
} );

it( 'should optionally show without prompt', () => {
it( 'should optionally show without prompt', async () => {
const user = userEvent.setup( {
advanceTimers: jest.advanceTimersByTime,
} );
const onAppend = jest.fn();
const wrapper = shallow(

const { container } = render(
<DefaultBlockAppender onAppend={ onAppend } showPrompt={ false } />
);
const input = wrapper.find( 'p' );

expect( input.prop( 'children' ) ).toEqual( ZWNBSP );
const appender = screen.getByRole( 'button', {
name: 'Add default block',
} );

await user.click( appender );

expect( appender ).toContainHTML( ZWNBSP );

expect( wrapper ).toMatchSnapshot();
expect( container ).toMatchSnapshot();
} );
} );