Skip to content
Prev Previous commit
Update action, reducer, and selectors for consistency, add tests
  • Loading branch information
andrewserong committed Sep 13, 2023
commit 13c42eda4882d78d3ac15ccf3783c65f15a231af
4 changes: 2 additions & 2 deletions packages/block-editor/src/store/private-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,10 @@ export function setBlockRemovalRules( rules = false ) {
/**
* Sets the client ID of the block settings menu that is currently open.
*
* @param {string} clientId The block client ID.
* @param {?string} clientId The block client ID.
* @return {Object} Action object.
*/
export function setOpenedBlockSettingsMenu( clientId = '' ) {
export function setOpenedBlockSettingsMenu( clientId ) {
return {
type: 'SET_OPENED_BLOCK_SETTINGS_MENU',
clientId,
Expand Down
2 changes: 1 addition & 1 deletion packages/block-editor/src/store/private-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export function getBlockRemovalRules( state ) {
* Returns the client ID of the block settings menu that is currently open.
*
* @param {Object} state Global application state.
* @return {string|undefined} The client ID of the block menu that is currently open.
* @return {string|null} The client ID of the block menu that is currently open.
*/
export function getOpenedBlockSettingsMenu( state ) {
return state.openedBlockSettingsMenu;
Expand Down
10 changes: 9 additions & 1 deletion packages/block-editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1913,9 +1913,17 @@ export function blockEditingModes( state = new Map(), action ) {
return state;
}

/**
* Reducer returning the clientId of the block settings menu that is currently open.
*
* @param {string|null} state Current state.
* @param {Object} action Dispatched action.
*
* @return {string|null} Updated state.
*/
export function openedBlockSettingsMenu( state = null, action ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The block editor reducer is quite thoroughly covered by unit tests. A couple of simple tests could be useful to cover this new reducer subtree.

if ( 'SET_OPENED_BLOCK_SETTINGS_MENU' === action.type ) {
return action?.clientId;
return action?.clientId ?? null;
}
return state;
}
Expand Down
17 changes: 17 additions & 0 deletions packages/block-editor/src/store/test/private-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
hideBlockInterface,
showBlockInterface,
__experimentalUpdateSettings,
setOpenedBlockSettingsMenu,
} from '../private-actions';

describe( 'private actions', () => {
Expand Down Expand Up @@ -78,4 +79,20 @@ describe( 'private actions', () => {
} );
} );
} );

describe( 'setOpenedBlockSettingsMenu', () => {
it( 'should return the SET_OPENED_BLOCK_SETTINGS_MENU action', () => {
expect( setOpenedBlockSettingsMenu() ).toEqual( {
clientId: undefined,
type: 'SET_OPENED_BLOCK_SETTINGS_MENU',
} );
} );

it( 'should return the SET_OPENED_BLOCK_SETTINGS_MENU action with client id if provided', () => {
expect( setOpenedBlockSettingsMenu( 'abcd' ) ).toEqual( {
clientId: 'abcd',
type: 'SET_OPENED_BLOCK_SETTINGS_MENU',
} );
} );
} );
} );
25 changes: 25 additions & 0 deletions packages/block-editor/src/store/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
lastBlockAttributesChange,
lastBlockInserted,
blockEditingModes,
openedBlockSettingsMenu,
} from '../reducer';

const noop = () => {};
Expand Down Expand Up @@ -3415,4 +3416,28 @@ describe( 'state', () => {
);
} );
} );

describe( 'openedBlockSettingsMenu', () => {
it( 'should return null by default', () => {
expect( openedBlockSettingsMenu( undefined, {} ) ).toBe( null );
} );

it( 'should set client id for opened block settings menu', () => {
const state = openedBlockSettingsMenu( null, {
type: 'SET_OPENED_BLOCK_SETTINGS_MENU',
clientId: '14501cc2-90a6-4f52-aa36-ab6e896135d1',
} );
expect( state ).toBe( '14501cc2-90a6-4f52-aa36-ab6e896135d1' );
} );

it( 'should clear the state when no client id is passed', () => {
const state = openedBlockSettingsMenu(
'14501cc2-90a6-4f52-aa36-ab6e896135d1',
{
type: 'SET_OPENED_BLOCK_SETTINGS_MENU',
}
);
expect( state ).toBe( null );
} );
} );
} );