const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' ); const { setCollaboration, } = require( '../collaboration/fixtures/collaboration-utils' ); test.use( { previewUtils: async ( { page }, use ) => { await use( new PreviewUtils( { page } ) ); }, enableCollaboration: async ( { requestUtils }, use ) => { await use( () => setCollaboration( requestUtils, true ) ); await setCollaboration( requestUtils, false ); }, } ); test.describe( 'Preview', () => { test.beforeEach( async ( { admin } ) => { await admin.createNewPost(); } ); test( 'hides viewport dimensions when space is limited and restores them when space returns', async ( { editor, page, } ) => { await page.setViewportSize( { width: 1600, height: 900 } ); await page.getByRole( 'button', { name: 'View', exact: true } ).click(); await page.getByRole( 'menuitemradio', { name: 'Tablet' } ).click(); await page.keyboard.press( 'Escape' ); const footer = page.getByRole( 'region', { name: 'Editor footer' } ); await expect( footer ).toContainText( /Tablet \(781 × \d+\)/ ); const canvas = page.locator( 'iframe[name="editor-canvas"]' ); await expect .poll( async () => Math.round( ( await canvas.boundingBox() ).width ) ) .toBe( 781 ); await page.setViewportSize( { width: 900, height: 900 } ); // Keep the footer present while the settings sidebar reduces the // space available to the canvas. await editor.openDocumentSettingsSidebar(); await expect( footer.getByText( 'Tablet', { exact: true } ) ).toBeVisible(); await expect( footer ).not.toContainText( /Tablet \(781 × \d+\)/ ); await expect .poll( async () => ( await canvas.boundingBox() ).width ) .toBeLessThan( 780 ); await page.setViewportSize( { width: 1600, height: 900 } ); await expect( footer ).toContainText( /Tablet \(781 × \d+\)/ ); await expect .poll( async () => Math.round( ( await canvas.boundingBox() ).width ) ) .toBe( 781 ); } ); test( 'preserves responsive editing when switching through the default viewport', async ( { page, } ) => { const view = page.getByRole( 'button', { name: 'View', exact: true } ); await view.click(); const responsiveStyles = page.getByRole( 'menuitemcheckbox', { name: 'Responsive styles', } ); await expect( responsiveStyles ).toBeDisabled(); await page.getByRole( 'menuitemradio', { name: 'Tablet' } ).click(); await expect( responsiveStyles ).toBeEnabled(); await responsiveStyles.click(); await expect( responsiveStyles ).toBeChecked(); await expect( view ).toHaveClass( /is-responsive-editing/ ); await page.getByRole( 'menuitemradio', { name: 'Desktop' } ).click(); await expect( responsiveStyles ).toBeDisabled(); await expect( responsiveStyles ).toBeChecked(); await expect( view ).not.toHaveClass( /is-responsive-editing/ ); await page.getByRole( 'menuitemradio', { name: 'Tablet' } ).click(); await expect( responsiveStyles ).toBeEnabled(); await expect( responsiveStyles ).toBeChecked(); await expect( view ).toHaveClass( /is-responsive-editing/ ); await page.keyboard.press( 'Escape' ); await expect( page .getByRole( 'region', { name: 'Editor footer' } ) .getByText( 'Responsive styles', { exact: true } ) ).toBeVisible(); } ); test( 'should open a preview window for a new post', async ( { editor, page, previewUtils, requestUtils, } ) => { const editorPage = page; await editor.canvas .locator( 'role=textbox[name="Add title"i]' ) .type( 'Hello World' ); const previewPage = await editor.openPreviewPage( editorPage ); const previewTitle = previewPage.locator( 'role=heading[level=1]' ); await expect( previewTitle ).toHaveText( 'Hello World' ); // When autosave completes for a new post, the URL of the editor should // update to include the ID. Use this to assert on preview URL. await expect( editorPage ).toHaveURL( /[\?&]post=(\d+)/ ); const [ , postId ] = editorPage.url().match( /[\?&]post=(\d+)/ ); const expectedPreviewURL = new URL( requestUtils.baseURL ); expectedPreviewURL.search = `?p=${ postId }&preview=true`; await expect( previewPage ).toHaveURL( expectedPreviewURL.href ); // Return to editor to change title. await editorPage.bringToFront(); await editor.canvas .locator( 'role=textbox[name="Add title"i]' ) .type( '!' ); await previewUtils.waitForPreviewNavigation( previewPage ); // Title in preview should match updated input. await expect( previewTitle ).toHaveText( 'Hello World!' ); // Pressing preview without changes should bring same preview window to // front and reload, but should not show interstitial. await editorPage.bringToFront(); await previewUtils.waitForPreviewNavigation( previewPage ); await expect( previewTitle ).toHaveText( 'Hello World!' ); // Preview for published post (no unsaved changes) directs to canonical URL for post. await editorPage.bringToFront(); await editor.publishPost(); // Close the panel. await page.getByRole( 'button', { name: 'Close panel' } ).click(); // Return to editor to change title. await editorPage.bringToFront(); await editor.canvas .locator( 'role=textbox[name="Add title"i]' ) .fill( 'Hello World! And more.' ); await previewUtils.waitForPreviewNavigation( previewPage ); // Title in preview should match updated input. await expect( previewTitle ).toHaveText( 'Hello World! And more.' ); // Published preview URL should include ID and nonce parameters. const { searchParams } = new URL( previewPage.url() ); expect( searchParams.has( 'preview_id' ) ).toEqual( true ); expect( searchParams.has( 'preview_nonce' ) ).toEqual( true ); // Return to editor. Previewing already-autosaved preview tab should // reuse the opened tab, skipping interstitial. This resolves an edge // cases where the post is dirty but not autosaveable (because the // autosave is already up-to-date). // // See: https://github.com/WordPress/gutenberg/issues/7561 await editorPage.bringToFront(); await previewUtils.waitForPreviewNavigation( previewPage ); // Title in preview should match updated input. await expect( previewTitle ).toHaveText( 'Hello World! And more.' ); await previewPage.close(); } ); test( 'should not revert title during a preview right after a save draft', async ( { editor, page, previewUtils, } ) => { await editor.openDocumentSettingsSidebar(); const editorPage = page; // Type aaaaa in the title field. await editor.canvas .locator( 'role=textbox[name="Add title"]' ) .type( 'aaaaa' ); await editorPage.keyboard.press( 'Tab' ); // Save the post as a draft. await editorPage.getByRole( 'button', { name: 'Save draft' } ).click(); await editorPage.waitForSelector( 'role=button[name="Dismiss this notice"] >> text=Draft saved' ); // Open the preview page. const previewPage = await editor.openPreviewPage( editorPage ); // Title in preview should match input. const previewTitle = previewPage.locator( 'role=heading[level=1]' ); await expect( previewTitle ).toHaveText( 'aaaaa' ); // Return to editor. await editorPage.bringToFront(); // Append bbbbb to the title, and tab away from the title so blur event is triggered. await editor.canvas .locator( 'role=textbox[name="Add title"i]' ) .fill( 'aaaaabbbbb' ); await editorPage.keyboard.press( 'Tab' ); // Save draft and open the preview page right after. await editorPage.getByRole( 'button', { name: 'Save draft' } ).click(); await editorPage.waitForSelector( 'role=button[name="Dismiss this notice"] >> text=Draft saved' ); await previewUtils.waitForPreviewNavigation( previewPage ); // Title in preview should match updated input. await expect( previewTitle ).toHaveText( 'aaaaabbbbb' ); await previewPage.close(); } ); // See: https://github.com/WordPress/gutenberg/issues/33758. test( 'should not use stale autosave data after reverting title', async ( { editor, enableCollaboration, page, previewUtils, } ) => { await enableCollaboration(); await Promise.all( [ page.waitForResponse( ( response ) => response.url().includes( 'wp-sync' ) && response.status() === 200 ), page.reload(), ] ); const editorPage = page; // Create and publish a post. await editor.canvas .locator( 'role=textbox[name="Add title"i]' ) .fill( 'Sample Page' ); await editor.insertBlock( { name: 'core/paragraph', attributes: { content: 'original content' }, } ); await editor.publishPost(); // Close the panel. await page.getByRole( 'button', { name: 'Close panel' } ).click(); // Change the title and preview to trigger an autosave. await editor.canvas .locator( 'role=textbox[name="Add title"i]' ) .fill( 'Sample Page 2' ); const previewPage = await editor.openPreviewPage( editorPage ); const previewTitle = previewPage.locator( 'role=heading[level=1]' ); await expect( previewTitle ).toHaveText( 'Sample Page 2' ); // Return to editor, revert the title, and preview again. await editorPage.bringToFront(); await editor.canvas .locator( 'role=textbox[name="Add title"i]' ) .fill( 'Sample Page' ); await previewUtils.waitForPreviewNavigation( previewPage ); // Preview should show the reverted title, not the stale autosave. await expect( previewTitle ).toHaveText( 'Sample Page' ); await previewPage.close(); } ); // Verify correct preview. See: https://github.com/WordPress/gutenberg/issues/33616 test( 'should display the correct preview when switching between published and draft statuses', async ( { editor, page, previewUtils, } ) => { const editorPage = page; // Type Lorem in the title field. await editor.canvas .locator( 'role=textbox[name="Add title"i]' ) .type( 'Lorem' ); await editor.openDocumentSettingsSidebar(); // Open the preview page. const previewPage = await editor.openPreviewPage( editorPage ); // Title in preview should match input. const previewTitle = previewPage.locator( 'role=heading[level=1]' ); await expect( previewTitle ).toHaveText( 'Lorem' ); // Return to editor and publish post. await editorPage.bringToFront(); await editor.publishPost(); // Close the panel. await page.getByRole( 'button', { name: 'Close panel' } ).click(); // Change the title and preview again. await editor.canvas .locator( 'role=textbox[name="Add title"i]' ) .type( ' Ipsum' ); await previewUtils.waitForPreviewNavigation( previewPage ); // Title in preview should match updated input. await expect( previewTitle ).toHaveText( 'Lorem Ipsum' ); // Return to editor and switch to Draft. await editorPage.bringToFront(); await page.getByRole( 'button', { name: 'Edit Status' } ).click(); await page.getByRole( 'radio', { name: 'Draft' } ).click(); await page .getByRole( 'region', { name: 'Editor top bar' } ) .getByRole( 'button', { name: 'Save', exact: true, } ) .click(); // Wait for the status change. // @see https://github.com/WordPress/gutenberg/pull/43933 await expect( page.locator( 'role=button[name="Publish"i]' ) ).toBeVisible(); // Change the title. await editor.canvas .locator( 'role=textbox[name="Add title"i]' ) .type( ' Draft' ); // Open the preview page. await previewUtils.waitForPreviewNavigation( previewPage ); // Title in preview should match updated input. await expect( previewTitle ).toHaveText( 'Lorem Ipsum Draft' ); await previewPage.close(); } ); } ); test.describe( 'Preview with Custom Fields enabled', () => { test.beforeAll( async ( { requestUtils } ) => { // Document-Isolation-Policy places the editor in its own agent cluster. // Preview opens frontend pages without the DIP header, creating an // agent cluster mismatch that breaks popup reuse and cross-window // communication. await requestUtils.activatePlugin( 'gutenberg-test-plugin-disable-client-side-media-processing' ); } ); test.beforeEach( async ( { admin, previewUtils } ) => { await admin.createNewPost(); await previewUtils.toggleCustomFieldsOption( true ); } ); test.afterEach( async ( { previewUtils } ) => { await previewUtils.toggleCustomFieldsOption( false ); } ); test.afterAll( async ( { requestUtils } ) => { await requestUtils.deactivatePlugin( 'gutenberg-test-plugin-disable-client-side-media-processing' ); } ); // Catch regressions of https://github.com/WordPress/gutenberg/issues/12617 test( 'displays edits to the post title and content in the preview', async ( { editor, page, previewUtils, } ) => { const editorPage = page; // Add an initial title and content. await editor.canvas .locator( 'role=textbox[name="Add title"i]' ) .type( 'title 1' ); await editor.insertBlock( { name: 'core/paragraph', attributes: { content: 'content 1' }, } ); // Publish the post and then close the publish panel. await editor.publishPost(); // Close the panel. await page.getByRole( 'button', { name: 'Close panel' } ).click(); // Open the preview page. const previewPage = await editor.openPreviewPage(); // Check the title and preview match. const previewTitle = previewPage.locator( 'role=heading[level=1]' ); await expect( previewTitle ).toHaveText( 'title 1' ); // No semantics we can grab here; it's just a
inside a