Skip to content

Commit

Permalink
unregister core formats on test end
Browse files Browse the repository at this point in the history
  • Loading branch information
glendaviesnz committed Jul 24, 2023
1 parent d85be3d commit 3587760
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
8 changes: 1 addition & 7 deletions packages/block-library/src/cover/test/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,7 @@ const disabledColorSettings = {
async function setup( attributes, useCoreBlocks, customSettings ) {
const testBlock = { name: 'core/cover', attributes };
const settings = customSettings || defaultSettings;
const skippedBlocks = [ 'core/footnotes' ];
return initializeEditor(
testBlock,
useCoreBlocks,
settings,
skippedBlocks
);
return initializeEditor( testBlock, useCoreBlocks, settings );
}

async function createAndSelectBlock() {
Expand Down
33 changes: 18 additions & 15 deletions test/integration/helpers/integration-test-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,16 @@ import {
ObserveTyping,
} from '@wordpress/block-editor';
import { Popover, SlotFillProvider } from '@wordpress/components';
import {
registerCoreBlocks,
__experimentalGetCoreBlocks,
} from '@wordpress/block-library';
import { registerCoreBlocks } from '@wordpress/block-library';
import { ShortcutProvider } from '@wordpress/keyboard-shortcuts';
import '@wordpress/format-library';
import {
createBlock,
unregisterBlockType,
getBlockTypes,
} from '@wordpress/blocks';
import { store as richTextStore } from '@wordpress/rich-text';
import { useSelect, useDispatch } from '@wordpress/data';

/**
* Internal dependencies
Expand Down Expand Up @@ -63,13 +62,22 @@ export async function selectBlock( name ) {
export function Editor( { testBlocks, settings = {} } ) {
const [ currentBlocks, updateBlocks ] = useState( testBlocks );

// Some blocks may register formats, eg. core/footnotes, so we need to also
// unregister those formats along with core blocks when the test is complete.
const availableFormats = useSelect(
( select ) => select( richTextStore ).getFormatTypes(),
[]
);
const { removeFormatTypes } = useDispatch( richTextStore );

useEffect( () => {
return () => {
getBlockTypes().forEach( ( { name } ) =>
unregisterBlockType( name )
);
getBlockTypes().forEach( ( { name } ) => {
unregisterBlockType( name );
} );
removeFormatTypes( availableFormats.map( ( { name } ) => name ) );
};
}, [] );
}, [ availableFormats, removeFormatTypes ] );

return (
<ShortcutProvider>
Expand Down Expand Up @@ -103,19 +111,14 @@ export function Editor( { testBlocks, settings = {} } ) {
* @param {Object | Array} testBlocks Block or array of block settings for blocks to be tested.
* @param {boolean} useCoreBlocks Defaults to true. If false, core blocks will not be registered.
* @param {Object} settings Any additional editor settings to be passed to the editor.
* @param {Array} skippedBlocks Blocks that should be skipped during registration.
*/
export async function initializeEditor(
testBlocks,
useCoreBlocks = true,
settings,
skippedBlocks = []
settings
) {
if ( useCoreBlocks ) {
const coreBlocks = __experimentalGetCoreBlocks().filter(
( { name } ) => ! skippedBlocks.includes( name )
);
registerCoreBlocks( coreBlocks );
registerCoreBlocks();
}
const blocks = Array.isArray( testBlocks ) ? testBlocks : [ testBlocks ];
const newBlocks = blocks.map( ( testBlock ) =>
Expand Down

0 comments on commit 3587760

Please sign in to comment.