-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…44084) * Components: Refactor Slot Fill tests to @testing-library/react * Add changelog
- Loading branch information
Showing
2 changed files
with
70 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} ); | ||
} ); |