Skip to content

Commit

Permalink
Move the setup to initialise method rather than an effect
Browse files Browse the repository at this point in the history
  • Loading branch information
glendaviesnz committed Mar 26, 2023
1 parent 7a2b353 commit b0f0746
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions test/integration/helpers/integration-test-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,33 +58,16 @@ export async function selectBlock( name, screen ) {
await userEvent.click( screen.getByLabelText( name ) );
}

export function Editor( { testBlocks, settings = {}, useCoreBlocks = true } ) {
export function Editor( { testBlocks, settings = {} } ) {
const [ currentBlocks, updateBlocks ] = useState( testBlocks );

useEffect( () => {
if ( useCoreBlocks ) {
registerCoreBlocks();
}
return () => {
getBlockTypes().forEach( ( { name } ) =>
unregisterBlockType( name )
);
};
}, [ useCoreBlocks ] );

useEffect( () => {
const blocks = Array.isArray( testBlocks )
? testBlocks
: [ testBlocks ];
const newBlocks = blocks.map( ( testBlock ) =>
createBlock(
testBlock.name,
testBlock.attributes,
testBlock.innerBlocks
)
);
updateBlocks( newBlocks );
}, [ testBlocks ] );
}, [] );

return (
<ShortcutProvider>
Expand Down Expand Up @@ -112,8 +95,23 @@ export function Editor( { testBlocks, settings = {}, useCoreBlocks = true } ) {
);
}

export async function initializeEditor( props ) {
export async function initializeEditor( {
useCoreBlocks = true,
testBlocks,
...props
} ) {
if ( useCoreBlocks ) {
registerCoreBlocks();
}
const blocks = Array.isArray( testBlocks ) ? testBlocks : [ testBlocks ];
const newBlocks = blocks.map( ( testBlock ) =>
createBlock(
testBlock.name,
testBlock.attributes,
testBlock.innerBlocks
)
);
return waitForStoreResolvers( () => {
return render( <Editor { ...props } /> );
return render( <Editor testBlocks={ newBlocks } { ...props } /> );
} );
}

0 comments on commit b0f0746

Please sign in to comment.