Skip to content

Commit

Permalink
moved utils to utils package
Browse files Browse the repository at this point in the history
  • Loading branch information
MaggieCabrera committed Feb 8, 2023
1 parent 3e3d551 commit 6f78851
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 56 deletions.
3 changes: 3 additions & 0 deletions packages/e2e-test-utils-playwright/src/request-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { activateTheme } from './themes';
import { deleteAllBlocks } from './blocks';
import { createComment, deleteAllComments } from './comments';
import { createPost, deleteAllPosts } from './posts';
import { createNavigationMenu, deleteAllNavigationMenus } from './menus';
import { resetPreferences } from './preferences';
import { getSiteSettings, updateSiteSettings } from './site-settings';
import { deleteAllWidgets, addWidgetBlock } from './widgets';
Expand Down Expand Up @@ -125,6 +126,8 @@ class RequestUtils {
deleteAllBlocks = deleteAllBlocks;
createPost = createPost.bind( this );
deleteAllPosts = deleteAllPosts.bind( this );
createNavigationMenu = createNavigationMenu.bind( this );
deleteAllNavigationMenus = deleteAllNavigationMenus.bind( this );
createComment = createComment.bind( this );
deleteAllComments = deleteAllComments.bind( this );
deleteAllWidgets = deleteAllWidgets.bind( this );
Expand Down
53 changes: 53 additions & 0 deletions packages/e2e-test-utils-playwright/src/request-utils/menus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Internal dependencies
*/
import type { RequestUtils } from './index';

export interface MenuData {
title: string;
content: string;
}
export interface Navigation {
id: number;
content: string;
status: 'publish' | 'future' | 'draft' | 'pending' | 'private';
}

/**
* Create a navigation menu
*
* @param {Object} 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 menus
*
*/
export async function deleteAllNavigationMenus( this: RequestUtils ) {
const menus = await this.rest< Navigation[] >( {
path: `/wp/v2/navigation/`,
} );

if ( ! menus?.length ) return;

await this.batchRest(
menus.map( ( menu ) => ( {
method: 'DELETE',
path: `/wp/v2/navigation/${ menu.id }?force=true`,
} ) )
);
}
62 changes: 6 additions & 56 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 ( { page, requestUtils }, use ) => {
await use( new NavigationBlockUtils( { page, requestUtils } ) );
},
} );

test.describe(
'As a user I want the navigation block to fallback to the best possible default',
() => {
Expand All @@ -17,16 +11,16 @@ test.describe(
await requestUtils.activateTheme( 'twentytwentythree' );
} );

test.beforeEach( async ( { admin, navBlockUtils } ) => {
test.beforeEach( async ( { admin, requestUtils } ) => {
await Promise.all( [
navBlockUtils.deleteAllNavigationMenus(),
requestUtils.deleteAllNavigationMenus(),
admin.createNewPost(),
] );
} );

test.afterAll( async ( { requestUtils, navBlockUtils } ) => {
test.afterAll( async ( { requestUtils } ) => {
await Promise.all( [
navBlockUtils.deleteAllNavigationMenus(),
requestUtils.deleteAllNavigationMenus(),
requestUtils.activateTheme( 'twentytwentyone' ),
] );
} );
Expand Down Expand Up @@ -65,9 +59,9 @@ test.describe(
test( 'default to my only existing menu', async ( {
editor,
page,
navBlockUtils,
requestUtils,
} ) => {
const createdMenu = await navBlockUtils.createNavigationMenu( {
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} /-->',
Expand Down Expand Up @@ -102,47 +96,3 @@ test.describe(
} );
}
);

class NavigationBlockUtils {
constructor( { editor, page, requestUtils } ) {
this.editor = editor;
this.page = page;
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,
},
} );
}

/**
* 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`,
} ) )
);
}
}

0 comments on commit 6f78851

Please sign in to comment.