Skip to content

Commit

Permalink
Stop using getPatternRecord
Browse files Browse the repository at this point in the history
  • Loading branch information
SantosGuillamot committed May 14, 2024
1 parent 6b97321 commit c122b79
Showing 1 changed file with 38 additions and 87 deletions.
125 changes: 38 additions & 87 deletions test/e2e/specs/editor/various/patterns.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@
*/
const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' );

async function getPatternRecord( page, patternRef ) {
return await page.evaluate( async ( ref ) => {
return window.wp.data
.select( 'core' )
.getEditedEntityRecord( 'postType', 'wp_block', ref );
}, patternRef );
}

test.describe( 'Unsynced pattern', () => {
test.beforeAll( async ( { requestUtils } ) => {
await requestUtils.deleteAllBlocks();
Expand Down Expand Up @@ -324,35 +316,21 @@ test.describe( 'Synced pattern', () => {
// Go back to the post.
await editorTopBar.getByRole( 'button', { name: 'Back' } ).click();

const expectedParagraphBlock = {
name: 'core/paragraph',
attributes: { content: 'After Edit' },
};

// It seems edited values are stored in the `blocks` array.
const [ syncedPattern ] = await editor.getBlocks();
const [ modifiedBlock ] = (
await getPatternRecord( page, syncedPattern?.attributes?.ref )
)?.blocks;

expect( modifiedBlock?.name ).toBe( expectedParagraphBlock.name );
expect( modifiedBlock?.attributes?.content ).toBe(
expectedParagraphBlock.attributes.content
);

await editor.selectBlocks(
editor.canvas.getByRole( 'document', { name: 'Block: Pattern' } )
);
await editor.clickBlockOptionsMenuItem( 'Detach' );

await expect
.poll( editor.getBlocks )
.toMatchObject( [ expectedParagraphBlock ] );
await expect.poll( editor.getBlocks ).toMatchObject( [
{
name: 'core/paragraph',
attributes: { content: 'After Edit' },
},
] );
} );

test( 'can be created, inserted, and converted to a regular block', async ( {
editor,
page,
requestUtils,
} ) => {
const { id } = await requestUtils.createBlock( {
Expand All @@ -367,32 +345,23 @@ test.describe( 'Synced pattern', () => {
attributes: { ref: id },
} );

// Wait until the pattern is created and inserted.
await editor.canvas.locator( 'text=Hello there!' ).waitFor();

const [ syncedPattern ] = await editor.getBlocks();
const patternRecord = await getPatternRecord(
page,
syncedPattern?.attributes?.ref
);

expect( patternRecord?.content ).toBe(
'<!-- wp:paragraph -->\n<p>Hello there!</p>\n<!-- /wp:paragraph -->'
);

const expectedParagraphBlock = {
name: 'core/paragraph',
attributes: { content: 'Hello there!' },
};
// Check that only the pattern block is present.
const existingBlocks = await editor.getBlocks();
expect(
existingBlocks.every( ( block ) => block.name === 'core/block' )
).toBe( true );

await editor.selectBlocks(
editor.canvas.getByRole( 'document', { name: 'Block: Pattern' } )
);
await editor.clickBlockOptionsMenuItem( 'Detach' );

await expect
.poll( editor.getBlocks )
.toMatchObject( [ expectedParagraphBlock ] );
await expect.poll( editor.getBlocks ).toMatchObject( [
{
name: 'core/paragraph',
attributes: { content: 'Hello there!' },
},
] );
} );

test( 'can be inserted after refresh', async ( {
Expand Down Expand Up @@ -426,20 +395,15 @@ test.describe( 'Synced pattern', () => {
await page.keyboard.type( '/Awesome block' );
await page.getByRole( 'option', { name: 'Awesome block' } ).click();

const [ syncedPattern ] = await editor.getBlocks();
const patternRecord = await getPatternRecord(
page,
syncedPattern?.attributes?.ref
);

expect( patternRecord?.content ).toBe(
'<!-- wp:paragraph -->\n<p>Awesome Paragraph</p>\n<!-- /wp:paragraph -->'
);
// Check that the pattern block is present.
const existingBlocks = await editor.getBlocks();
expect(
existingBlocks.every( ( block ) => block.name === 'core/block' )
).toBe( true );
} );

test( 'can be created from multiselection and converted back to regular blocks', async ( {
editor,
page,
pageUtils,
} ) => {
await editor.insertBlock( {
Expand Down Expand Up @@ -467,48 +431,35 @@ test.describe( 'Synced pattern', () => {
.getByRole( 'button', { name: 'Create' } )
.click();

const expectedParagraphBlocks = [
{
name: 'core/paragraph',
attributes: { content: 'Hello there!' },
},
{
name: 'core/paragraph',
attributes: { content: 'Second paragraph' },
},
];

// Wait until the pattern is created.
await editor.canvas
.getByRole( 'document', {
name: 'Block: Pattern',
} )
.waitFor();

const [ syncedPattern ] = await editor.getBlocks();
const patternRecord = await getPatternRecord(
page,
syncedPattern?.attributes?.ref
);

expect( patternRecord?.content ).toBe(
`<!-- wp:paragraph -->
<p>Hello there!</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph -->
<p>Second paragraph</p>
<!-- /wp:paragraph -->`
);
// Check that only the pattern block is present.
const existingBlocks = await editor.getBlocks();
expect(
existingBlocks.every( ( block ) => block.name === 'core/block' )
).toBe( true );

// Convert the pattern back to regular blocks.
await editor.selectBlocks(
editor.canvas.getByRole( 'document', { name: 'Block: Pattern' } )
);
await editor.clickBlockOptionsMenuItem( 'Detach' );

await expect
.poll( editor.getBlocks )
.toMatchObject( expectedParagraphBlocks );
await expect.poll( editor.getBlocks ).toMatchObject( [
{
name: 'core/paragraph',
attributes: { content: 'Hello there!' },
},
{
name: 'core/paragraph',
attributes: { content: 'Second paragraph' },
},
] );
} );

// Check for regressions of https://github.com/WordPress/gutenberg/pull/26484.
Expand Down

0 comments on commit c122b79

Please sign in to comment.