From 55c18fc594445f9c312b851ab707daa504abd6f7 Mon Sep 17 00:00:00 2001 From: Miguel Fonseca <150562+mcsf@users.noreply.github.com> Date: Thu, 29 Jun 2023 11:05:00 +0100 Subject: [PATCH] Block editor store: also attach private APIs to old store descriptor (#52088) As a workaround, until #39632 is merged, make sure that private actions and selectors can be unlocked from the original store descriptor (the one created by `createReduxStore`) and not just the one registered in the default registry (created by `registerStore`). Without this workaround, specific setups will unexpectedly fail, such as the action tests in the `reusable-blocks` package, due to the way that those tests create their own registries in which they register stores like `block-editor`. Context: https://github.com/WordPress/gutenberg/pull/51145#discussion_r1239999590 Props jsnajdr --- packages/block-editor/src/store/index.js | 10 ++++++++++ packages/block-editor/src/store/private-actions.js | 14 +------------- packages/block-editor/src/store/test/actions.js | 3 +++ 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/packages/block-editor/src/store/index.js b/packages/block-editor/src/store/index.js index ed17b387ba588..0bcc00cb5f6ae 100644 --- a/packages/block-editor/src/store/index.js +++ b/packages/block-editor/src/store/index.js @@ -43,3 +43,13 @@ const registeredStore = registerStore( STORE_NAME, { } ); unlock( registeredStore ).registerPrivateActions( privateActions ); unlock( registeredStore ).registerPrivateSelectors( privateSelectors ); + +// TODO: Remove once we switch to the `register` function (see above). +// +// Until then, private functions also need to be attached to the original +// `store` descriptor in order to avoid unit tests failing, which could happen +// when tests create new registries in which they register stores. +// +// @see https://github.com/WordPress/gutenberg/pull/51145#discussion_r1239999590 +unlock( store ).registerPrivateActions( privateActions ); +unlock( store ).registerPrivateSelectors( privateSelectors ); diff --git a/packages/block-editor/src/store/private-actions.js b/packages/block-editor/src/store/private-actions.js index 8b5e066e5a83c..d90aae340a92a 100644 --- a/packages/block-editor/src/store/private-actions.js +++ b/packages/block-editor/src/store/private-actions.js @@ -161,19 +161,7 @@ export const privateRemoveBlocks = // register using `toggleRemovalPromptSupport()`. // // @see https://github.com/WordPress/gutenberg/pull/51145 - if ( - ! forceRemove && - // FIXME: Without this existence check, the unit tests for - // `__experimentalDeleteReusableBlock` in - // `packages/reusable-blocks/src/store/test/actions.js` fail due to - // the fact that the `registry` object passed to the thunk actions - // doesn't include this private action. This needs to be - // investigated to understand whether it's a real smell or if it's - // because not all store code has been updated to accommodate - // private selectors. - select.isRemovalPromptSupported && - select.isRemovalPromptSupported() - ) { + if ( ! forceRemove && select.isRemovalPromptSupported() ) { const blockNamesForPrompt = new Set(); // Given a list of client IDs of blocks that the user intended to diff --git a/packages/block-editor/src/store/test/actions.js b/packages/block-editor/src/store/test/actions.js index 27cd77b076993..78df4a3e131d7 100644 --- a/packages/block-editor/src/store/test/actions.js +++ b/packages/block-editor/src/store/test/actions.js @@ -618,6 +618,7 @@ describe( 'actions', () => { const select = { getBlockRootClientId: () => undefined, canRemoveBlocks: () => true, + isRemovalPromptSupported: () => false, }; const dispatch = Object.assign( jest.fn(), { selectPreviousBlock: jest.fn(), @@ -728,6 +729,7 @@ describe( 'actions', () => { const select = { getBlockRootClientId: () => null, canRemoveBlocks: () => true, + isRemovalPromptSupported: () => false, }; const dispatch = Object.assign( jest.fn(), { selectPreviousBlock: jest.fn(), @@ -752,6 +754,7 @@ describe( 'actions', () => { const select = { getBlockRootClientId: () => null, canRemoveBlocks: () => true, + isRemovalPromptSupported: () => false, }; const dispatch = Object.assign( jest.fn(), { selectPreviousBlock: jest.fn(),