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

Preserve bindings metadata in block transforms #59179

Merged
merged 16 commits into from
Feb 22, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Revert "Add heading and button transform from/to paragraph"
This reverts commit c3138e3.
  • Loading branch information
SantosGuillamot committed Feb 20, 2024
commit a7cd279c89630f05ca62980f19cc592076a2362e
287 changes: 14 additions & 273 deletions test/e2e/specs/editor/various/convert-block-type.spec.js
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
*/
const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' );

test.describe( 'Block transforms', () => {
test.describe( 'Code block', () => {
test.beforeEach( async ( { admin } ) => {
await admin.createNewPost();
} );
@@ -12,286 +12,27 @@ test.describe( 'Block transforms', () => {
await requestUtils.deleteAllPosts();
} );

test.describe( 'Code block', () => {
test( 'should convert to a preformatted block', async ( {
page,
editor,
} ) => {
const code = 'print "Hello Dolly!"';
test( 'should convert to a preformatted block', async ( {
page,
editor,
} ) => {
const code = 'print "Hello Dolly!"';

await editor.insertBlock( { name: 'core/code' } );
await page.keyboard.type( code );
await editor.insertBlock( { name: 'core/code' } );
await page.keyboard.type( code );

// Verify the content starts out as a Code block.
// Verify the content starts out as a Code block.

await expect.poll( editor.getEditedPostContent )
.toBe( `<!-- wp:code -->
await expect.poll( editor.getEditedPostContent ).toBe( `<!-- wp:code -->
<pre class="wp-block-code"><code>${ code }</code></pre>
<!-- /wp:code -->` );

await editor.transformBlockTo( 'core/preformatted' );
await editor.transformBlockTo( 'core/preformatted' );

// The content should now be a Preformatted block with no data loss.
await expect.poll( editor.getEditedPostContent )
.toBe( `<!-- wp:preformatted -->
// The content should now be a Preformatted block with no data loss.
await expect.poll( editor.getEditedPostContent )
.toBe( `<!-- wp:preformatted -->
<pre class="wp-block-preformatted">${ code }</pre>
<!-- /wp:preformatted -->` );
} );
} );

test.describe( 'Heading block', () => {
test.describe( 'FROM paragraph', () => {
test( 'should preserve the content', async ( { editor } ) => {
await editor.insertBlock( {
name: 'core/paragraph',
attributes: {
content: 'initial content',
},
} );
await editor.transformBlockTo( 'core/heading' );
const headingBlock = ( await editor.getBlocks() )[ 0 ];
expect( headingBlock.name ).toBe( 'core/heading' );
expect( headingBlock.attributes.content ).toBe(
'initial content'
);
} );

test( 'should preserve the text align attribute', async ( {
editor,
} ) => {
await editor.insertBlock( {
name: 'core/paragraph',
attributes: {
align: 'right',
content: 'initial content',
},
} );
await editor.transformBlockTo( 'core/heading' );
const headingBlock = ( await editor.getBlocks() )[ 0 ];
expect( headingBlock.name ).toBe( 'core/heading' );
expect( headingBlock.attributes.textAlign ).toBe( 'right' );
} );

test( 'should preserve the metadata attribute', async ( {
editor,
} ) => {
await editor.insertBlock( {
name: 'core/paragraph',
attributes: {
content: 'initial content',
metadata: {
name: 'Custom name',
},
},
} );

await editor.transformBlockTo( 'core/heading' );
const headingBlock = ( await editor.getBlocks() )[ 0 ];
expect( headingBlock.name ).toBe( 'core/heading' );
expect( headingBlock.attributes.metadata ).toMatchObject( {
name: 'Custom name',
} );
} );

test( 'should preserve the block bindings', async ( {
editor,
} ) => {
await editor.insertBlock( {
name: 'core/paragraph',
attributes: {
content: 'initial content',
metadata: {
bindings: {
content: {
source: 'core/post-meta',
args: {
key: 'custom_field',
},
},
},
},
},
} );

await editor.transformBlockTo( 'core/heading' );
const headingBlock = ( await editor.getBlocks() )[ 0 ];
expect( headingBlock.name ).toBe( 'core/heading' );
expect(
headingBlock.attributes.metadata.bindings
).toMatchObject( {
content: {
source: 'core/post-meta',
args: {
key: 'custom_field',
},
},
} );
} );
} );

test.describe( 'TO paragraph', () => {
test( 'should preserve the content', async ( { editor } ) => {
await editor.insertBlock( {
name: 'core/heading',
attributes: {
content: 'initial content',
},
} );
await editor.transformBlockTo( 'core/paragraph' );
const paragraphBlock = ( await editor.getBlocks() )[ 0 ];
expect( paragraphBlock.name ).toBe( 'core/paragraph' );
expect( paragraphBlock.attributes.content ).toBe(
'initial content'
);
} );

test( 'should preserve the text align attribute', async ( {
editor,
} ) => {
await editor.insertBlock( {
name: 'core/heading',
attributes: {
textAlign: 'right',
content: 'initial content',
},
} );
await editor.transformBlockTo( 'core/paragraph' );
const paragraphBlock = ( await editor.getBlocks() )[ 0 ];
expect( paragraphBlock.name ).toBe( 'core/paragraph' );
expect( paragraphBlock.attributes.align ).toBe( 'right' );
} );

test( 'should preserve the metadata attribute', async ( {
editor,
} ) => {
await editor.insertBlock( {
name: 'core/heading',
attributes: {
content: 'initial content',
metadata: {
name: 'Custom name',
},
},
} );

await editor.transformBlockTo( 'core/paragraph' );
const paragraphBlock = ( await editor.getBlocks() )[ 0 ];
expect( paragraphBlock.name ).toBe( 'core/paragraph' );
expect( paragraphBlock.attributes.metadata ).toMatchObject( {
name: 'Custom name',
} );
} );

test( 'should preserve the block bindings', async ( {
editor,
} ) => {
await editor.insertBlock( {
name: 'core/heading',
attributes: {
content: 'initial content',
metadata: {
bindings: {
content: {
source: 'core/post-meta',
args: {
key: 'custom_field',
},
},
},
},
},
} );

await editor.transformBlockTo( 'core/paragraph' );
const paragraphBlock = ( await editor.getBlocks() )[ 0 ];
expect( paragraphBlock.name ).toBe( 'core/paragraph' );
expect(
paragraphBlock.attributes.metadata.bindings
).toMatchObject( {
content: {
source: 'core/post-meta',
args: {
key: 'custom_field',
},
},
} );
} );
} );
} );

test.describe( 'Button block', () => {
test.describe( 'FROM paragraph', () => {
test( 'should preserve the content', async ( { editor } ) => {
await editor.insertBlock( {
name: 'core/paragraph',
attributes: {
content: 'initial content',
},
} );
await editor.transformBlockTo( 'core/buttons' );
const buttonBlock = ( await editor.getBlocks() )[ 0 ]
.innerBlocks[ 0 ];
expect( buttonBlock.name ).toBe( 'core/button' );
expect( buttonBlock.attributes.text ).toBe( 'initial content' );
} );

test( 'should preserve the metadata attribute', async ( {
editor,
} ) => {
await editor.insertBlock( {
name: 'core/paragraph',
attributes: {
content: 'initial content',
metadata: {
name: 'Custom name',
},
},
} );

await editor.transformBlockTo( 'core/buttons' );
const buttonBlock = ( await editor.getBlocks() )[ 0 ]
.innerBlocks[ 0 ];
expect( buttonBlock.name ).toBe( 'core/button' );
expect( buttonBlock.attributes.metadata ).toMatchObject( {
name: 'Custom name',
} );
} );

test( 'should preserve the block bindings', async ( {
editor,
} ) => {
await editor.insertBlock( {
name: 'core/paragraph',
attributes: {
content: 'initial content',
metadata: {
bindings: {
content: {
source: 'core/post-meta',
args: {
key: 'custom_field',
},
},
},
},
},
} );

await editor.transformBlockTo( 'core/buttons' );
const buttonBlock = ( await editor.getBlocks() )[ 0 ]
.innerBlocks[ 0 ];
expect( buttonBlock.name ).toBe( 'core/button' );
expect(
buttonBlock.attributes.metadata.bindings
).toMatchObject( {
text: {
source: 'core/post-meta',
args: {
key: 'custom_field',
},
},
} );
} );
} );
} );
} );