-
Notifications
You must be signed in to change notification settings - Fork 8
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
Command/40 Added checkBlockPatternExists
command.
#62
Conversation
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.
@faisal-alvi thank you for the great tool. I left few comments to the code.
It also seems to me that command name and/or the work it does is not accurate. The command will insert the block pattern if it finds it, while from its name we expect just the check of existence.
In case we just want to check if block pattern exists, maybe it's better to finish the procedure with wrapping the result. It will allow to chain and tester will decide if they want to insert the pattern:
cy
.checkBlockPatternExists({title:'Title', categoryValue:'cat'})
.then((exists) => {
if(exists) {
// insert pattern
} else {
// alternative test workflow
}
});
Please refer to createTerm
command and it's tests to see how it works
cy.visit('/wp-admin/post-new.php'); | ||
|
||
// Close Welcome Guide. | ||
const welcomeGuide = |
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.
Could you please update this to use closeWelcomeGuide
command
} | ||
}); | ||
|
||
// Open inerter. |
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.
A typo here?
const inserterWPNew = '.edit-post-header-toolbar__inserter-toggle'; | ||
cy.get('body').then($body => { | ||
if ($body.find(inserterWPOld).length > 0) { | ||
cy.get(inserterWPOld).click({ force: true }); |
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.
Using {force: true}
brings a potentially false positive workflow to the test process. In case if the element is hidden, the command will click it anyway, while regular users could not do that.
While this behaviour is ok for testing end-products, but it's not welcomed within the library. @dinhtungdu, need your opinion on this please.
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.
While this behaviour is ok for testing end-products, but it's not welcomed within the library.
@cadic I agree. Using click
with force
set to true
will bypass actionability checks. In the other words, a hidden element can be clickable. It doesn't replicate human behavior so we should avoid that when possible.
I have the same thought. Besides, I think it's developer's responsibility to ensure the patterns exist before inserting. So we just need a command to simply insert a pattern to the page. If the pattern doesn't exist for any reason:
|
The cypress checks for the pattern but when we switch the category as shown in the below screenshot, the patterns render slowly (replaces Please share if there is a better solution. |
If the sole purpose of this command is to check whether a pattern exists or not, rather than going through the UI, can we use code instead? Something like this: cy.window().then( win => {
const { wp } = win;
const { getSettings } = wp.data.select( 'core/block-editor' );
const allRegisteredPatterns = getSettings().__experimentalBlockPatterns;
/**
* loop through all the patterns to check if pattern exists
*/
} ); |
The function can be reduced to: /* eslint-disable tsdoc/syntax */
/**
* Check Block Pattern Exists. Works only with WordPress \>=5.5
*
* \@param postData {
* `title` - Patttern name/title,
* `categoryValue` - Value of the pattern category,
* }
*
* @example
* For WP v5.5
* ```
* cy.checkBlockPatternExists({
* title: 'Two buttons',
* });
* ```
*
* @example
* For WP v5.9
* ```
* cy.checkBlockPatternExists({
* title: 'Three columns with offset images',
* categoryValue: 'gallery',
* });
* ```
*/
export const checkBlockPatternExists = ({
title,
categoryValue = 'featured',
}: {
title: string;
categoryValue?: string;
}): void => {
cy.visit('/wp-admin/post-new.php');
// Close Welcome Guide.
cy.closeWelcomeGuide();
cy.window().then( win => {
const { wp } = win;
const { getSettings } = wp.data.select( 'core/block-editor' );
const allRegisteredPatterns = getSettings().__experimentalBlockPatterns;
for ( let i = 0; i < allRegisteredPatterns.length; i++ ) {
if ( title === allRegisteredPatterns[i].title && allRegisteredPatterns[i].categories && allRegisteredPatterns[i].categories.includes(categoryValue) ) {
cy.wrap(true);
return;
}
}
cy.wrap(false);
});
}; |
@Sidsector9 this is what I was thinking about but as the Cypress is an E2E test, I thought to stick with the UI. I've checked the code and it works in old and new WP versions, however, while building the project, the following error is displayed: |
@faisal-alvi I haven't looked through the project entirely, but adding the following at the top of your file should work: declare global {
interface Window {
wp: any;
}
}
While you're correct to do so, one important thing to consider is that these utilities are not meant for testing Gutenberg itself. They're helpers to test "things" built using Gutenberg's features. Keeping this in mind, going the UI/non-UI way doesn't make much of a difference. @dinhtungdu, any thoughts on this? |
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.
@faisal-alvi Can you please check and fix the canceled workflows here?
I agree with Sid that this doesn't make much difference for this command even in the E2E context.
@dinhtungdu looks like all 3 workflows were cancelled by timeout after 6 hours of being stuck |
if (exists) { | ||
alert('The block patter exists!'); | ||
} else { | ||
alert('The block pattern does not exist!'); | ||
} |
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.
@faisal-alvi alert()
requires human interaction to close the alert which never happens in the CI environments. I believe this causes the tests to be stuck.
Awesome @dinhtungdu !! |
@faisal-alvi I've updated the test and checked the command against every WP core since 5.2 You can use ./run-all-cores.sh -s tests/cypress/integration/check-block-pattern-exists.test.js
const args = {
title: testCase.title,
};
if (compare(Cypress.env('WORDPRESS_CORE').toString(), '5.7', '>=')) {
args.categoryValue = testCase.cat;
}
In WP 5.5 and 5.6 Block patterns were in very early stage, I believe we could skip supporting this command for very old versions. But support of WP 6.0+ is important. Could it be related to __experimentalBlockPatterns? const allRegisteredPatterns = getSettings().__experimentalBlockPatterns; |
@cadic I fixed the tests for WP 6.0+. I think 6.0 takes time or maybe defers loading of block patterns. @faisal-alvi I've made a minor change. I have moved Another reason is that if we use |
@Sidsector9 makes sense. However, is it fine to use |
2be2101
to
a535b32
Compare
const shouldIt = testCase.expected ? 'should' : 'should not'; | ||
it(`Pattern "${testCase.title}" ${shouldIt} exist in category "${testCase.cat}"`, () => { | ||
// Wait for patterns to load on the post edit page. | ||
cy.wait(1000); |
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.
@Sidsector9 I wonder why do we need this? Tests run successfully without this wait on my local environment. Maybe there is GitHub-related issue?
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.
@cadic same here. Works well locally but fails on Actions. That is what fixed the issue with WP6.0
@cadic @Sidsector9 any further updates here? Eager to see this merged! |
Failed test is related to another command |
@faisal-alvi @cadic I have removed the wait command, can you check? |
@Sidsector9 @cadic I've tried to test this PR, the Cypress is throwing this error: |
@faisal-alvi that's expected to fail on local as we're only setting the environment variables when the actions are run.
|
& Merge branch 'trunk' into command/40
I have merged the |
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.
Thank you @faisal-alvi, this has been waiting for too long, finally LGTM 🔥
…62) * Command/40 Added `checkBlockPatternExists` command. * add examples in the documentaion * try to fix the lower version issue * using `wrap` to return & skipped pattern insertion * added 10s waiting as a workaround of slow redner by wp * suggestion implemented * declare global var * command/40 replacing the alerts with simple comments * Tests for checkBlockPatterExists * Remove debug output * Use correct values * Make test optional for old WordPress core * Fail-fast for matrix * replace semver with compare-versions for optional testing * Add 5.9 to versions list in run-all-cores * Threshold 5.7 * run-all-cores.sh * fix: WP 6.0 tests * try without wait * try checking with setInterval Co-authored-by: Faisal Alvi <[email protected]> Co-authored-by: Max Lyuchin <[email protected]> Co-authored-by: Siddharth Thevaril <[email protected]>
Description of the Change
Added a new command
checkBlockPatternExists
which checks whether a given pattern exists on the site or not.It accepts the following parameters.
title
: Pattern name/title.categoryValue
: Value of the pattern category. for example "twentytwentyone" for the Twenty Twenty-One category.Closes #40
Verification Process
npm run build
.npm run env:start
.npm run cypress:open
.check-block-pattern-exists.test.js
./tests/cypress/integration/check-block-pattern-exists.test.js
file and see the test results.Checklist:
Changelog Entry
Credits
Props @faisal-alvi