Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Navigation block: e2e code quality fixes #48071

Merged
merged 9 commits into from
Feb 15, 2023
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ module.exports = {
rules: {
'@wordpress/no-global-active-element': 'off',
'@wordpress/no-global-get-selection': 'off',
'playwright/no-page-pause': 'error',
'no-restricted-syntax': [
'error',
{
Expand Down
36 changes: 17 additions & 19 deletions packages/e2e-test-utils-playwright/src/request-utils/menus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export interface NavigationMenu {
/**
* Create a classic menu
*
* @param {string} name Menu name.
* @return {string} Menu content.
* @param name Menu name.
* @return Menu content.
*/
export async function createClassicMenu( this: RequestUtils, name: string ) {
const menuItems = [
Expand All @@ -37,28 +37,26 @@ export async function createClassicMenu( this: RequestUtils, name: string ) {
},
} );

if ( menuItems?.length ) {
await this.batchRest(
menuItems.map( ( menuItem ) => ( {
method: 'POST',
path: `/wp/v2/menu-items`,
body: {
menus: menu.id,
object_id: undefined,
...menuItem,
parent: undefined,
},
} ) )
);
}
await this.batchRest(
menuItems.map( ( menuItem ) => ( {
method: 'POST',
path: `/wp/v2/menu-items`,
body: {
menus: menu.id,
object_id: undefined,
...menuItem,
parent: undefined,
},
} ) )
);

return menu;
}

/**
* Create a navigation menu
*
* @param {Object} menuData navigation menu post data.
* @param menuData navigation menu post data.
* @return {string} Menu content.
*/
export async function createNavigationMenu(
Expand All @@ -84,7 +82,7 @@ export async function deleteAllMenus( this: RequestUtils ) {
path: `/wp/v2/navigation/`,
} );

if ( navMenus?.length ) {
if ( navMenus.length ) {
await this.batchRest(
navMenus.map( ( menu ) => ( {
method: 'DELETE',
Expand All @@ -97,7 +95,7 @@ export async function deleteAllMenus( this: RequestUtils ) {
path: `/wp/v2/menus/`,
} );

if ( classicMenus?.length ) {
if ( classicMenus.length ) {
await this.batchRest(
classicMenus.map( ( menu ) => ( {
method: 'DELETE',
Expand Down
64 changes: 24 additions & 40 deletions test/e2e/specs/editor/blocks/navigation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
*/
const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' );

test.use( {
navBlockUtils: async ( { editor, page, requestUtils }, use ) => {
await use( new NavigationBlockUtils( { editor, page, requestUtils } ) );
},
} );

test.describe(
'As a user I want the navigation block to fallback to the best possible default',
() => {
Expand Down Expand Up @@ -66,7 +60,6 @@ test.describe(
editor,
page,
requestUtils,
navBlockUtils,
} ) => {
await admin.createNewPost();
const createdMenu = await requestUtils.createNavigationMenu( {
Expand All @@ -78,7 +71,11 @@ test.describe(
await editor.insertBlock( { name: 'core/navigation' } );

// Check the block in the canvas.
await navBlockUtils.selectNavigationItemOnCanvas( 'WordPress' );
await expect(
editor.canvas.locator(
`role=textbox[name="Navigation link text"i] >> text="WordPress"`
)
).toBeVisible();

// Check the markup of the block is correct.
await editor.publishPost();
Expand All @@ -91,14 +88,19 @@ test.describe(
await page.locator( 'role=button[name="Close panel"i]' ).click();

// Check the block in the frontend.
await navBlockUtils.selectNavigationItemOnFrontend( 'WordPress' );
await page.goto( '/' );
await expect(
page.locator(
`role=navigation >> role=link[name="WordPress"i]`
)
).toBeVisible();
} );

test( 'default to the only existing classic menu if there are no block menus', async ( {
admin,
MaggieCabrera marked this conversation as resolved.
Show resolved Hide resolved
page,
editor,
requestUtils,
navBlockUtils,
} ) => {
// Create a classic menu.
await requestUtils.createClassicMenu( 'Test Classic 1' );
Expand All @@ -113,42 +115,24 @@ test.describe(
] );

// Check the block in the canvas.
await editor.page.pause();
await navBlockUtils.selectNavigationItemOnCanvas( 'Custom link' );
await expect(
editor.canvas.locator(
`role=textbox[name="Navigation link text"i] >> text="Custom link"`
)
).toBeVisible();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting that this is already reported as flaky (#48033). We might want to take a look at this test 🙂 .

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right, I'll have a look tomorrow!


// Check the block in the frontend.
await navBlockUtils.selectNavigationItemOnFrontend( 'Custom link' );
await editor.page.pause();
await page.goto( '/' );

await expect(
page.locator(
`role=navigation >> role=link[name="Custom link"i]`
)
).toBeVisible();
} );
}
);

class NavigationBlockUtils {
constructor( { editor, page, requestUtils } ) {
this.editor = editor;
this.page = page;
this.requestUtils = requestUtils;
}

async selectNavigationItemOnCanvas( name ) {
await expect(
this.editor.canvas.locator(
`role=textbox[name="Navigation link text"i] >> text="${ name }"`
)
).toBeVisible();
}

async selectNavigationItemOnFrontend( name ) {
await this.page.goto( '/' );
await this.editor.page.pause();
await expect(
this.page.locator(
`role=navigation >> role=link[name="${ name }"i]`
)
).toBeVisible();
}
}

test.describe( 'Navigation block', () => {
test.describe(
'As a user I want to see a warning if the menu referenced by a navigation block is not available',
Expand Down