diff --git a/packages/e2e-tests/specs/site-editor/settings-sidebar.test.js b/packages/e2e-tests/specs/site-editor/settings-sidebar.test.js deleted file mode 100644 index 5ad50ff58b5542..00000000000000 --- a/packages/e2e-tests/specs/site-editor/settings-sidebar.test.js +++ /dev/null @@ -1,117 +0,0 @@ -/** - * WordPress dependencies - */ -import { - deleteAllTemplates, - activateTheme, - getAllBlocks, - selectBlockByClientId, - insertBlock, - visitSiteEditor, -} from '@wordpress/e2e-test-utils'; - -async function toggleSidebar() { - await page.click( - '.edit-site-header-edit-mode__actions button[aria-label="Settings"]' - ); -} - -async function getActiveTabLabel() { - return await page.$eval( - '.edit-site-sidebar-edit-mode__panel-tab.is-active', - ( element ) => element.getAttribute( 'aria-label' ) - ); -} - -async function getTemplateCard() { - return { - title: await page.$eval( - '.edit-site-template-card__title', - ( element ) => element.innerText - ), - description: await page.$eval( - '.edit-site-template-card__description', - ( element ) => element.innerText - ), - }; -} - -describe( 'Settings sidebar', () => { - beforeAll( async () => { - await activateTheme( 'emptytheme' ); - await deleteAllTemplates( 'wp_template' ); - await deleteAllTemplates( 'wp_template_part' ); - } ); - afterAll( async () => { - await deleteAllTemplates( 'wp_template' ); - await deleteAllTemplates( 'wp_template_part' ); - await activateTheme( 'twentytwentyone' ); - } ); - beforeEach( async () => { - await visitSiteEditor(); - } ); - - describe( 'Template tab', () => { - it( 'should open template tab by default if no block is selected', async () => { - await toggleSidebar(); - - expect( await getActiveTabLabel() ).toEqual( - 'Template (selected)' - ); - } ); - - it( "should show the currently selected template's title and description", async () => { - await toggleSidebar(); - - const templateCardBeforeNavigation = await getTemplateCard(); - await visitSiteEditor( { - postId: 'emptytheme//singular', - postType: 'wp_template', - } ); - const templateCardAfterNavigation = await getTemplateCard(); - - expect( templateCardBeforeNavigation ).toMatchObject( { - title: 'Index', - description: 'Displays posts.', - } ); - expect( templateCardAfterNavigation ).toMatchObject( { - title: 'Singular', - description: 'Displays a single post or page.', - } ); - } ); - } ); - - describe( 'Block tab', () => { - it( 'should open block tab by default if a block is selected', async () => { - const allBlocks = await getAllBlocks(); - await selectBlockByClientId( allBlocks[ 0 ].clientId ); - - await toggleSidebar(); - - expect( await getActiveTabLabel() ).toEqual( 'Block (selected)' ); - } ); - } ); - - describe( 'Tab switch based on selection', () => { - it( 'should switch to block tab if we select a block, when Template is selected', async () => { - await toggleSidebar(); - expect( await getActiveTabLabel() ).toEqual( - 'Template (selected)' - ); - // By inserting the block is also selected. - await insertBlock( 'Heading' ); - expect( await getActiveTabLabel() ).toEqual( 'Block (selected)' ); - } ); - it( 'should switch to Template tab when a block was selected and we select the Template', async () => { - await insertBlock( 'Heading' ); - await toggleSidebar(); - expect( await getActiveTabLabel() ).toEqual( 'Block (selected)' ); - await page.evaluate( () => { - wp.data.dispatch( 'core/block-editor' ).clearSelectedBlock(); - } ); - expect( await getActiveTabLabel() ).toEqual( - 'Template (selected)' - ); - } ); - } ); -} ); diff --git a/test/e2e/specs/site-editor/settings-sidebar.spec.js b/test/e2e/specs/site-editor/settings-sidebar.spec.js new file mode 100644 index 00000000000000..7f13f88f5d6957 --- /dev/null +++ b/test/e2e/specs/site-editor/settings-sidebar.spec.js @@ -0,0 +1,154 @@ +/** + * WordPress dependencies + */ +const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' ); + +test.use( { + siteBarFunction: async ( { page }, use ) => { + await use( new Sitebar( { page } ) ); + }, +} ); +test.describe( 'Settings sidebar', () => { + test.beforeAll( async ( { requestUtils } ) => { + await Promise.all( [ + requestUtils.activateTheme( 'emptytheme' ), + requestUtils.deleteAllTemplates( 'wp_template' ), + requestUtils.deleteAllTemplates( 'wp_template_part' ), + ] ); + } ); + + test.afterAll( async ( { requestUtils } ) => { + await requestUtils.activateTheme( 'twentytwentyone' ); + } ); + + test.beforeEach( async ( { admin } ) => { + await admin.visitSiteEditor(); + } ); + + test( 'should open template tab by default if no block is selected', async ( { + page, + } ) => { + // Toggle Sidebar + await page.click( + '.edit-site-header-edit-mode__actions button[aria-label="Settings"]' + ); + const elements = page.locator( + '.edit-site-sidebar-edit-mode__panel-tab.is-active' + ); + await expect( elements ).toContainText( 'Template' ); + } ); + test( "should show the currently selected template's title and description", async ( { + page, + admin, + siteBarFunction, + } ) => { + // Toggle Sidebar + await page.click( + '.edit-site-header-edit-mode__actions button[aria-label="Settings"]' + ); + const templateCardBeforeNavigation = + await siteBarFunction.getTemplateCard(); + await admin.visitSiteEditor( { + postId: 'emptytheme//singular', + postType: 'wp_template', + } ); + const templateCardAfterNavigation = + await siteBarFunction.getTemplateCard(); + expect( templateCardBeforeNavigation ).toMatchObject( { + title: 'Index', + description: 'Displays posts.', + } ); + expect( templateCardAfterNavigation ).toMatchObject( { + title: 'Singular', + description: 'Displays a single post or page.', + } ); + } ); + test( 'should open block tab by default if a block is selected', async ( { + page, + siteBarFunction, + } ) => { + const allBlocks = await siteBarFunction.getAllBlocks(); + await siteBarFunction.selectBlockByClientId( allBlocks[ 0 ].clientId ); + await siteBarFunction.toggleSidebar(); + + const elements = page.locator( + '.edit-site-sidebar-edit-mode__panel-tab.is-active' + ); + await expect( elements ).toContainText( 'Block' ); + } ); + + test( 'should switch to block tab if we select a block, when Template is selected', async ( { + page, + editor, + } ) => { + await page.click( + '.edit-site-header-edit-mode__actions button[aria-label="Settings"]' + ); + const element = page.locator( + '.edit-site-sidebar-edit-mode__panel-tab.is-active' + ); + await expect( element ).not.toBeNull(); + // By inserting the block is also selected. + await editor.insertBlock( { name: 'core/heading' } ); + + const elements = page.locator( + '.edit-site-sidebar-edit-mode__panel-tab.is-active' + ); + await expect( elements ).not.toBeNull(); + } ); + test( 'should switch to Template tab when a block was selected and we select the Template', async ( { + page, + siteBarFunction, + editor, + } ) => { + await editor.insertBlock( { name: 'core/heading' } ); + await siteBarFunction.toggleSidebar(); + const element = page.locator( + '.edit-site-sidebar-edit-mode__panel-tab.is-active' + ); + await expect( element ).toContainText( 'Block' ); + await page.evaluate( () => { + window.wp.data.dispatch( 'core/block-editor' ).clearSelectedBlock(); + } ); + const elements = page.locator( + '.edit-site-sidebar-edit-mode__panel-tab.is-active' + ); + await expect( elements ).toContainText( 'Template' ); + } ); +} ); + +class Sitebar { + constructor( { page } ) { + this.page = page; + } + + async selectBlockByClientId( clientId ) { + await this.page.evaluate( ( id ) => { + window.wp.data.dispatch( 'core/block-editor' ).selectBlock( id ); + }, clientId ); + } + async toggleSidebar() { + await this.page.click( + '.edit-site-header-edit-mode__actions button[aria-label="Settings"]' + ); + } + + async getAllBlocks() { + return this.page.evaluate( () => { + const blocks = window.wp.data + .select( 'core/block-editor' ) + .getBlocks(); + return JSON.parse( JSON.stringify( blocks ) ); + } ); + } + async getTemplateCard() { + const locator1 = this.page.locator( '.edit-site-template-card__title' ); + const locator2 = this.page.locator( + '.edit-site-template-card__description' + ); + return { + title: await locator1.evaluate( ( node ) => node.innerText ), + description: await locator2.evaluate( ( node ) => node.innerText ), + }; + } +}