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

Editor: Refactor PostSavedState tests to @testing-library/react #43769

Merged
merged 1 commit into from
Sep 1, 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,34 +1,43 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`PostSavedState returns a disabled button if the post is not saveable 1`] = `
<ForwardRef(Button)
aria-disabled={true}
icon={
<SVG
viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg"
>
<Path
d="M17.3 10.1c0-2.5-2.1-4.4-4.8-4.4-2.2 0-4.1 1.4-4.6 3.3h-.2C5.7 9 4 10.7 4 12.8c0 2.1 1.7 3.8 3.7 3.8h9c1.8 0 3.2-1.5 3.2-3.3.1-1.6-1.1-2.9-2.6-3.2zm-.5 5.1h-4v-2.4L14 14l1-1-3-3-3 3 1 1 1.2-1.2v2.4H7.7c-1.2 0-2.2-1.1-2.2-2.3s1-2.4 2.2-2.4H9l.3-1.1c.4-1.3 1.7-2.2 3.2-2.2 1.8 0 3.3 1.3 3.3 2.9v1.3l1.3.2c.8.1 1.4.9 1.4 1.8 0 1-.8 1.8-1.7 1.8z"
/>
</SVG>
}
label="Save draft"
shortcut="Ctrl+S"
/>
<button
aria-disabled="true"
aria-label="Save draft"
class="components-button has-text has-icon"
type="button"
>
<svg
aria-hidden="true"
focusable="false"
height="24"
viewBox="0 0 24 24"
width="24"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M17.3 10.1c0-2.5-2.1-4.4-4.8-4.4-2.2 0-4.1 1.4-4.6 3.3h-.2C5.7 9 4 10.7 4 12.8c0 2.1 1.7 3.8 3.7 3.8h9c1.8 0 3.2-1.5 3.2-3.3.1-1.6-1.1-2.9-2.6-3.2zm-.5 5.1h-4v-2.4L14 14l1-1-3-3-3 3 1 1 1.2-1.2v2.4H7.7c-1.2 0-2.2-1.1-2.2-2.3s1-2.4 2.2-2.4H9l.3-1.1c.4-1.3 1.7-2.2 3.2-2.2 1.8 0 3.3 1.3 3.3 2.9v1.3l1.3.2c.8.1 1.4.9 1.4 1.8 0 1-.8 1.8-1.7 1.8z"
/>
</svg>
</button>
`;

exports[`PostSavedState returns a switch to draft link if the post is published 1`] = `<WithSelect(WithDispatch(PostSwitchToDraftButton)) />`;
exports[`PostSavedState returns a switch to draft link if the post is published 1`] = `
<button
class="components-button editor-post-switch-to-draft is-tertiary"
type="button"
>
Switch to draft
</button>
`;

exports[`PostSavedState should return Save button if edits to be saved 1`] = `
<ForwardRef(Button)
aria-disabled={false}
className="editor-post-save-draft"
label="Save draft"
onClick={[Function]}
shortcut="Ctrl+S"
variant="tertiary"
<button
aria-disabled="false"
aria-label="Save draft"
class="components-button editor-post-save-draft is-tertiary"
type="button"
>
Save draft
</ForwardRef(Button)>
</button>
`;
45 changes: 31 additions & 14 deletions packages/editor/src/components/post-saved-state/test/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/**
* External dependencies
*/
import { mount, shallow } from 'enzyme';
import { render, screen, within } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

/**
* WordPress dependencies
Expand All @@ -19,6 +20,7 @@ const mockSavePost = jest.fn();
jest.mock( '@wordpress/data/src/components/use-dispatch', () => {
return {
useDispatch: () => ( { savePost: mockSavePost } ),
useDispatchWithMap: jest.fn(),
};
} );

Expand All @@ -34,6 +36,10 @@ jest.mock( '@wordpress/compose/src/hooks/use-viewport-match', () => {
return mock;
} );

jest.mock( '@wordpress/icons/src/icon', () => () => (
<div data-testid="test-icon" />
) );
Comment on lines +39 to +41
Copy link
Contributor

@ciampo ciampo Sep 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the issue preventing us from using the real Icon component?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't find a good way to query for the icon. Any recommendations?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mhh, I can't find either. Ideally we could at least assign a default role="presentation" to these icons, or at otherwise label them with some accessible text. But it's not a topic for this PR

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the Button in PostSavedDate can render 2 icons at the same time (one as children and one via the icon prop) — that's weird


describe( 'PostSavedState', () => {
it( 'should display saving while save in progress, even if not saveable', () => {
useSelect.mockImplementation( () => ( {
Expand All @@ -43,9 +49,9 @@ describe( 'PostSavedState', () => {
isSaving: true,
} ) );

const wrapper = mount( <PostSavedState /> );
render( <PostSavedState /> );

expect( wrapper.text() ).toContain( 'Saving' );
expect( screen.getByText( 'Saving' ) ).toBeVisible();
} );

it( 'returns a disabled button if the post is not saveable', () => {
Expand All @@ -56,19 +62,19 @@ describe( 'PostSavedState', () => {
isSaving: false,
} ) );

const wrapper = shallow( <PostSavedState /> );
render( <PostSavedState /> );

expect( wrapper ).toMatchSnapshot();
expect( screen.getByRole( 'button' ) ).toMatchSnapshot();
} );

it( 'returns a switch to draft link if the post is published', () => {
useSelect.mockImplementation( () => ( {
isPublished: true,
} ) );

const wrapper = shallow( <PostSavedState /> );
render( <PostSavedState /> );

expect( wrapper ).toMatchSnapshot();
expect( screen.getByRole( 'button' ) ).toMatchSnapshot();
} );

it( 'should return Saved text if not new and not dirty', () => {
Expand All @@ -79,13 +85,19 @@ describe( 'PostSavedState', () => {
isSaving: false,
} ) );

const wrapper = shallow( <PostSavedState /> );
render( <PostSavedState /> );

const button = screen.getByRole( 'button' );

expect( wrapper.childAt( 0 ).name() ).toBe( 'Icon' );
expect( wrapper.childAt( 1 ).text() ).toBe( 'Saved' );
expect( within( button ).getByTestId( 'test-icon' ) ).toBeVisible();
expect( within( button ).getByText( 'Saved' ) ).toBeVisible();
} );

it( 'should return Save button if edits to be saved', () => {
it( 'should return Save button if edits to be saved', async () => {
const user = userEvent.setup( {
advanceTimers: jest.advanceTimersByTime,
} );

useSelect.mockImplementation( () => ( {
isDirty: true,
isNew: false,
Expand All @@ -96,11 +108,16 @@ describe( 'PostSavedState', () => {
// Simulate the viewport being considered large.
useViewportMatch.mockImplementation( () => true );

const wrapper = shallow( <PostSavedState /> );
render( <PostSavedState /> );

const button = screen.getByRole( 'button' );

expect( button ).toMatchSnapshot();

await user.click( button );

expect( wrapper ).toMatchSnapshot();
wrapper.simulate( 'click', {} );
expect( mockSavePost ).toHaveBeenCalled();

// Regression: Verify the event object is not passed to prop callback.
expect( mockSavePost.mock.calls[ 0 ] ).toEqual( [] );
} );
Expand Down