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

[RNMobile] Allow wildcard block transformations for container blocks #48792

Merged
merged 4 commits into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
@@ -0,0 +1,59 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Paragraph block transforms to Columns block 1`] = `
"<!-- wp:columns -->
<div class="wp-block-columns"><!-- wp:column {"width":"100%"} -->
<div class="wp-block-column" style="flex-basis:100%"><!-- wp:paragraph -->
<p>Example text</p>
<!-- /wp:paragraph --></div>
<!-- /wp:column --></div>
<!-- /wp:columns -->"
`;

exports[`Paragraph block transforms to Group block 1`] = `
"<!-- wp:group {"layout":{"type":"constrained"}} -->
<div class="wp-block-group"><!-- wp:paragraph -->
<p>Example text</p>
<!-- /wp:paragraph --></div>
<!-- /wp:group -->"
`;

exports[`Paragraph block transforms to Heading block 1`] = `
"<!-- wp:heading -->
<h2 class="wp-block-heading">Example text</h2>
<!-- /wp:heading -->"
`;

exports[`Paragraph block transforms to List block 1`] = `
"<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>Example text</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->"
`;

exports[`Paragraph block transforms to Preformatted block 1`] = `
"<!-- wp:preformatted -->
<pre class="wp-block-preformatted">Example text</pre>
<!-- /wp:preformatted -->"
`;

exports[`Paragraph block transforms to Pullquote block 1`] = `
"<!-- wp:pullquote -->
<figure class="wp-block-pullquote"><blockquote><p>Example text</p></blockquote></figure>
<!-- /wp:pullquote -->"
`;

exports[`Paragraph block transforms to Quote block 1`] = `
"<!-- wp:quote -->
<blockquote class="wp-block-quote"><!-- wp:paragraph -->
<p>Example text</p>
<!-- /wp:paragraph --></blockquote>
<!-- /wp:quote -->"
`;

exports[`Paragraph block transforms to Verse block 1`] = `
"<!-- wp:verse -->
<pre class="wp-block-verse">Example text</pre>
<!-- /wp:verse -->"
`;
53 changes: 53 additions & 0 deletions packages/block-library/src/paragraph/test/transforms.native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* External dependencies
*/
import {
fireEvent,
getBlock,
getEditorHtml,
initializeEditor,
openBlockActionsMenu,
setupCoreBlocks,
triggerBlockListLayout,
} from 'test/helpers';

const block = 'Paragraph';
const initialHtml = `
<!-- wp:paragraph -->
<p>Example text</p>
<!-- /wp:paragraph -->`;

// NOTE: Paragraph block can be transformed to Buttons block in web,
// however this transform is not supported in the native version.
const transformsWithInnerBlocks = [ 'List', 'Quote', 'Columns', 'Group' ];
const blockTransforms = [
'Heading',
'Preformatted',
'Pullquote',
'Verse',
...transformsWithInnerBlocks,
];
Comment on lines +20 to +29
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This unit test covers the blocks that a Paragraph block can be transformed to:

  • Heading ✅
  • List ✅
  • Quote ✅
  • Buttons (only in web) ❌ => Native version needs document.createHTMLDocument function to work.
  • Preformatted ✅
  • Columns ✅
  • Verse ✅
  • Pullquote ✅
  • Group ✅


setupCoreBlocks();

describe( `${ block } block transforms`, () => {
test.each( blockTransforms )(
'to %s block',
async ( blockTransformation ) => {
const screen = await initializeEditor( { initialHtml } );
const { getByText } = screen;
fireEvent.press( getBlock( screen, block ) );

await openBlockActionsMenu( screen );
fireEvent.press( getByText( 'Transform block…' ) );
fireEvent.press( getByText( blockTransformation ) );

const newBlock = getBlock( screen, blockTransformation );
if ( transformsWithInnerBlocks.includes( blockTransformation ) )
await triggerBlockListLayout( newBlock );

expect( newBlock ).toBeVisible();
expect( getEditorHtml() ).toMatchSnapshot();
}
);
} );
8 changes: 0 additions & 8 deletions packages/blocks/src/api/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,6 @@ const isPossibleTransformForSource = ( transform, direction, blocks ) => {
return false;
}

if (
transform.usingMobileTransformations &&
isWildcardBlockTransform( transform ) &&
! isContainerGroupBlock( sourceBlock.name )
) {
return false;
}

return true;
};

Expand Down
4 changes: 4 additions & 0 deletions test/native/integration-test-helpers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ Gets an inner block from another block.

Initialize an editor for test assertions.

### [`openBlockActionsMenu`](https://github.com/WordPress/gutenberg/blob/HEAD/test/native/integration-test-helpers/open-block-actions-menu.js)

Opens the block's actions menu of the current selected block.

### [`openBlockSettings`](https://github.com/WordPress/gutenberg/blob/HEAD/test/native/integration-test-helpers/open-block-settings.js)

Opens the block settings of the current selected block.
Expand Down
1 change: 1 addition & 0 deletions test/native/integration-test-helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export { getBlock } from './get-block';
export { getEditorHtml } from './get-editor-html';
export { getInnerBlock } from './get-inner-block';
export { initializeEditor } from './initialize-editor';
export { openBlockActionsMenu } from './open-block-actions-menu';
export { openBlockSettings } from './open-block-settings';
export { changeAndSelectTextOfRichText } from './rich-text-change-and-select-text';
export { changeTextOfRichText } from './rich-text-change-text';
Expand Down
20 changes: 20 additions & 0 deletions test/native/integration-test-helpers/open-block-actions-menu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* External dependencies
*/
import { fireEvent } from '@testing-library/react-native';

/**
* Internal dependencies
*/
import { waitForModalVisible } from './wait-for-modal-visible';

/**
* Opens the block's actions menu of the current selected block.
*
* @param {import('@testing-library/react-native').RenderAPI} screen The Testing Library screen.
*/
export const openBlockActionsMenu = async ( screen ) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I introduced this test helper to simplify the action of opening the Block actions menu, which is needed for transforming a block.

const { getByLabelText, getByTestId } = screen;
fireEvent.press( getByLabelText( 'Open Block Actions Menu' ) );
return waitForModalVisible( getByTestId( 'block-actions-menu' ) );
};
16 changes: 11 additions & 5 deletions test/native/integration-test-helpers/trigger-block-list-layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,20 @@ import { waitForStoreResolvers } from './wait-for-store-resolvers';
* case any of the inner elements use selectors that are associated with store
* resolvers.
*
* @param {import('react-test-renderer').ReactTestInstance} block Block test instance to trigger layout event.
* @param {Object} [options] Configuration options for the event.
* @param {number} [options.width] Width value to be passed to the event.
* @param {import('react-test-renderer').ReactTestInstance} block Block test instance to trigger layout event.
* @param {Object} [options] Configuration options for the event.
* @param {number} [options.width] Width value to be passed to the event.
* @param {number} [options.blockListIndex] Block list index, for cases when there is more than one, like in inner blocks.
*/
export const triggerBlockListLayout = async ( block, { width = 320 } = {} ) =>
export const triggerBlockListLayout = async (
block,
{ width = 320, blockListIndex = 0 } = {}
) =>
waitForStoreResolvers( () =>
fireEvent(
within( block ).getByTestId( 'block-list-wrapper' ),
within( block ).getAllByTestId( 'block-list-wrapper' )[
blockListIndex
],
'layout',
{
nativeEvent: {
Expand Down