Skip to content

Commit

Permalink
Block editor store: also attach private APIs to old store descriptor (#…
Browse files Browse the repository at this point in the history
…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: #51145 (comment)

Props jsnajdr
  • Loading branch information
mcsf authored and tellthemachines committed Jul 17, 2023
1 parent 184448b commit 4de66d2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
10 changes: 10 additions & 0 deletions packages/block-editor/src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
14 changes: 1 addition & 13 deletions packages/block-editor/src/store/private-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,19 +158,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
Expand Down
3 changes: 3 additions & 0 deletions packages/block-editor/src/store/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,7 @@ describe( 'actions', () => {
const select = {
getBlockRootClientId: () => undefined,
canRemoveBlocks: () => true,
isRemovalPromptSupported: () => false,
};
const dispatch = Object.assign( jest.fn(), {
selectPreviousBlock: jest.fn(),
Expand Down Expand Up @@ -727,6 +728,7 @@ describe( 'actions', () => {
const select = {
getBlockRootClientId: () => null,
canRemoveBlocks: () => true,
isRemovalPromptSupported: () => false,
};
const dispatch = Object.assign( jest.fn(), {
selectPreviousBlock: jest.fn(),
Expand All @@ -751,6 +753,7 @@ describe( 'actions', () => {
const select = {
getBlockRootClientId: () => null,
canRemoveBlocks: () => true,
isRemovalPromptSupported: () => false,
};
const dispatch = Object.assign( jest.fn(), {
selectPreviousBlock: jest.fn(),
Expand Down

0 comments on commit 4de66d2

Please sign in to comment.