Skip to content

Commit

Permalink
Add a new request utils function to update or create a template.
Browse files Browse the repository at this point in the history
We need this in case the `updateTemplate` function throws an error if the template is not found.
  • Loading branch information
michalczaplinski committed Dec 13, 2024
1 parent f42dc0a commit 4b3b2b3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
7 changes: 4 additions & 3 deletions packages/e2e-test-utils-playwright/src/request-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { getPluginsMap, activatePlugin, deactivatePlugin } from './plugins';
import {
deleteAllTemplates,
createTemplate,
updateTemplate,
updateOrCreateTemplate,
} from './templates';
import {
activateTheme,
Expand Down Expand Up @@ -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 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 >( {
Expand All @@ -117,4 +118,4 @@ async function updateTemplate(
return updatedTemplate;
}

export { deleteAllTemplates, createTemplate, updateTemplate };
export { deleteAllTemplates, createTemplate, updateOrCreateTemplate };
6 changes: 3 additions & 3 deletions test/e2e/specs/interactivity/instant-search.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: `
<!-- wp:query {"enhancedPagination":true,"queryId":1111,"query":{"inherit":true,"perPage":2,"order":"desc","orderBy":"date"}} -->
Expand Down Expand Up @@ -727,7 +727,7 @@ test.describe( 'Instant Search', () => {
page,
requestUtils,
} ) => {
await requestUtils.updateTemplate( 'wp_template', {
await requestUtils.updateOrCreateTemplate( 'wp_template', {
slug: 'home',
content: `
<!-- wp:query {"enhancedPagination":true,"queryId":1111,"query":{"inherit":true,"perPage":2,"order":"desc","orderBy":"date"}} -->
Expand Down Expand Up @@ -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: `
<!-- wp:query {"enhancedPagination":true,"queryId":1111,"query":{"inherit":true,"perPage":2,"order":"desc","orderBy":"date"}} -->
Expand Down

0 comments on commit 4b3b2b3

Please sign in to comment.