-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tests: Use REST API to delete templates and template parts #38524
Changes from all commits
5dd33c0
cda30e3
8853cc6
fb50a27
fe4f44e
f2d3f4e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { rest, batch } from './rest-api'; | ||
|
||
const PATH_MAPPING = { | ||
wp_template: '/wp/v2/templates', | ||
wp_template_part: '/wp/v2/template-parts', | ||
}; | ||
|
||
/** | ||
* Delete all the templates of given type. | ||
* | ||
* @param {('wp_template'|'wp_template_part')} type - Template type to delete. | ||
*/ | ||
export async function deleteAllTemplates( type ) { | ||
const path = PATH_MAPPING[ type ]; | ||
|
||
if ( ! path ) { | ||
throw new Error( `Unsupported template type: ${ type }.` ); | ||
} | ||
|
||
const templates = await rest( { path } ); | ||
|
||
await batch( | ||
templates | ||
.filter( ( template ) => !! template.wp_id ) | ||
.map( ( template ) => ( { | ||
method: 'DELETE', | ||
path: `/wp/v2/posts/${ template.wp_id }?force=true`, | ||
} ) ) | ||
); | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,13 +8,13 @@ import { writeFileSync } from 'fs'; | |
* WordPress dependencies | ||
*/ | ||
import { | ||
trashAllPosts, | ||
activateTheme, | ||
canvas, | ||
createNewPost, | ||
visitSiteEditor, | ||
saveDraft, | ||
insertBlock, | ||
deleteAllTemplates, | ||
} from '@wordpress/e2e-test-utils'; | ||
|
||
/** | ||
|
@@ -32,13 +32,12 @@ jest.setTimeout( 1000000 ); | |
describe( 'Site Editor Performance', () => { | ||
beforeAll( async () => { | ||
await activateTheme( 'emptytheme' ); | ||
await trashAllPosts( 'wp_template' ); | ||
await trashAllPosts( 'wp_template', 'auto-draft' ); | ||
await trashAllPosts( 'wp_template_part' ); | ||
await deleteAllTemplates( 'wp_template' ); | ||
await deleteAllTemplates( 'wp_template_part' ); | ||
} ); | ||
afterAll( async () => { | ||
await trashAllPosts( 'wp_template' ); | ||
await trashAllPosts( 'wp_template_part' ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we switch trashAllPosts to REST API too? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can. I don't see a reason for visiting post list table before every test. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not saying we should do so, just replacing the implementation of the current util. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ICYMI, there's an ongoing project to migrate e2e tests to Playwright, which follows the best practices of using |
||
await deleteAllTemplates( 'wp_template' ); | ||
await deleteAllTemplates( 'wp_template_part' ); | ||
await activateTheme( 'twentytwentyone' ); | ||
} ); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be faster than using the
templates
endpoint.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not working properly in my tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting. I'll have a look tomorrow or Monday.
Are you testing this util separately, or e2e tests are failing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was debugging the template-part test in my PR and noticed that the template parts were not removed between runs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One limitation of this util (and in some parts of templates endpoint) is that it can only delete templates for the current theme.
But I will see if there are any other issues. If util isn't reliable, we can always revert to my original PR - #38486.