Skip to content

Commit

Permalink
SlotFill: add unit test for useSlotFills loop
Browse files Browse the repository at this point in the history
  • Loading branch information
jsnajdr committed Dec 20, 2024
1 parent 30b1f08 commit 4be209d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
14 changes: 14 additions & 0 deletions packages/components/src/slot-fill/test/__snapshots__/slot.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,20 @@ exports[`Slot bubblesVirtually true should unmount two slots with the same name
</div>
`;

exports[`Slot should not infinite loop with useSlotFills 1`] = `
<div>
<div>
<div>
fills:
1
</div>
<div>
fill content
</div>
</div>
</div>
`;

exports[`Slot should re-render Slot when not bubbling virtually 1`] = `
<div>
<div>
Expand Down
25 changes: 24 additions & 1 deletion packages/components/src/slot-fill/test/slot.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import userEvent from '@testing-library/user-event';
/**
* Internal dependencies
*/
import { Slot, Fill, Provider } from '../';
import { Slot, Fill, Provider, useSlotFills } from '../';

/**
* WordPress dependencies
Expand Down Expand Up @@ -381,4 +381,27 @@ describe( 'Slot', () => {
} );
}
);

it( 'should not infinite loop with useSlotFills', () => {
function App() {
// if `useSlotFills` triggers a state update every time the `Fill` is rerendered with
// new `children`, it will cause infinite rerender loop. This test checks we don't do that.
const fills = useSlotFills( 'editor' );
return (
<div>
<div>fills:{ fills?.length ?? 'none' }</div>
<Slot name="editor" />
<Fill name="editor">
<div>fill content</div>
</Fill>
</div>
);
}
const { container } = render(
<Provider>
<App />
</Provider>
);
expect( container ).toMatchSnapshot();
} );
} );

0 comments on commit 4be209d

Please sign in to comment.