From 3cc9af1b7ac544f851cb035ce6dbf3c71339ba2b Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Mon, 2 Oct 2023 16:36:42 +1300 Subject: [PATCH] Including adding of category and retrieving pattern by category to the main flow (#54923) --- .../src/request-utils/index.ts | 3 ++ .../src/request-utils/patterns.ts | 31 +++++++++++++++++++ .../e2e/specs/editor/various/patterns.spec.js | 17 ++++++++-- 3 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 packages/e2e-test-utils-playwright/src/request-utils/patterns.ts diff --git a/packages/e2e-test-utils-playwright/src/request-utils/index.ts b/packages/e2e-test-utils-playwright/src/request-utils/index.ts index 99a11bf995679..1d604ce6d4d31 100644 --- a/packages/e2e-test-utils-playwright/src/request-utils/index.ts +++ b/packages/e2e-test-utils-playwright/src/request-utils/index.ts @@ -35,6 +35,7 @@ import { deleteAllPages, createPage } from './pages'; import { resetPreferences } from './preferences'; import { getSiteSettings, updateSiteSettings } from './site-settings'; import { deleteAllWidgets, addWidgetBlock } from './widgets'; +import { deleteAllPatternCategories } from './patterns'; interface StorageState { cookies: Cookie[]; @@ -198,6 +199,8 @@ class RequestUtils { /** @borrows getThemeGlobalStylesRevisions as this.getThemeGlobalStylesRevisions */ getThemeGlobalStylesRevisions: typeof getThemeGlobalStylesRevisions = getThemeGlobalStylesRevisions.bind( this ); + /** @borrows deleteAllPatternCategories as this.deleteAllPatternCategories */ + deleteAllPatternCategories = deleteAllPatternCategories.bind( this ); } export type { StorageState }; diff --git a/packages/e2e-test-utils-playwright/src/request-utils/patterns.ts b/packages/e2e-test-utils-playwright/src/request-utils/patterns.ts new file mode 100644 index 0000000000000..ea1e24c9c7aea --- /dev/null +++ b/packages/e2e-test-utils-playwright/src/request-utils/patterns.ts @@ -0,0 +1,31 @@ +/** + * Internal dependencies + */ +import type { RequestUtils } from './index'; + +/** + * Delete all pattern categories using REST API. + * + * @see https://developer.wordpress.org/rest-api/reference/categories/#list-categories + * @param this + */ +export async function deleteAllPatternCategories( this: RequestUtils ) { + // List all pattern categories. + // https://developer.wordpress.org/rest-api/reference/categories/#list-categories + const categories = await this.rest( { + path: '/wp/v2/wp_pattern_category', + params: { + per_page: 100, + }, + } ); + + // Delete pattern categories. + // https://developer.wordpress.org/rest-api/reference/categories/#delete-a-category + // "/wp/v2/category" does not yet supports batch requests. + await this.batchRest( + categories.map( ( category: { id: number } ) => ( { + method: 'DELETE', + path: `/wp/v2/wp_pattern_category/${ category.id }?force=true`, + } ) ) + ); +} diff --git a/test/e2e/specs/editor/various/patterns.spec.js b/test/e2e/specs/editor/various/patterns.spec.js index 7e75fdb0f6c69..62412f1a3790a 100644 --- a/test/e2e/specs/editor/various/patterns.spec.js +++ b/test/e2e/specs/editor/various/patterns.spec.js @@ -6,6 +6,7 @@ const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' ); test.describe( 'Unsynced pattern', () => { test.beforeAll( async ( { requestUtils } ) => { await requestUtils.deleteAllBlocks(); + await requestUtils.deleteAllPatternCategories(); } ); test.beforeEach( async ( { admin } ) => { @@ -14,6 +15,7 @@ test.describe( 'Unsynced pattern', () => { test.afterEach( async ( { requestUtils } ) => { await requestUtils.deleteAllBlocks(); + await requestUtils.deleteAllPatternCategories(); } ); test( 'create a new unsynced pattern via the block options menu', async ( { @@ -40,6 +42,10 @@ test.describe( 'Unsynced pattern', () => { await createPatternDialog .getByRole( 'textbox', { name: 'Name' } ) .fill( 'My unsynced pattern' ); + const newCategory = 'Contact details'; + await createPatternDialog + .getByRole( 'combobox', { name: 'Categories' } ) + .fill( newCategory ); await createPatternDialog .getByRole( 'checkbox', { name: 'Synced' } ) .setChecked( false ); @@ -59,10 +65,15 @@ test.describe( 'Unsynced pattern', () => { // a plain paragraph block. await page.getByLabel( 'Toggle block inserter' ).click(); await page - .getByRole( 'searchbox', { - name: 'Search for blocks and patterns', + .getByRole( 'tab', { + name: 'Patterns', } ) - .fill( 'My unsynced pattern' ); + .click(); + await page + .getByRole( 'button', { + name: newCategory, + } ) + .click(); await page.getByLabel( 'My unsynced pattern' ).click(); // Just compare the block name and content as the clientIDs will be different.