Skip to content

Commit

Permalink
[Mobile] - Fix crash when switching to the HTML Editor (WordPress#51650)
Browse files Browse the repository at this point in the history
* Mobile - HTML Editor - Fix an issue where the editor would crash if it was focused on a block since it was calling clearSelectedBlock twice.

* Mobile - Editor test - Update test to use toBeVisible instead
  • Loading branch information
Gerardo Pacheco authored and sethrubenstein committed Jul 13, 2023
1 parent bc2ed19 commit c951541
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
43 changes: 34 additions & 9 deletions packages/edit-post/src/test/editor.native.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
/**
* External dependencies
*/
import { act, render } from 'test/helpers';
import {
act,
addBlock,
fireEvent,
getBlock,
initializeEditor,
render,
setupCoreBlocks,
} from 'test/helpers';

/**
* WordPress dependencies
*/
import { registerCoreBlocks } from '@wordpress/block-library';
import RNReactNativeGutenbergBridge from '@wordpress/react-native-bridge';
import RNReactNativeGutenbergBridge, {
subscribeParentToggleHTMLMode,
} from '@wordpress/react-native-bridge';
// Force register 'core/editor' store.
import { store } from '@wordpress/editor'; // eslint-disable-line no-unused-vars

jest.mock( '../components/layout', () => () => 'Layout' );

/**
* Internal dependencies
*/
Expand All @@ -34,11 +41,9 @@ afterAll( () => {
jest.useRealTimers();
} );

describe( 'Editor', () => {
beforeAll( () => {
registerCoreBlocks();
} );
setupCoreBlocks();

describe( 'Editor', () => {
it( 'detects unsupported block and sends hasUnsupportedBlocks true to native', () => {
RNReactNativeGutenbergBridge.editorDidMount = jest.fn();

Expand All @@ -56,6 +61,26 @@ describe( 'Editor', () => {
RNReactNativeGutenbergBridge.editorDidMount
).toHaveBeenCalledWith( [ 'core/notablock' ] );
} );

it( 'toggles the editor from Visual to HTML mode', async () => {
// Arrange
let toggleMode;
subscribeParentToggleHTMLMode.mockImplementation( ( callback ) => {
toggleMode = callback;
} );
const screen = await initializeEditor();
await addBlock( screen, 'Paragraph' );

// Act
const paragraphBlock = getBlock( screen, 'Paragraph' );
fireEvent.press( paragraphBlock );

toggleMode();

// Assert
const htmlEditor = await screen.findByLabelText( 'html-view-content' );
expect( htmlEditor ).toBeVisible();
} );
} );

// Utilities.
Expand Down
11 changes: 2 additions & 9 deletions packages/editor/src/components/provider/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,6 @@ class NativeEditorProvider extends Component {
const { mode, switchMode } = this.props;
// Refresh html content first.
this.serializeToNativeAction();
// Make sure to blur the selected block and dismiss the keyboard.
this.props.clearSelectedBlock();
switchMode( mode === 'visual' ? 'text' : 'visual' );
}

Expand Down Expand Up @@ -387,12 +385,8 @@ const ComposedNativeProvider = compose( [
withDispatch( ( dispatch ) => {
const { editPost, resetEditorBlocks, updateEditorSettings } =
dispatch( editorStore );
const {
updateSettings,
clearSelectedBlock,
insertBlock,
replaceBlock,
} = dispatch( blockEditorStore );
const { updateSettings, insertBlock, replaceBlock } =
dispatch( blockEditorStore );
const { switchEditorMode } = dispatch( editPostStore );
const { addEntities, receiveEntityRecords } = dispatch( coreStore );
const { createSuccessNotice } = dispatch( noticesStore );
Expand All @@ -401,7 +395,6 @@ const ComposedNativeProvider = compose( [
updateBlockEditorSettings: updateSettings,
updateEditorSettings,
addEntities,
clearSelectedBlock,
insertBlock,
createSuccessNotice,
editTitle( title ) {
Expand Down

0 comments on commit c951541

Please sign in to comment.