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

Components: Refactor withFocusReturn tests to RTL #45012

Merged
merged 4 commits into from
Oct 18, 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
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- `Sandbox`: Use `toString` to create observe and resize script string ([#42872](https://github.com/WordPress/gutenberg/pull/42872)).
- `Navigator`: refactor unit tests to TypeScript and to `user-event` ([#44970](https://github.com/WordPress/gutenberg/pull/44970)).
- `Navigator`: Refactor Storybook code to TypeScript and controls ([#44979](https://github.com/WordPress/gutenberg/pull/44979)).
- `withFocusReturn`: Refactor tests to `@testing-library/react` ([#45012](https://github.com/WordPress/gutenberg/pull/45012)).

## 21.2.0 (2022-10-05)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* External dependencies
*/
import renderer from 'react-test-renderer';
import { render, fireEvent } from '@testing-library/react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

/**
* WordPress dependencies
Expand All @@ -16,9 +16,14 @@ import withFocusReturn from '../';

class Test extends Component {
render() {
const { className, focusHistory } = this.props;
return (
<div className="test">
<textarea />
<div
className={ className }
data-testid="test-element"
data-focus-history={ focusHistory }
>
<textarea aria-label="Textarea" />
</div>
);
}
Expand All @@ -41,32 +46,25 @@ describe( 'withFocusReturn()', () => {
} );

it( 'should render a basic Test component inside the HOC', () => {
const renderedComposite = renderer.create( <Composite /> );
const wrappedElement = renderedComposite.root.findByType( Test );
const wrappedElementShallow = wrappedElement.children[ 0 ];
expect( wrappedElementShallow.props.className ).toBe( 'test' );
expect( wrappedElementShallow.type ).toBe( 'div' );
expect( wrappedElementShallow.children[ 0 ].type ).toBe(
'textarea'
);
render( <Composite /> );

expect( screen.getByTestId( 'test-element' ) ).toBeVisible();
} );

it( 'should pass own props through to the wrapped element', () => {
const renderedComposite = renderer.create(
<Composite test="test" />
render( <Composite className="test" /> );

expect( screen.getByTestId( 'test-element' ) ).toHaveClass(
'test'
);
const wrappedElement = renderedComposite.root.findByType( Test );
// Ensure that the wrapped Test element has the appropriate props.
expect( wrappedElement.props.test ).toBe( 'test' );
} );

it( 'should not pass any withFocusReturn context props through to the wrapped element', () => {
const renderedComposite = renderer.create(
<Composite test="test" />
render( <Composite className="test" /> );

expect( screen.getByTestId( 'test-element' ) ).not.toHaveAttribute(
'data-focus-history'
);
const wrappedElement = renderedComposite.root.findByType( Test );
// Ensure that the wrapped Test element has the appropriate props.
expect( wrappedElement.props.focusHistory ).toBeUndefined();
} );

it( 'should not switch focus back to the bound focus element', () => {
Expand All @@ -85,17 +83,23 @@ describe( 'withFocusReturn()', () => {
expect( switchFocusTo ).toHaveFocus();
} );

it( 'should switch focus back when unmounted while having focus', () => {
it( 'should switch focus back when unmounted while having focus', async () => {
const user = userEvent.setup( {
advanceTimers: jest.advanceTimersByTime,
} );

const { container, unmount } = render( <Composite />, {
container: document.body.appendChild(
document.createElement( 'div' )
),
} );

const textarea = container.querySelector( 'textarea' );
fireEvent.focusIn( textarea, { target: textarea } );
textarea.focus();
expect( textarea ).toHaveFocus();
// Click inside the textarea to focus it.
await user.click(
screen.getByRole( 'textbox', {
name: 'Textarea',
} )
);

// Should return to the activeElement saved with this component.
unmount();
Expand Down