Skip to content
Draft
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
198 changes: 198 additions & 0 deletions e2e/get-title-and-content.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import { test, expect } from '@playwright/test';
import EditorPage from './editor-page';

// Core migrates these legacy alignment attributes when parsing, so the
// serialized blocks differ from the stored markup.
const MIGRATED_CONTENT = [
'<!-- wp:heading {"textAlign":"center"} -->',
'<h2 class="wp-block-heading has-text-align-center">Centered heading</h2>',
'<!-- /wp:heading -->',
'',
'<!-- wp:paragraph {"align":"center"} -->',
'<p class="has-text-align-center">Centered paragraph</p>',
'<!-- /wp:paragraph -->',
].join( '\n' );

test.describe( 'getTitleAndContent', () => {
test( 'returns correct title and content before any edits', async ( {
page,
Expand Down Expand Up @@ -28,6 +40,192 @@ test.describe( 'getTitleAndContent', () => {
expect( result.changed ).toBe( false );
} );

test( 'reports no change for untouched blocks migrated on parse', async ( {
page,
} ) => {
const editor = new EditorPage( page );
await editor.setup( {
post: {
id: 1,
type: 'post',
status: 'draft',
title: '',
content: MIGRATED_CONTENT,
},
} );

const result = await editor.getTitleAndContent();

expect( result.content ).toBe( MIGRATED_CONTENT );
expect( result.changed ).toBe( false );
} );

test( 'reports no change for edits that cancel out on migrated blocks', async ( {
page,
} ) => {
const editor = new EditorPage( page );
await editor.setup( {
post: {
id: 1,
type: 'post',
status: 'draft',
title: '',
content: MIGRATED_CONTENT,
},
} );

await page.getByText( 'Centered paragraph' ).click();
await page.keyboard.press( 'End' );
await page.keyboard.type( 'x' );
await page.keyboard.press( 'Backspace' );

const isDirty = await page.evaluate( () =>
window.wp.data.select( 'core/editor' ).isEditedPostDirty()
);
const result = await editor.getTitleAndContent();

// The edits are real and the content is now the migrated markup, so
// only a serialized baseline recognizes it as unchanged.
expect( isDirty ).toBe( true );
expect( result.content ).not.toBe( MIGRATED_CONTENT );
expect( result.changed ).toBe( false );
} );

test( 'reports no change for non-persistent block updates', async ( {
page,
} ) => {
const editor = new EditorPage( page );
await editor.setup( {
post: {
id: 1,
type: 'post',
status: 'draft',
title: '',
content:
'<!-- wp:paragraph -->\n<p>Hello</p>\n<!-- /wp:paragraph -->',
},
} );

// Mirrors blocks that cache derived attributes on mount.
await page.evaluate( () => {
const blockEditor = window.wp.data.dispatch( 'core/block-editor' );
const [ clientId ] = window.wp.data
.select( 'core/block-editor' )
.getBlockOrder();
blockEditor.__unstableMarkNextChangeAsNotPersistent();
blockEditor.updateBlockAttributes( clientId, {
className: 'is-cached',
} );
} );

const untouched = await editor.getTitleAndContent();
expect( untouched.content ).toContain( 'is-cached' );
expect( untouched.changed ).toBe( false );

await page
.getByRole( 'document', { name: /Block: Paragraph/ } )
.click();
await page.keyboard.press( 'End' );
await page.keyboard.type( ' world' );

const edited = await editor.getTitleAndContent();
expect( edited.content ).toContain( 'Hello world' );
expect( edited.changed ).toBe( true );
} );

test( 'reports typing that continues after a read', async ( { page } ) => {
const editor = new EditorPage( page );
await editor.setup( {
post: {
id: 1,
type: 'post',
status: 'draft',
title: '',
content:
'<!-- wp:paragraph -->\n<p>Hello</p>\n<!-- /wp:paragraph -->',
},
} );
const isSameContentEdit = () =>
page.evaluate( () => {
const edit = window.wp.data
.select( 'core' )
.getEntityRecordEdits( 'postType', 'post', 1 )?.content;
const same = edit === window.__lastContentEdit;
window.__lastContentEdit = edit;
return same;
} );

await page
.getByRole( 'document', { name: /Block: Paragraph/ } )
.click();
await page.keyboard.press( 'End' );
await page.keyboard.type( ' wo' );
await isSameContentEdit();
const first = await editor.getTitleAndContent();
await page.keyboard.type( 'rld' );
const second = await editor.getTitleAndContent();

// Core keeps one edit for a run of typing in the same field.
expect(
await isSameContentEdit(),
'Precondition: core no longer keeps one edit for a run of typing, so revisit this test rather than the bridge'
).toBe( true );
expect( first.changed ).toBe( true );
expect( second.content ).toContain( 'Hello world' );
expect( second.changed ).toBe( true );
} );

test( 'reports undoing to the opened content after a read', async ( {
page,
} ) => {
const editor = new EditorPage( page );
await editor.setup( {
post: {
id: 1,
type: 'post',
status: 'draft',
title: '',
content: MIGRATED_CONTENT,
},
} );

await page.getByText( 'Centered paragraph' ).click();
await page.keyboard.press( 'End' );
await page.keyboard.type( 'x' );
const edited = await editor.getTitleAndContent();
await page.evaluate( () => window.editor.undo() );
const undone = await editor.getTitleAndContent();

// The host holds the edit, so it must get the opened content back.
expect( edited.changed ).toBe( true );
expect( undone.content ).toBe( MIGRATED_CONTENT );
expect( undone.changed ).toBe( true );
} );

test( 'reports no change for undoing to the opened content without a read', async ( {
page,
} ) => {
const editor = new EditorPage( page );
await editor.setup( {
post: {
id: 1,
type: 'post',
status: 'draft',
title: '',
content: MIGRATED_CONTENT,
},
} );

await page.getByText( 'Centered paragraph' ).click();
await page.keyboard.press( 'End' );
await page.keyboard.type( 'x' );
await page.evaluate( () => window.editor.undo() );
const result = await editor.getTitleAndContent();

expect( result.content ).toBe( MIGRATED_CONTENT );
expect( result.changed ).toBe( false );
} );

test( 'returns plain strings after editing the title', async ( {
page,
} ) => {
Expand Down
137 changes: 136 additions & 1 deletion src/components/editor/test/use-host-bridge.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useHostBridge } from '../use-host-bridge';

const mockGetEditedPostAttribute = vi.fn();
const mockGetEditedPostContent = vi.fn();
const mockGetEntityRecordEdits = vi.fn();
const mockGetSelectedBlockClientId = vi.fn();
const mockGetBlock = vi.fn();
const mockGetSelectionStart = vi.fn();
Expand All @@ -20,6 +21,9 @@ vi.mock( '@wordpress/data', () => ( {
getEditedPostContent: mockGetEditedPostContent,
};
}
if ( store?.name === 'core' ) {
return { getEntityRecordEdits: mockGetEntityRecordEdits };
}
// block-editor store selectors
return {
getSelectedBlockClientId: mockGetSelectedBlockClientId,
Expand All @@ -37,7 +41,7 @@ vi.mock( '@wordpress/data', () => ( {
selectionChange: mockSelectionChange,
} ),
} ) );
vi.mock( '@wordpress/core-data' );
vi.mock( '@wordpress/core-data', () => ( { store: { name: 'core' } } ) );
vi.mock( '@wordpress/editor' );
vi.mock( '@wordpress/blocks' );
vi.mock( '@wordpress/rich-text', () => ( {
Expand Down Expand Up @@ -72,6 +76,11 @@ describe( 'useHostBridge', () => {

beforeEach( () => {
vi.clearAllMocks();
// Default to a new persistent edit on every read, so tests that only
// vary the content exercise the markup comparison.
mockGetEntityRecordEdits.mockImplementation( () => ( {
content: () => {},
} ) );
editorRef = { current: document.createElement( 'div' ) };
markBridgeReady = vi.fn();
// Reset window.editor to initial state (matches the module-level
Expand Down Expand Up @@ -270,6 +279,132 @@ describe( 'useHostBridge', () => {
} );
} );

it( 'getTitleAndContent ignores blocks migrated while parsing', () => {
// The baseline serializes the migrated blocks, but without an edit
// the editor still returns the stored markup.
parse.mockImplementationOnce( () => 'Migrated' );
serialize.mockImplementationOnce( ( blocks ) => blocks );
mockGetEntityRecordEdits.mockReturnValue( {} );
mockGetEditedPostAttribute.mockReturnValue( '' );
mockGetEditedPostContent.mockReturnValue( 'Stored' );

renderHook( () =>
useHostBridge( defaultPost, editorRef, markBridgeReady )
);

expect( window.editor.getTitleAndContent() ).toEqual( {
title: '',
content: 'Stored',
changed: false,
} );
} );

it( 'getTitleAndContent ignores non-persistent changes', () => {
parse.mockImplementationOnce( ( value ) => value );
serialize.mockImplementationOnce( ( blocks ) => blocks );
mockGetEntityRecordEdits.mockReturnValue( { content: () => {} } );
mockGetEditedPostAttribute.mockReturnValue( '' );
mockGetEditedPostContent.mockReturnValueOnce( 'Original' );

renderHook( () =>
useHostBridge( defaultPost, editorRef, markBridgeReady )
);

// A block wrote an attribute on mount without replacing the edit.
mockGetEditedPostContent.mockReturnValue( 'Original with cache' );

expect( window.editor.getTitleAndContent().changed ).toBe( false );
} );

it( 'getTitleAndContent reports a persistent change after non-persistent ones', () => {
parse.mockImplementationOnce( ( value ) => value );
serialize.mockImplementationOnce( ( blocks ) => blocks );
mockGetEntityRecordEdits.mockReturnValue( {} );
mockGetEditedPostAttribute.mockReturnValue( '' );
mockGetEditedPostContent.mockReturnValueOnce( 'Original' );

renderHook( () =>
useHostBridge( defaultPost, editorRef, markBridgeReady )
);

mockGetEditedPostContent.mockReturnValue( 'Original with cache' );
expect( window.editor.getTitleAndContent().changed ).toBe( false );

mockGetEntityRecordEdits.mockReturnValue( { content: () => {} } );
mockGetEditedPostContent.mockReturnValue( 'Edited with cache' );
expect( window.editor.getTitleAndContent() ).toEqual( {
title: '',
content: 'Edited with cache',
changed: true,
} );

// The baseline advanced, so the same state reports no further change.
expect( window.editor.getTitleAndContent().changed ).toBe( false );
} );

it( 'getTitleAndContent reports no change when an edit restores the baseline', () => {
parse.mockImplementationOnce( ( value ) => value );
serialize.mockImplementationOnce( ( blocks ) => blocks );
mockGetEditedPostAttribute.mockReturnValue( '' );
mockGetEditedPostContent.mockReturnValue( 'Original' );

renderHook( () =>
useHostBridge( defaultPost, editorRef, markBridgeReady )
);

// The default mock reports an edit, but the markup matches the baseline.
expect( window.editor.getTitleAndContent().changed ).toBe( false );
} );

it( 'getTitleAndContent reports typing that continues after a read', () => {
parse.mockImplementationOnce( ( value ) => value );
serialize.mockImplementationOnce( ( blocks ) => blocks );
mockGetEntityRecordEdits.mockReturnValue( {} );
mockGetEditedPostAttribute.mockReturnValue( '' );
mockGetEditedPostContent.mockReturnValueOnce( 'Hello' );

renderHook( () =>
useHostBridge( defaultPost, editorRef, markBridgeReady )
);

// Core keeps one edit for a run of typing in the same field.
mockGetEntityRecordEdits.mockReturnValue( { content: () => {} } );
mockGetEditedPostContent.mockReturnValue( 'Hello wo' );
expect( window.editor.getTitleAndContent().changed ).toBe( true );

mockGetEditedPostContent.mockReturnValue( 'Hello world' );
expect( window.editor.getTitleAndContent() ).toEqual( {
title: '',
content: 'Hello world',
changed: true,
} );
} );

it( 'getTitleAndContent reports undoing to the opened content after a read', () => {
parse.mockImplementationOnce( () => 'Migrated' );
serialize.mockImplementationOnce( ( blocks ) => blocks );
mockGetEntityRecordEdits.mockReturnValue( {} );
mockGetEditedPostAttribute.mockReturnValue( '' );
mockGetEditedPostContent.mockReturnValueOnce( 'Stored' );

renderHook( () =>
useHostBridge( defaultPost, editorRef, markBridgeReady )
);

mockGetEntityRecordEdits.mockReturnValue( { content: () => {} } );
mockGetEditedPostContent.mockReturnValue( 'Edited' );
expect( window.editor.getTitleAndContent().changed ).toBe( true );

// Undo removes the edit, so the host must get the opened content back.
mockGetEntityRecordEdits.mockReturnValue( {} );
mockGetEditedPostContent.mockReturnValue( 'Stored' );
expect( window.editor.getTitleAndContent() ).toEqual( {
title: '',
content: 'Stored',
changed: true,
} );
} );

it( "getTitleAndContent starts from the host's content when the editor has none", () => {
const content =
'<!-- wp:paragraph -->\n<p>Host</p>\n<!-- /wp:paragraph -->';
Expand Down
Loading
Loading