-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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: e2e tests: default to classic menu #47867
Changes from all commits
f26c7a0
08fae6e
36f04bb
e46bbd5
dd0c1ca
de0272b
a129cc1
b418078
3e3d551
6f78851
574334d
8acf0fd
2a440cc
0dab307
4d4d736
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { RequestUtils } from './index'; | ||
|
||
export interface MenuData { | ||
title: string; | ||
content: string; | ||
} | ||
export interface NavigationMenu { | ||
id: number; | ||
content: string; | ||
status: 'publish' | 'future' | 'draft' | 'pending' | 'private'; | ||
} | ||
|
||
/** | ||
* Create a classic menu | ||
* | ||
* @param {string} name Menu name. | ||
* @return {string} Menu content. | ||
*/ | ||
export async function createClassicMenu( this: RequestUtils, name: string ) { | ||
const menuItems = [ | ||
{ | ||
title: 'Custom link', | ||
url: 'http://localhost:8889/', | ||
type: 'custom', | ||
menu_order: 1, | ||
}, | ||
]; | ||
|
||
const menu = await this.rest< NavigationMenu >( { | ||
method: 'POST', | ||
path: `/wp/v2/menus/`, | ||
data: { | ||
name, | ||
}, | ||
} ); | ||
|
||
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, | ||
}, | ||
} ) ) | ||
); | ||
} | ||
|
||
return menu; | ||
} | ||
|
||
/** | ||
* Create a navigation menu | ||
* | ||
* @param {Object} menuData navigation menu post data. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Prefer TS types than JSDoc types. I think we can just omit the type here. * @param menuData navigation menu post data. |
||
* @return {string} Menu content. | ||
*/ | ||
export async function createNavigationMenu( | ||
this: RequestUtils, | ||
menuData: MenuData | ||
) { | ||
return this.rest( { | ||
method: 'POST', | ||
path: `/wp/v2/navigation/`, | ||
data: { | ||
status: 'publish', | ||
...menuData, | ||
}, | ||
} ); | ||
} | ||
|
||
/** | ||
* Delete all navigation and classic menus | ||
* | ||
*/ | ||
export async function deleteAllMenus( this: RequestUtils ) { | ||
const navMenus = await this.rest< NavigationMenu[] >( { | ||
path: `/wp/v2/navigation/`, | ||
} ); | ||
|
||
if ( navMenus?.length ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: It's always going to return an array so let's remove the optional chaining here :) |
||
await this.batchRest( | ||
navMenus.map( ( menu ) => ( { | ||
method: 'DELETE', | ||
path: `/wp/v2/navigation/${ menu.id }?force=true`, | ||
} ) ) | ||
); | ||
} | ||
|
||
const classicMenus = await this.rest< NavigationMenu[] >( { | ||
path: `/wp/v2/menus/`, | ||
} ); | ||
|
||
if ( classicMenus?.length ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
await this.batchRest( | ||
classicMenus.map( ( menu ) => ( { | ||
method: 'DELETE', | ||
path: `/wp/v2/menus/${ menu.id }?force=true`, | ||
} ) ) | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,8 @@ | |
const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' ); | ||
|
||
test.use( { | ||
navBlockUtils: async ( { page, requestUtils }, use ) => { | ||
await use( new NavigationBlockUtils( { page, requestUtils } ) ); | ||
navBlockUtils: async ( { editor, page, requestUtils }, use ) => { | ||
await use( new NavigationBlockUtils( { editor, page, requestUtils } ) ); | ||
}, | ||
} ); | ||
|
||
|
@@ -17,16 +17,13 @@ test.describe( | |
await requestUtils.activateTheme( 'twentytwentythree' ); | ||
} ); | ||
|
||
test.beforeEach( async ( { admin, navBlockUtils } ) => { | ||
await Promise.all( [ | ||
navBlockUtils.deleteAllNavigationMenus(), | ||
admin.createNewPost(), | ||
] ); | ||
test.beforeEach( async ( { requestUtils } ) => { | ||
await Promise.all( [ requestUtils.deleteAllMenus() ] ); | ||
} ); | ||
|
||
test.afterAll( async ( { requestUtils, navBlockUtils } ) => { | ||
test.afterAll( async ( { requestUtils } ) => { | ||
await Promise.all( [ | ||
navBlockUtils.deleteAllNavigationMenus(), | ||
requestUtils.deleteAllMenus(), | ||
requestUtils.activateTheme( 'twentytwentyone' ), | ||
] ); | ||
} ); | ||
|
@@ -36,8 +33,10 @@ test.describe( | |
} ); | ||
|
||
test( 'default to a list of pages if there are no menus', async ( { | ||
admin, | ||
editor, | ||
} ) => { | ||
await admin.createNewPost(); | ||
await editor.insertBlock( { name: 'core/navigation' } ); | ||
|
||
const pageListBlock = editor.canvas.getByRole( 'document', { | ||
|
@@ -63,24 +62,23 @@ test.describe( | |
} ); | ||
|
||
test( 'default to my only existing menu', async ( { | ||
admin, | ||
editor, | ||
page, | ||
requestUtils, | ||
navBlockUtils, | ||
} ) => { | ||
const createdMenu = await navBlockUtils.createNavigationMenu( { | ||
await admin.createNewPost(); | ||
const createdMenu = await requestUtils.createNavigationMenu( { | ||
title: 'Test Menu 1', | ||
content: | ||
'<!-- wp:navigation-link {"label":"WordPress","type":"custom","url":"http://www.wordpress.org/","kind":"custom","isTopLevelLink":true} /-->', | ||
} ); | ||
|
||
await editor.insertBlock( { name: 'core/navigation' } ); | ||
|
||
//check the block in the canvas. | ||
await expect( | ||
editor.canvas.locator( | ||
'role=textbox[name="Navigation link text"i] >> text="WordPress"' | ||
) | ||
).toBeVisible(); | ||
// Check the block in the canvas. | ||
await navBlockUtils.selectNavigationItemOnCanvas( 'WordPress' ); | ||
|
||
// Check the markup of the block is correct. | ||
await editor.publishPost(); | ||
|
@@ -92,13 +90,35 @@ test.describe( | |
] ); | ||
await page.locator( 'role=button[name="Close panel"i]' ).click(); | ||
|
||
//check the block in the frontend. | ||
await page.goto( '/' ); | ||
await expect( | ||
page.locator( | ||
'role=navigation >> role=link[name="WordPress"i]' | ||
) | ||
).toBeVisible(); | ||
// Check the block in the frontend. | ||
await navBlockUtils.selectNavigationItemOnFrontend( 'WordPress' ); | ||
} ); | ||
|
||
test( 'default to the only existing classic menu if there are no block menus', async ( { | ||
admin, | ||
editor, | ||
requestUtils, | ||
navBlockUtils, | ||
} ) => { | ||
// Create a classic menu. | ||
await requestUtils.createClassicMenu( 'Test Classic 1' ); | ||
await admin.createNewPost(); | ||
|
||
await editor.insertBlock( { name: 'core/navigation' } ); | ||
// We need to check the canvas after inserting the navigation block to be able to target the block. | ||
await expect.poll( editor.getBlocks ).toMatchObject( [ | ||
{ | ||
name: 'core/navigation', | ||
}, | ||
] ); | ||
|
||
// Check the block in the canvas. | ||
await editor.page.pause(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Forgot to delete? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ugh, again! |
||
await navBlockUtils.selectNavigationItemOnCanvas( 'Custom link' ); | ||
|
||
// Check the block in the frontend. | ||
await navBlockUtils.selectNavigationItemOnFrontend( 'Custom link' ); | ||
await editor.page.pause(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto. |
||
} ); | ||
} | ||
); | ||
|
@@ -110,39 +130,21 @@ class NavigationBlockUtils { | |
this.requestUtils = requestUtils; | ||
} | ||
|
||
/** | ||
* Create a navigation menu | ||
* | ||
* @param {Object} menuData navigation menu post data. | ||
* @return {string} Menu content. | ||
*/ | ||
async createNavigationMenu( menuData ) { | ||
return this.requestUtils.rest( { | ||
method: 'POST', | ||
path: `/wp/v2/navigation/`, | ||
data: { | ||
status: 'publish', | ||
...menuData, | ||
}, | ||
} ); | ||
async selectNavigationItemOnCanvas( name ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefer inlining one-liner assertions like this, makes the test a bit easier to read. |
||
await expect( | ||
this.editor.canvas.locator( | ||
`role=textbox[name="Navigation link text"i] >> text="${ name }"` | ||
) | ||
).toBeVisible(); | ||
} | ||
|
||
/** | ||
* Delete all navigation menus | ||
* | ||
*/ | ||
async deleteAllNavigationMenus() { | ||
const menus = await this.requestUtils.rest( { | ||
path: `/wp/v2/navigation/`, | ||
} ); | ||
|
||
if ( ! menus?.length ) return; | ||
|
||
await this.requestUtils.batchRest( | ||
menus.map( ( menu ) => ( { | ||
method: 'DELETE', | ||
path: `/wp/v2/navigation/${ menu.id }?force=true`, | ||
} ) ) | ||
); | ||
async selectNavigationItemOnFrontend( name ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto. |
||
await this.page.goto( '/' ); | ||
await this.editor.page.pause(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto. |
||
await expect( | ||
this.page.locator( | ||
`role=navigation >> role=link[name="${ name }"i]` | ||
) | ||
).toBeVisible(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure why this is needed? Isn't
menuItems
statically defined above?