Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stabilize isPreviewMode flag #66149

Merged
merged 12 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { render } from '@testing-library/react';
/**
* WordPress dependencies
*/
import { useRegistry } from '@wordpress/data';
import { useRegistry, useSelect } from '@wordpress/data';

/**
* Internal dependencies
Expand All @@ -21,6 +21,20 @@ const HasEditorSetting = ( props ) => {
return <p>Test.</p>;
};

const PreviewModeGetter = () => {
const previewModeKeys = useSelect( ( select ) => {
const { getSettings } = select( blockEditorStore );
const settings = getSettings();
return {
// This property will be removed in the future. There is a test that asserts we're throwing a deprecation warning.
__unstableIsPreviewMode: settings.__unstableIsPreviewMode,
isPreviewMode: settings.isPreviewMode,
};
}, [] );

return <>{ JSON.stringify( previewModeKeys ) }</>;
};

describe( 'BlockEditorProvider', () => {
let registry;
const setRegistry = ( reg ) => {
Expand Down Expand Up @@ -58,6 +72,34 @@ describe( 'BlockEditorProvider', () => {
const settings = registry.select( blockEditorStore ).getSettings();
expect( settings ).toHaveProperty( 'stableSetting' );
} );
it( 'preserves deprecated getters incoming from the settings reducer', async () => {
const consoleWarn = jest
.spyOn( global.console, 'warn' )
.mockImplementation();

const { container } = render(
<BlockEditorProvider
settings={ {
isPreviewMode: true,
} }
>
<PreviewModeGetter />
</BlockEditorProvider>
);

expect( container ).toHaveTextContent(
JSON.stringify( {
__unstableIsPreviewMode: true,
isPreviewMode: true,
} )
);

expect( consoleWarn ).toHaveBeenCalledWith(
'__unstableIsPreviewMode is deprecated since version 6.8. Please use isPreviewMode instead.'
);

consoleWarn.mockRestore();
} );
} );

describe( 'ExperimentalBlockEditorProvider', () => {
Expand Down
18 changes: 17 additions & 1 deletion packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1591,7 +1591,23 @@ export function updateBlockListSettings( clientId, settings ) {
* @return {Object} Action object
*/
export function updateSettings( settings ) {
return __experimentalUpdateSettings( settings, {
let updatedSettings = settings;

if ( Object.hasOwn( updatedSettings, '__unstableIsPreviewMode' ) ) {
deprecated(
"__unstableIsPreviewMode argument in wp.data.dispatch('core/block-editor').updateSettings",
{
since: '6.8',
alternative: 'isPreviewMode',
}
);

updatedSettings = { ...updatedSettings };
updatedSettings.isPreviewMode = updatedSettings.__unstableIsPreviewMode;
delete updatedSettings.__unstableIsPreviewMode;
}

return __experimentalUpdateSettings( updatedSettings, {
stripExperimentalSettings: true,
} );
}
Expand Down
2 changes: 1 addition & 1 deletion packages/block-editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1675,7 +1675,7 @@ export function settings( state = SETTINGS_DEFAULTS, action ) {
Object.defineProperty( updatedSettings, '__unstableIsPreviewMode', {
get() {
deprecated( '__unstableIsPreviewMode', {
since: '19.5',
since: '6.8',
alternative: 'isPreviewMode',
} );

Expand Down
41 changes: 41 additions & 0 deletions packages/block-editor/src/store/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1125,6 +1125,47 @@ describe( 'actions', () => {
} );
} );

describe( 'updateSettings', () => {
it( 'warns when setting the deprecated __unstableIsPreviewMode property and sets the stable property instead', () => {
const consoleWarn = jest
.spyOn( global.console, 'warn' )
.mockImplementation();

const store = createRegistry().registerStore(
blockEditorStoreName,
{
actions,
selectors,
reducer,
}
);

store.dispatch(
updateSettings( {
__unstableIsPreviewMode: true,
} )
);

expect( consoleWarn ).toHaveBeenCalledWith(
"__unstableIsPreviewMode argument in wp.data.dispatch('core/block-editor').updateSettings is deprecated since version 6.8. Please use isPreviewMode instead."
);

consoleWarn.mockClear();

expect( store.getState().settings.__unstableIsPreviewMode ).toBe(
true
);

expect( store.getState().settings.isPreviewMode ).toBe( true );

expect( consoleWarn ).toHaveBeenCalledWith(
'__unstableIsPreviewMode is deprecated since version 6.8. Please use isPreviewMode instead.'
);

consoleWarn.mockRestore();
} );
} );

describe( 'registerInserterMediaCategory', () => {
describe( 'should log errors when invalid', () => {
it( 'valid object', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/block-editor/src/store/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3086,7 +3086,7 @@ describe( 'state', () => {
expect( settingsObject.isPreviewMode ).toBeDefined();

expect( consoleWarn ).toHaveBeenCalledWith(
'__unstableIsPreviewMode is deprecated since version 19.5. Please use isPreviewMode instead.'
'__unstableIsPreviewMode is deprecated since version 6.8. Please use isPreviewMode instead.'
);

consoleWarn.mockRestore();
Expand Down
Loading