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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* WordPress dependencies
*/
import { createRoot, useEffect } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
Expand Down Expand Up @@ -309,6 +310,11 @@ function TitleToolbarWrapper(): React.JSX.Element {
// Create a container for our toolbar
toolbarContainer = editorDoc.createElement( 'div' );
toolbarContainer.className = 'ai-title-toolbar-container';
toolbarContainer.setAttribute( 'role', 'toolbar' );
toolbarContainer.setAttribute(
'aria-label',
__( 'Generate title toolbar', 'ai' )
);
toolbarContainer.style.cssText =
'display: none; position: absolute; z-index: 1000; top: -60px;';

Expand Down
112 changes: 61 additions & 51 deletions tests/e2e/specs/experiments/title-generation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,44 +50,45 @@ test.describe( 'Title Generation Experiment', () => {
await editor.saveDraft();

// Click into the title field.
await editor.canvas.locator( '.editor-post-title__input' ).click();
await editor.canvas
.getByRole( 'textbox', { name: 'Add Title' } )
.click();

const generateTitleToolbar = editor.canvas.getByRole( 'toolbar', {
name: 'Generate title toolbar',
} );

// Ensure the title toolbar is visible with "Generate" label.
await expect(
editor.canvas.locator( '.ai-title-toolbar-container', {
hasText: 'Generate',
} )
generateTitleToolbar.filter( { hasText: 'Generate' } )
).toBeVisible();

// Click the Generate button.
await editor.canvas
.locator( '.ai-title-toolbar-container button' )
await generateTitleToolbar
.getByRole( 'button', { name: 'Generate' } )
.click();

const modal = page.getByRole( 'dialog', {
name: 'Title suggestion',
} );

// Ensure the title modal is visible.
await expect(
page.locator( '.ai-title-generation-modal' )
).toBeVisible();
await expect( modal ).toBeVisible();

// Ensure the generated title textarea is visible.
await expect(
page.locator( '.ai-title-generation-modal textarea' )
page.getByRole( 'textbox', { name: 'Generated title' } )
).toBeVisible();

// Click Insert to apply the generated title.
await page
.locator( '.ai-title-generation-modal' )
.getByRole( 'button', { name: 'Insert' } )
.click();
await modal.getByRole( 'button', { name: 'Insert' } ).click();

// Ensure the title modal is closed.
await expect(
page.locator( '.ai-title-generation-modal' )
).not.toBeVisible();
await expect( modal ).not.toBeVisible();

// Ensure the title is updated.
await expect(
editor.canvas.locator( '.editor-post-title__input' )
editor.canvas.getByRole( 'textbox', { name: 'Add Title' } )
).toHaveText(
'Edit or Delete Your First WordPress Post to Begin Your Blogging Adventure',
{ timeout: 10000 }
Expand Down Expand Up @@ -119,44 +120,41 @@ test.describe( 'Title Generation Experiment', () => {
await editor.saveDraft();

// Click into the title field.
await editor.canvas.locator( '.editor-post-title__input' ).click();
await editor.canvas
.getByRole( 'textbox', { name: 'Add Title' } )
.click();

// Ensure the title toolbar is visible with "Regenerate" label.
await expect(
editor.canvas.locator( '.ai-title-toolbar-container', {
hasText: 'Regenerate',
} )
).toBeVisible();
const generateTitleToolbar = editor.canvas.getByRole( 'toolbar', {
name: 'Generate title toolbar',
} );

// Ensure the title toolbar is visible.
await expect( generateTitleToolbar ).toBeVisible();

// Click the Regenerate button.
await editor.canvas
.locator( '.ai-title-toolbar-container button' )
await generateTitleToolbar
.getByRole( 'button', { name: 'Regenerate' } )
.click();

const modal = page.getByRole( 'dialog', { name: 'Title suggestion' } );

// Ensure the title modal is visible.
await expect(
page.locator( '.ai-title-generation-modal' )
).toBeVisible();
await expect( modal ).toBeVisible();

// Ensure the generated title textarea is visible.
await expect(
page.locator( '.ai-title-generation-modal textarea' )
page.getByRole( 'textbox', { name: 'Generated title' } )
).toBeVisible();

// Click Insert to apply the generated title.
await page
.locator( '.ai-title-generation-modal' )
.getByRole( 'button', { name: 'Insert' } )
.click();
await modal.getByRole( 'button', { name: 'Insert' } ).click();

// Ensure the title modal is closed.
await expect(
page.locator( '.ai-title-generation-modal' )
).not.toBeVisible();
await expect( modal ).not.toBeVisible();

// Ensure the title is updated.
await expect(
editor.canvas.locator( '.editor-post-title__input' )
editor.canvas.getByRole( 'textbox', { name: 'Add Title' } )
).toHaveText(
'Edit or Delete Your First WordPress Post to Begin Your Blogging Adventure',
{ timeout: 10000 }
Expand Down Expand Up @@ -188,18 +186,22 @@ test.describe( 'Title Generation Experiment', () => {
await editor.saveDraft();

// Click into the title field to reveal the toolbar.
await editor.canvas.locator( '.editor-post-title__input' ).click();
await editor.canvas
.getByRole( 'textbox', { name: 'Add Title' } )
.click();

const generateTitleToolbar = editor.canvas.getByRole( 'toolbar', {
name: 'Generate title toolbar',
} );

// The toolbar is visible with the "Generate" label.
await expect(
editor.canvas.locator( '.ai-title-toolbar-container', {
hasText: 'Generate',
} )
).toBeVisible();
await expect( generateTitleToolbar ).toBeVisible();

await expect(
editor.canvas.locator( '.ai-title-toolbar-container button' )
).toHaveAttribute( 'aria-disabled', 'true' );
generateTitleToolbar
.getByRole( 'button' )
.filter( { hasText: 'Generate' } )
).toBeDisabled();
} );

test( 'Ensure the Title Generation Experiment UI is not visible when Experiments are globally disabled', async ( {
Expand All @@ -225,11 +227,15 @@ test.describe( 'Title Generation Experiment', () => {
await editor.saveDraft();

// Click into the title field.
await editor.canvas.locator( '.editor-post-title__input' ).click();
await editor.canvas
.getByRole( 'textbox', { name: 'Add Title' } )
.click();

// Ensure the title toolbar is not there.
await expect(
editor.canvas.locator( '.ai-title-toolbar-container' )
editor.canvas.getByRole( 'toolbar', {
name: 'Generate title toolbar',
} )
).not.toBeVisible();
} );

Expand All @@ -256,11 +262,15 @@ test.describe( 'Title Generation Experiment', () => {
await editor.saveDraft();

// Click into the title field.
await editor.canvas.locator( '.editor-post-title__input' ).click();
await editor.canvas
.getByRole( 'textbox', { name: 'Add Title' } )
.click();

// Ensure the title toolbar is not there.
await expect(
editor.canvas.locator( '.ai-title-toolbar-container' )
editor.canvas.getByRole( 'toolbar', {
name: 'Generate title toolbar',
} )
).not.toBeVisible();
} );
} );
Loading