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: Add test on SlotFill #21226

Merged
merged 2 commits into from
Mar 30, 2020
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
Expand Up @@ -25,7 +25,6 @@ function useSlotRegistry() {

const unregisterSlot = useCallback( ( name, ref ) => {
setSlots( ( prevSlots ) => {
// eslint-disable-next-line no-unused-vars
const { [ name ]: slot, ...nextSlots } = prevSlots;
Copy link
Member Author

Choose a reason for hiding this comment

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

We're using the slot variable, so the comment above is unnecessary.

// Make sure we're not unregistering a slot registered by another element
// See https://github.com/WordPress/gutenberg/pull/19242#issuecomment-590295412
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Array [
]
`;

exports[`Slot bubblesVirtually false should unmount two slots with the same name 1`] = `"<div data-position=\\"first\\"></div><div data-position=\\"second\\"></div>"`;

exports[`Slot bubblesVirtually true should subsume another slot by the same name 1`] = `
Array [
<div
Expand Down Expand Up @@ -54,6 +56,8 @@ Array [
]
`;

exports[`Slot bubblesVirtually true should unmount two slots with the same name 1`] = `"<div data-position=\\"first\\"></div><div data-position=\\"second\\"></div>"`;

exports[`Slot should re-render Slot when not bubbling virtually 1`] = `
Array [
<div>
Expand Down
65 changes: 65 additions & 0 deletions packages/components/src/slot-fill/test/slot.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*/
import { isEmpty } from 'lodash';
import ReactTestRenderer from 'react-test-renderer';
import { render, unmountComponentAtNode } from 'react-dom';
import { act } from 'react-dom/test-utils';

/**
* Internal dependencies
Expand Down Expand Up @@ -36,6 +38,20 @@ class Filler extends Component {
}
}

let container = null;
beforeEach( () => {
// setup a DOM element as a render target
container = document.createElement( 'div' );
document.body.appendChild( container );
} );

afterEach( () => {
// cleanup on exiting
unmountComponentAtNode( container );
container.remove();
container = null;
} );

describe( 'Slot', () => {
it( 'should render empty Fills', () => {
const tree = ReactTestRenderer.create(
Expand Down Expand Up @@ -260,6 +276,55 @@ describe( 'Slot', () => {

expect( testRenderer.toJSON() ).toMatchSnapshot();
} );

it( 'should unmount two slots with the same name', () => {
act( () => {
render(
<Provider>
<div data-position="first">
<Slot
name="egg"
bubblesVirtually={ bubblesVirtually }
/>
</div>
<div data-position="second">
<Slot
name="egg"
bubblesVirtually={ bubblesVirtually }
/>
</div>
<Fill name="egg">Content</Fill>
</Provider>,
container
);
} );
act( () => {
render(
<Provider>
<div data-position="first">
<Slot
name="egg"
bubblesVirtually={ bubblesVirtually }
/>
</div>
<div data-position="second" />
<Fill name="egg">Content</Fill>
</Provider>,
container
);
} );
act( () => {
render(
<Provider>
<div data-position="first" />
<div data-position="second" />
<Fill name="egg">Content</Fill>
</Provider>,
container
);
} );
expect( container.innerHTML ).toMatchSnapshot();
} );
}
);
} );