Skip to content

Commit

Permalink
[Mobile] - Fix crash when switching to the HTML Editor (#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 Jun 19, 2023
1 parent 5d371bc commit 8989749
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

1 comment on commit 8989749

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in 8989749.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/5313418052
📝 Reported issues:

Please sign in to comment.