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 b125005f86f1c8..364f5369564ac2 100644 --- a/packages/e2e-test-utils-playwright/src/request-utils/index.ts +++ b/packages/e2e-test-utils-playwright/src/request-utils/index.ts @@ -19,7 +19,7 @@ import { getPluginsMap, activatePlugin, deactivatePlugin } from './plugins'; import { deleteAllTemplates, createTemplate, - updateTemplate, + updateOrCreateTemplate, } from './templates'; import { activateTheme, @@ -179,8 +179,9 @@ class RequestUtils { deleteAllTemplates.bind( this ); /** @borrows createTemplate as this.createTemplate */ createTemplate: typeof createTemplate = createTemplate.bind( this ); - /** @borrows updateTemplate as this.updateTemplate */ - updateTemplate: typeof updateTemplate = updateTemplate.bind( this ); + /** @borrows updateOrCreateTemplate as this.updateOrCreateTemplate */ + updateOrCreateTemplate: typeof updateOrCreateTemplate = + updateOrCreateTemplate.bind( this ); /** @borrows resetPreferences as this.resetPreferences */ resetPreferences: typeof resetPreferences = resetPreferences.bind( this ); /** @borrows listMedia as this.listMedia */ diff --git a/packages/e2e-test-utils-playwright/src/request-utils/templates.ts b/packages/e2e-test-utils-playwright/src/request-utils/templates.ts index bb7467cce87a3f..6be65978ae48cd 100644 --- a/packages/e2e-test-utils-playwright/src/request-utils/templates.ts +++ b/packages/e2e-test-utils-playwright/src/request-utils/templates.ts @@ -89,7 +89,7 @@ async function createTemplate( * @param type Template type. * @param payload Template attributes. */ -async function updateTemplate( +async function updateOrCreateTemplate( this: RequestUtils, type: TemplateType, payload: CreateTemplatePayload @@ -104,8 +104,9 @@ async function updateTemplate( const template = templates.find( ( t ) => t.slug === payload.slug ); + // If the template is not found, create it. if ( ! template ) { - throw new Error( `Template with slug "${ payload.slug }" not found.` ); + return createTemplate.bind( this )( type, payload ); } const updatedTemplate = await this.rest< Template >( { @@ -117,4 +118,4 @@ async function updateTemplate( return updatedTemplate; } -export { deleteAllTemplates, createTemplate, updateTemplate }; +export { deleteAllTemplates, createTemplate, updateOrCreateTemplate }; diff --git a/test/e2e/specs/interactivity/instant-search.spec.ts b/test/e2e/specs/interactivity/instant-search.spec.ts index a50c3df42a65d3..b8bdeb8f0163b8 100644 --- a/test/e2e/specs/interactivity/instant-search.spec.ts +++ b/test/e2e/specs/interactivity/instant-search.spec.ts @@ -556,7 +556,7 @@ test.describe( 'Instant Search', () => { test.beforeAll( async ( { requestUtils } ) => { // Edit the Home template instead of creating a new page - await requestUtils.updateTemplate( 'wp_template', { + await requestUtils.updateOrCreateTemplate( 'wp_template', { slug: 'home', content: ` @@ -727,7 +727,7 @@ test.describe( 'Instant Search', () => { page, requestUtils, } ) => { - await requestUtils.updateTemplate( 'wp_template', { + await requestUtils.updateOrCreateTemplate( 'wp_template', { slug: 'home', content: ` @@ -807,7 +807,7 @@ test.describe( 'Instant Search', () => { requestUtils, } ) => { // Set up: Add one inherited and one custom query to the home template - await requestUtils.updateTemplate( 'wp_template', { + await requestUtils.updateOrCreateTemplate( 'wp_template', { slug: 'home', content: `