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 Slot/Fill tests to @testing-library/react #44084

Merged
merged 2 commits into from
Sep 13, 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 @@ -53,6 +53,7 @@
- `withFilters`: Refactor tests to `@testing-library/react` ([#44017](https://github.com/WordPress/gutenberg/pull/44017)).
- `IsolatedEventContainer`: Refactor tests to `@testing-library/react` ([#44073](https://github.com/WordPress/gutenberg/pull/44073)).
- `KeyboardShortcuts`: Refactor tests to `@testing-library/react` ([#44075](https://github.com/WordPress/gutenberg/pull/44075)).
- `Slot`/`Fill`: Refactor tests to `@testing-library/react` ([#44084](https://github.com/WordPress/gutenberg/pull/44084)).

## 20.0.0 (2022-08-24)

Expand Down
81 changes: 69 additions & 12 deletions packages/components/src/slot-fill/test/index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,85 @@
/**
* External dependencies
*/
import { shallow } from 'enzyme';
import { render, screen, within } from '@testing-library/react';

/**
* Internal dependencies
*/
import { createSlotFill, Fill, Slot } from '../';
import { createSlotFill, Provider as SlotFillProvider } from '../';

describe( 'createSlotFill', () => {
const SLOT_NAME = 'MySlotFill';
const MySlotFill = createSlotFill( SLOT_NAME );
test( 'should render all slot fills in order of rendering', () => {
const PostSidebar = createSlotFill( 'PostSidebar' );

test( 'should match snapshot for Fill', () => {
const wrapper = shallow( <MySlotFill.Fill /> );
render(
<SlotFillProvider>
<PostSidebar.Fill>
<p>Post Section 1</p>
</PostSidebar.Fill>
<PostSidebar.Fill>
<p>Post Section 2</p>
</PostSidebar.Fill>
<div title="Post Sidebar">
<PostSidebar.Slot />
</div>
</SlotFillProvider>
);

expect( wrapper.type() ).toBe( Fill );
expect( wrapper.prop( 'name' ) ).toBe( SLOT_NAME );
const postSidebar = screen.getByTitle( 'Post Sidebar' );
const postSections =
within( postSidebar ).getAllByText( /Post Section \d/ );

expect( postSidebar ).toBeVisible();
expect( postSections ).toHaveLength( 2 );
postSections.forEach( ( postSection, index ) => {
expect( postSection ).toBeVisible();
expect( postSection ).toHaveTextContent(
`Post Section ${ index + 1 }`
);
} );
} );

test( 'should match snapshot for Slot', () => {
const wrapper = shallow( <MySlotFill.Slot /> );
test( 'should support separate multiple slots and fills', () => {
const PostSidebar = createSlotFill( 'PostSidebar' );
const PageSidebar = createSlotFill( 'PageSidebar' );

render(
<SlotFillProvider>
<PostSidebar.Fill>
<p>Post Section</p>
</PostSidebar.Fill>
<PageSidebar.Fill>
<p>Page Section</p>
</PageSidebar.Fill>
<div title="Post Sidebar">
<PostSidebar.Slot />
</div>
<div title="Page Sidebar">
<PageSidebar.Slot />
</div>
</SlotFillProvider>
);

const postSidebar = screen.getByTitle( 'Post Sidebar' );

expect( postSidebar ).toBeVisible();
expect(
within( postSidebar ).getByText( 'Post Section' )
).toBeVisible();

const pageSidebar = screen.getByTitle( 'Page Sidebar' );

expect( pageSidebar ).toBeVisible();
expect(
within( pageSidebar ).getByText( 'Page Section' )
).toBeVisible();

expect( wrapper.type() ).toBe( Slot );
expect( wrapper.prop( 'name' ) ).toBe( SLOT_NAME );
expect(
within( postSidebar ).queryByText( 'Page Section' )
).not.toBeInTheDocument();
expect(
within( pageSidebar ).queryByText( 'Post Section' )
).not.toBeInTheDocument();
} );
} );