Skip to content

Commit

Permalink
Add custom insert block command to fix an issue on WordPress trunk, u…
Browse files Browse the repository at this point in the history
…ntil we get our utils package updated
  • Loading branch information
dkotter committed Feb 12, 2025
1 parent 3847ad7 commit 6e40c54
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe( '[Language processing] Key Takeaways Tests', () => {
title: 'Test Key Takeaways post',
content: 'Test GPT content',
beforeSave: () => {
cy.insertBlock( 'classifai/key-takeaways' );
cy.customInsertBlock( 'classifai/key-takeaways' );
},
} ).then( () => {
cy.getBlockEditor()
Expand All @@ -44,91 +44,4 @@ describe( '[Language processing] Key Takeaways Tests', () => {
.should( 'contain.text', 'Request failed' );
} );
} );

it( 'Can disable feature', () => {
// Disable feature.
cy.visitFeatureSettings( 'language_processing/feature_key_takeaways' );
cy.disableFeature();
cy.saveFeatureSettings();

// Verify that the feature is not available.
cy.createPost( {
title: 'Test Key Takeaways post disabled',
content: 'Test GPT content',
beforeSave: () => {
cy.insertBlock( 'classifai/key-takeaways' );
},
} ).then( () => {
cy.getBlockEditor()
.find( '.wp-block-classifai-key-takeaways' )
.should( 'not.exist' );
} );
} );

it( 'Can disable feature by role', () => {
cy.visitFeatureSettings( 'language_processing/feature_key_takeaways' );
cy.enableFeature();
cy.saveFeatureSettings();

// Disable admin role.
cy.disableFeatureForRoles( 'feature_key_takeaways', [
'administrator',
] );

// Verify that the feature is not available.
cy.createPost( {
title: 'Test Key Takeaways post disabled user',
content: 'Test GPT content',
beforeSave: () => {
cy.insertBlock( 'classifai/key-takeaways' );
},
} ).then( () => {
cy.getBlockEditor()
.find( '.wp-block-classifai-key-takeaways' )
.should( 'not.exist' );
} );
} );

it( 'Can disable feature by user', () => {
// Disable admin role.
cy.disableFeatureForRoles( 'feature_key_takeaways', [
'administrator',
] );

cy.enableFeatureForUsers( 'feature_key_takeaways', [] );

// Verify that the feature is not available.
cy.createPost( {
title: 'Test Key Takeaways post disabled user',
content: 'Test GPT content',
beforeSave: () => {
cy.insertBlock( 'classifai/key-takeaways' );
},
} ).then( () => {
cy.getBlockEditor()
.find( '.wp-block-classifai-key-takeaways' )
.should( 'not.exist' );
} );
} );

it( 'User can opt-out of feature', () => {
// Enable user based opt-out.
cy.enableFeatureOptOut( 'feature_key_takeaways', 'azure_openai' );

// opt-out
cy.optOutFeature( 'feature_key_takeaways' );

// Verify that the feature is not available.
cy.createPost( {
title: 'Test Key Takeaways post disabled',
content: 'Test GPT content',
beforeSave: () => {
cy.insertBlock( 'classifai/key-takeaways' );
},
} ).then( () => {
cy.getBlockEditor()
.find( '.wp-block-classifai-key-takeaways' )
.should( 'not.exist' );
} );
} );
} );
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe( '[Language processing] Key Takeaways Tests', () => {
title: 'Test Key Takeaways post',
content: 'Test GPT content',
beforeSave: () => {
cy.insertBlock( 'classifai/key-takeaways' );
cy.customInsertBlock( 'classifai/key-takeaways' );
},
} ).then( () => {
cy.getBlockEditor()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe( '[Language processing] Key Takeaways Tests', () => {
title: 'Test Key Takeaways post',
content: 'Test GPT content',
beforeSave: () => {
cy.insertBlock( 'classifai/key-takeaways' );
cy.customInsertBlock( 'classifai/key-takeaways' );
},
} ).then( () => {
cy.getBlockEditor()
Expand Down Expand Up @@ -102,7 +102,7 @@ describe( '[Language processing] Key Takeaways Tests', () => {
title: 'Test Key Takeaways post disabled',
content: 'Test GPT content',
beforeSave: () => {
cy.insertBlock( 'classifai/key-takeaways' );
cy.customInsertBlock( 'classifai/key-takeaways' );
},
} ).then( () => {
cy.getBlockEditor()
Expand All @@ -126,7 +126,7 @@ describe( '[Language processing] Key Takeaways Tests', () => {
title: 'Test Key Takeaways post disabled user',
content: 'Test GPT content',
beforeSave: () => {
cy.insertBlock( 'classifai/key-takeaways' );
cy.customInsertBlock( 'classifai/key-takeaways' );
},
} ).then( () => {
cy.getBlockEditor()
Expand All @@ -148,7 +148,7 @@ describe( '[Language processing] Key Takeaways Tests', () => {
title: 'Test Key Takeaways post disabled user',
content: 'Test GPT content',
beforeSave: () => {
cy.insertBlock( 'classifai/key-takeaways' );
cy.customInsertBlock( 'classifai/key-takeaways' );
},
} ).then( () => {
cy.getBlockEditor()
Expand All @@ -169,7 +169,7 @@ describe( '[Language processing] Key Takeaways Tests', () => {
title: 'Test Key Takeaways post disabled',
content: 'Test GPT content',
beforeSave: () => {
cy.insertBlock( 'classifai/key-takeaways' );
cy.customInsertBlock( 'classifai/key-takeaways' );
},
} ).then( () => {
cy.getBlockEditor()
Expand Down
125 changes: 125 additions & 0 deletions tests/cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
// -- This will overwrite an existing command --
// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })
import { getNLUData } from '../plugins/functions';
import { getIframe } from '@10up/cypress-wp-utils/lib/functions/get-iframe';

const imageProcessingFeatures = [
'feature_descriptive_text_generator',
Expand Down Expand Up @@ -669,3 +670,127 @@ Cypress.Commands.add( 'allowFeatureToAdmin', () => {
cy.openUserPermissionsPanel();
cy.get( '.settings-allowed-roles input#administrator' ).check();
} );

/**
* Custom command to insert a block, until a new release of cypress-wp-utils.
*
* TODO: remove this command once cypress-wp-utils is updated.
*/
Cypress.Commands.add( 'customInsertBlock', ( type, name ) => {
const [ namespace = '', ...blockNameRest ] = type.split( '/' );
let blockNames = [
blockNameRest.join( '/' ).replace( '/', '-' ),
blockNameRest.join( '/' ).replace( '/', '\\/' ),
];

blockNames = blockNames.filter( ( x, i, a ) => a.indexOf( x ) === i );

let inserterBtn;
let search = '';

if ( typeof name === 'string' && name.length ) {
search = name;
} else {
search = type;
}

// Start of block inserter toggle button click logic.
cy.get( 'body' ).then( ( $body ) => {
const selectors = [
'button[aria-label="Add block"]', // 5.7
'button[aria-label="Toggle block inserter"]', // 6.4
'button[aria-label="Block Inserter"]', // 6.8
];

selectors.forEach( ( selector ) => {
if ( $body.find( selector ).length ) {
cy.get( selector ).then( ( $button ) => {
if ( $button.length ) {
inserterBtn = cy.wrap( $button );
inserterBtn.first().click();
}
} );
}
} );
} );
// End of block inserter toggle button click logic.

// Start of Block tab click logic.
cy.get( 'button[role="tab"]' )
.contains( 'Blocks' )
.then( ( $tab ) => {
if ( $tab.length ) {
cy.wrap( $tab ).click();
}
} );
// End of Block tab click logic.

// Start of Block search logic.
cy.get( 'input[placeholder="Search"]' ).then( ( $input ) => {
if ( $input.length ) {
cy.wrap( $input ).type( search );
}
} );
// End of Block search logic.

blockNames.forEach( ( blockName ) => {
const blockSelector = `.editor-block-list-item-${
'core' === namespace ? '' : namespace + '-'
}${ blockName }`;

cy.get( 'body' ).then( ( $body ) => {
if ( $body.find( blockSelector ).length ) {
// Start of Block insertion by click logic.
cy.get( blockSelector ).then( ( $block ) => {
if ( $block.length ) {
cy.wrap( $block ).click();
inserterBtn.click();

const [ ns, rest ] = type.split( '/' ); // namespace = ns, second namespace or block name = rest

cy.get( 'body' ).then( ( $innerBody ) => {
if (
$innerBody.find(
'iframe[name="editor-canvas"]'
).length
) {
// Works with WP 6.4
getIframe(
'iframe[name="editor-canvas"]'
).then( ( $iframe ) => {
const blockInIframe = $iframe.find(
`.wp-block[data-type="${ ns }/${ rest }"]`
);
if ( blockInIframe.length > 0 ) {
cy.wrap(
blockInIframe.last().prop( 'id' )
);
}
} );
} else if (
$innerBody.find(
`.wp-block[data-type="${ ns }/${ rest }"]`
).length
) {
// Works with WP 5.7
cy.get(
`.wp-block[data-type="${ ns }/${ rest }"]`
).then( ( $blockInEditor ) => {
expect( $blockInEditor.length ).to.equal(
1
);
cy.wrap( $blockInEditor.prop( 'id' ) );
} );
} else {
throw new Error(
`${ ns }/${ rest } not found.`
);
}
} );
}
} );
// End of Block insertion by click logic.
}
} );
} );
} );

0 comments on commit 6e40c54

Please sign in to comment.