diff --git a/packages/e2e-test-utils-playwright/src/page/create-new-post.js b/packages/e2e-test-utils-playwright/src/page/create-new-post.js new file mode 100644 index 00000000000000..2c88ec3fc07abb --- /dev/null +++ b/packages/e2e-test-utils-playwright/src/page/create-new-post.js @@ -0,0 +1,66 @@ +/** + * WordPress dependencies + */ +import { addQueryArgs } from '@wordpress/url'; + +/** + * Creates new post. + * + * @this {import('.').PageUtils} + * @param {Object} object Object to create new post, along with tips enabling option. + * @param {string} [object.postType] Post type of the new post. + * @param {string} [object.title] Title of the new post. + * @param {string} [object.content] Content of the new post. + * @param {string} [object.excerpt] Excerpt of the new post. + * @param {boolean} [object.showWelcomeGuide] Whether to show the welcome guide. + */ +export async function createNewPost( { + postType, + title, + content, + excerpt, + showWelcomeGuide = false, +} = {} ) { + const query = addQueryArgs( '', { + post_type: postType, + post_title: title, + content, + excerpt, + } ).slice( 1 ); + + await this.visitAdminPage( 'post-new.php', query ); + + await this.page.waitForSelector( '.edit-post-layout' ); + + const isWelcomeGuideActive = await this.page.evaluate( () => + window.wp.data + .select( 'core/edit-post' ) + .isFeatureActive( 'welcomeGuide' ) + ); + const isFullscreenMode = await this.page.evaluate( () => + window.wp.data + .select( 'core/edit-post' ) + .isFeatureActive( 'fullscreenMode' ) + ); + + if ( showWelcomeGuide !== isWelcomeGuideActive ) { + await this.page.evaluate( () => + window.wp.data + .dispatch( 'core/edit-post' ) + .toggleFeature( 'welcomeGuide' ) + ); + + await this.page.reload(); + await this.page.waitForSelector( '.edit-post-layout' ); + } + + if ( isFullscreenMode ) { + await this.page.evaluate( () => + window.wp.data + .dispatch( 'core/edit-post' ) + .toggleFeature( 'fullscreenMode' ) + ); + + await this.page.waitForSelector( 'body:not(.is-fullscreen-mode)' ); + } +} diff --git a/packages/e2e-test-utils-playwright/src/page/index.ts b/packages/e2e-test-utils-playwright/src/page/index.ts index 2988bc444d5a5b..4ca772f7ae63b7 100644 --- a/packages/e2e-test-utils-playwright/src/page/index.ts +++ b/packages/e2e-test-utils-playwright/src/page/index.ts @@ -6,6 +6,7 @@ import type { Browser, Page, BrowserContext } from '@playwright/test'; /** * Internal dependencies */ +import { createNewPost } from './create-new-post'; import { getPageError } from './get-page-error'; import { isCurrentURL } from './is-current-url'; import { visitAdminPage } from './visit-admin-page'; @@ -21,6 +22,7 @@ class PageUtils { this.browser = this.context.browser()!; } + createNewPost = createNewPost; getPageError = getPageError; isCurrentURL = isCurrentURL; visitAdminPage = visitAdminPage; diff --git a/packages/e2e-tests/specs/editor/various/new-post.test.js b/packages/e2e-tests/specs/editor/various/new-post.test.js deleted file mode 100644 index 0d1491370cb87b..00000000000000 --- a/packages/e2e-tests/specs/editor/various/new-post.test.js +++ /dev/null @@ -1,99 +0,0 @@ -/** - * WordPress dependencies - */ -import { - activatePlugin, - createNewPost, - deactivatePlugin, -} from '@wordpress/e2e-test-utils'; - -describe( 'new editor state', () => { - beforeAll( async () => { - await activatePlugin( 'gutenberg-test-plugin-post-formats-support' ); - } ); - - beforeEach( async () => { - await createNewPost(); - } ); - - afterAll( async () => { - await deactivatePlugin( 'gutenberg-test-plugin-post-formats-support' ); - } ); - - it( 'should show the New Post page in Gutenberg', async () => { - expect( page.url() ).toEqual( - expect.stringContaining( 'post-new.php' ) - ); - // Should display the blank title. - const title = await page.$( '[aria-label="Add title"]' ); - expect( title ).not.toBeNull(); - // Trim padding non-breaking space. - expect( - await title.evaluate( ( el ) => el.textContent.trim() ) - ).toBeFalsy(); - // Should display the Preview button. - const postPreviewButton = await page.$( - '.editor-post-preview.components-button' - ); - expect( postPreviewButton ).not.toBeNull(); - // Should display the Post Formats UI. - const postFormatsUi = await page.$( '.editor-post-format' ); - expect( postFormatsUi ).not.toBeNull(); - } ); - - it( 'should have no history', async () => { - const undoButton = await page.$( - '.editor-history__undo[aria-disabled="false"]' - ); - const redoButton = await page.$( - '.editor-history__redo[aria-disabled="false"]' - ); - - expect( undoButton ).toBeNull(); - expect( redoButton ).toBeNull(); - } ); - - it( 'should focus the title if the title is empty', async () => { - const activeElementClasses = await page.evaluate( () => { - return Object.values( document.activeElement.classList ); - } ); - const activeElementTagName = await page.evaluate( () => { - return document.activeElement.tagName.toLowerCase(); - } ); - - expect( activeElementClasses ).toContain( 'editor-post-title__input' ); - expect( activeElementTagName ).toEqual( 'h1' ); - } ); - - it( 'should not focus the title if the title exists', async () => { - // Enter a title for this post. - await page.type( '.editor-post-title__input', 'Here is the title' ); - // Save the post as a draft. - await page.click( '.editor-post-save-draft' ); - await page.waitForSelector( '.editor-post-saved-state.is-saved' ); - // Reload the browser so a post is loaded with a title. - await page.reload(); - await page.waitForSelector( '.edit-post-layout' ); - - const activeElementClasses = await page.evaluate( () => { - return Object.values( document.activeElement.classList ); - } ); - const activeElementTagName = await page.evaluate( () => { - return document.activeElement.tagName.toLowerCase(); - } ); - - expect( activeElementClasses ).not.toContain( - 'editor-post-title__input' - ); - // The document `body` should be the `activeElement`, because nothing is - // focused by default when a post already has a title. - expect( activeElementTagName ).toEqual( 'body' ); - } ); - - it( 'should be saveable with sufficient initial edits', async () => { - await createNewPost( { title: 'Here is the title' } ); - - // Verify saveable by presence of the Save Draft button. - await page.$( 'button.editor-post-save-draft' ); - } ); -} ); diff --git a/test/e2e/specs/editor/various/new-post.spec.js b/test/e2e/specs/editor/various/new-post.spec.js new file mode 100644 index 00000000000000..4f695da2f00e54 --- /dev/null +++ b/test/e2e/specs/editor/various/new-post.spec.js @@ -0,0 +1,104 @@ +/** + * WordPress dependencies + */ +const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' ); + +test.describe( 'new editor state', () => { + test.beforeAll( async ( { requestUtils } ) => { + await requestUtils.activatePlugin( + 'gutenberg-test-plugin-post-formats-support' + ); + } ); + + test.afterAll( async ( { requestUtils } ) => { + await requestUtils.deactivatePlugin( + 'gutenberg-test-plugin-post-formats-support' + ); + } ); + + test( 'should show the New Post page in Gutenberg', async ( { + page, + pageUtils, + } ) => { + await pageUtils.createNewPost(); + + await expect( page ).toHaveURL( /post-new.php/ ); + + // Should display the blank title. + const title = page.locator( 'role=textbox[name="Add title"i]' ); + await expect( title ).toBeEditable(); + await expect( title ).toHaveText( '' ); + + // Should display the Preview button. + await expect( + page.locator( 'role=button[name="Preview"i]' ) + ).toBeVisible(); + + // Should display the Post Formats UI. + await expect( + page.locator( 'role=combobox[name="Post Format"i]' ) + ).toBeVisible(); + } ); + + test( 'should have no history', async ( { page, pageUtils } ) => { + await pageUtils.createNewPost(); + + await expect( + page.locator( 'role=button[name="Undo"i]' ) + ).toBeDisabled(); + await expect( + page.locator( 'role=button[name="Redo"i]' ) + ).toBeDisabled(); + } ); + + test( 'should focus the title if the title is empty', async ( { + page, + pageUtils, + } ) => { + await pageUtils.createNewPost(); + + await expect( + page.locator( 'role=textbox[name="Add title"i]' ) + ).toBeFocused(); + } ); + + test( 'should not focus the title if the title exists', async ( { + page, + pageUtils, + } ) => { + await pageUtils.createNewPost(); + + // Enter a title for this post. + await page.type( + 'role=textbox[name="Add title"i]', + 'Here is the title' + ); + // Save the post as a draft. + await page.click( 'role=button[name="Save draft"i]' ); + await page.waitForSelector( + 'role=button[name="Dismiss this notice"] >> text=Draft saved' + ); + + // Reload the browser so a post is loaded with a title. + await page.reload(); + await page.waitForSelector( '.edit-post-layout' ); + + // The document `body` should be the `activeElement`, because nothing is + // focused by default when a post already has a title. + await expect( page.locator( 'body' ) ).toBeFocused(); + } ); + + test( 'should be saveable with sufficient initial edits', async ( { + page, + pageUtils, + } ) => { + await pageUtils.createNewPost( { title: 'Here is the title' } ); + + // Verify saveable by presence of the Save Draft button. + const saveDraftButton = page.locator( + 'role=button[name="Save draft"i]' + ); + await expect( saveDraftButton ).toBeVisible(); + await expect( saveDraftButton ).toBeEnabled(); + } ); +} );