-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial commit migrating puppetear utils into playwright and typescript
- Loading branch information
1 parent
53a4d51
commit 552438f
Showing
4 changed files
with
333 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
179 changes: 179 additions & 0 deletions
179
packages/e2e-test-utils-playwright/src/request-utils/menus.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { RequestUtils } from './index'; | ||
|
||
const MENUS_ENDPOINT = '/wp/v2/menus'; | ||
const MENU_ITEMS_ENDPOINT = '/wp/v2/menu-items'; | ||
|
||
export interface Menu { | ||
name: string; | ||
} | ||
|
||
export interface MenuItem { | ||
id: number; | ||
title: string; | ||
status: 'publish' | 'future' | 'draft' | 'pending' | 'private'; | ||
object: 'page'; | ||
menu_order: number; | ||
} | ||
|
||
export interface Post { | ||
id: number; | ||
content: string; | ||
status: 'publish' | 'future' | 'draft' | 'pending' | 'private'; | ||
title: { | ||
raw: string; | ||
rendered: string; | ||
}; | ||
} | ||
|
||
export interface ObjectRequests extends Post { | ||
method?: string; | ||
path: string; | ||
headers?: Record< string, string | string[] >; | ||
link: string; | ||
} | ||
|
||
const menuItemObjectRequests = { | ||
post: ( menuItem: MenuItem ) => ( { | ||
path: '/wp/v2/posts', | ||
method: 'POST', | ||
data: { | ||
title: menuItem.title, | ||
status: 'publish', | ||
}, | ||
} ), | ||
page: ( menuItem: MenuItem ) => ( { | ||
path: '/wp/v2/pages', | ||
method: 'POST', | ||
data: { | ||
title: menuItem.title, | ||
status: 'publish', | ||
}, | ||
} ), | ||
}; | ||
|
||
const menuItemObjectMatchers = { | ||
post: ( menuItem: MenuItem, post: Post ) => | ||
menuItem.title === post.title.raw, | ||
page: ( menuItem: MenuItem, page: Post ) => | ||
menuItem.title === page.title.raw, | ||
}; | ||
|
||
/** | ||
* Reset user preferences | ||
* | ||
*/ | ||
export async function deleteAllMenus( this: RequestUtils ) { | ||
const menus = await this.rest( { path: MENUS_ENDPOINT } ); | ||
|
||
if ( ! menus?.length ) return; | ||
|
||
await this.batchRest( | ||
menus.map( ( menu: MenuItem ) => ( { | ||
method: 'DELETE', | ||
path: `${ MENUS_ENDPOINT }/${ menu.id }?force=true`, | ||
} ) ) | ||
); | ||
} | ||
|
||
/** | ||
* Create menus and all linked resources for the menu using the REST API. | ||
* | ||
* @param {} this RequestUtils. | ||
* @param {Object} menu Rest payload for the menu | ||
* @param {?Array} menuItems Data for any menu items to be created. | ||
*/ | ||
export async function createMenu( | ||
this: RequestUtils, | ||
menu: Menu, | ||
menuItems: MenuItem[] | ||
) { | ||
// Step 1. Create the menu. | ||
const menuResponse = await this.rest( { | ||
method: 'POST', | ||
path: MENUS_ENDPOINT, | ||
data: menu, | ||
} ); | ||
|
||
if ( ! menuItems?.length ) { | ||
return; | ||
} | ||
|
||
// Step 2. Create all the pages/posts/categories etc. that menu items | ||
// are linked to. These items don't support rest batching so create them | ||
// using individual requests. | ||
const objectRequests = menuItems | ||
.map( ( menuItem: MenuItem ) => { | ||
const getRequest = menuItemObjectRequests[ menuItem.object ]; | ||
if ( ! getRequest ) { | ||
return undefined; | ||
} | ||
return getRequest( menuItem ); | ||
} ) | ||
.filter( ( request ) => !! request ); | ||
const objectResponses: ObjectRequests[] = []; | ||
for ( const objectRequest of objectRequests ) { | ||
if ( ! objectRequest ) continue; | ||
const objectResponse = await this.rest( objectRequest ); | ||
objectResponses.push( objectResponse ); | ||
} | ||
|
||
// Step 3. Create the initial menu items without assigned parents. We need | ||
// the ids of all the menu items first before being able to assign the | ||
// correct id of the parent. | ||
/*const menuItemsResponse =*/ await this.batchRest( | ||
menuItems.map( ( menuItem: MenuItem ) => { | ||
// If the menu item is linked to an 'object', get the id for that | ||
// object. | ||
const objectMatcher = menuItemObjectMatchers[ menuItem.object ]; | ||
let object; | ||
if ( objectMatcher ) { | ||
object = objectResponses.find( ( objectResponse ) => { | ||
return objectMatcher( menuItem, objectResponse ); | ||
} ); | ||
} | ||
|
||
return { | ||
method: 'POST', | ||
path: MENU_ITEMS_ENDPOINT, | ||
body: { | ||
menus: menuResponse.id, | ||
object_id: object?.id, | ||
url: object?.link, | ||
...menuItem, | ||
parent: undefined, | ||
}, | ||
}; | ||
} ) | ||
); | ||
|
||
// Step 4. Make another menu item request to assign parents. | ||
|
||
/*await this.batchRest( | ||
menuItems | ||
.map( ( menuItem: MenuItem, index: number ) => { | ||
// In the fixture data, the parent corresponds to the | ||
// index in the array, dereference that to find the actual | ||
// menu item id. | ||
const fixtureParentIndex = menuItem?.parent !== undefined; | ||
// Skip any menu items that are top level. | ||
if ( ! fixtureParentIndex ) { | ||
return undefined; | ||
} | ||
const parent = menuItemsResponse[ menuItem.parent ].body.id; | ||
const menuItemResponse = menuItemsResponse[ index ]; | ||
const menuItemId = menuItemResponse.body.id; | ||
return { | ||
method: 'PUT', | ||
path: `${ MENU_ITEMS_ENDPOINT }/${ menuItemId }`, | ||
body: { ...menuItemResponse.body, parent }, | ||
}; | ||
} ) | ||
.filter( ( request ) => !! request ) | ||
);*/ | ||
} |
84 changes: 84 additions & 0 deletions
84
test/e2e/specs/editor/blocks/fixtures/menu-items-request-fixture.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
module.exports = [ | ||
{ | ||
title: 'Home', | ||
url: 'http://localhost:8889/', | ||
menu_order: 1, | ||
}, | ||
{ | ||
title: 'About', | ||
type: 'post_type', | ||
object: 'page', | ||
menu_order: 2, | ||
}, | ||
{ | ||
title: 'Our team', | ||
type: 'post_type', | ||
object: 'page', | ||
menu_order: 3, | ||
parent: 1, | ||
}, | ||
{ | ||
title: 'Shop', | ||
type: 'post_type', | ||
object: 'page', | ||
menu_order: 4, | ||
}, | ||
{ | ||
title: 'Winter apparel', | ||
type: 'post_type', | ||
object: 'page', | ||
menu_order: 5, | ||
parent: 3, | ||
}, | ||
{ | ||
title: 'Chunky socks', | ||
type: 'post_type', | ||
object: 'page', | ||
menu_order: 6, | ||
parent: 4, | ||
}, | ||
{ | ||
title: 'Hideous hats', | ||
type: 'post_type', | ||
object: 'page', | ||
menu_order: 7, | ||
parent: 4, | ||
}, | ||
{ | ||
title: 'Glorious gloves', | ||
type: 'post_type', | ||
object: 'page', | ||
menu_order: 8, | ||
parent: 4, | ||
}, | ||
{ | ||
title: 'Jazzy Jumpers', | ||
type: 'post_type', | ||
object: 'page', | ||
menu_order: 9, | ||
parent: 4, | ||
}, | ||
{ | ||
title: 'Shipping', | ||
type: 'post_type', | ||
object: 'page', | ||
menu_order: 10, | ||
}, | ||
{ | ||
title: 'Contact Us', | ||
type: 'post_type', | ||
object: 'page', | ||
menu_order: 11, | ||
}, | ||
{ | ||
title: 'WordPress.org', | ||
url: 'https://wordpress.org', | ||
menu_order: 12, | ||
}, | ||
{ | ||
title: 'Google', | ||
url: 'https://google.com', | ||
menu_order: 13, | ||
parent: 11, | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters