Skip to content

Commit

Permalink
cleanup plugins page test with intercept and helper to setCapability
Browse files Browse the repository at this point in the history
  • Loading branch information
circlecube committed Nov 8, 2024
1 parent 34238c9 commit 396133a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 30 deletions.
48 changes: 20 additions & 28 deletions tests/cypress/integration/solutions-plugins.cy.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// <reference types="Cypress" />
import { wpLogin, wpCli } from '../wp-module-support/utils.cy';
import { wpLogin, wpCli, setCapability } from '../wp-module-support/utils.cy';

// path to the entitlements json to load into transient
const entitlementsjson =
'vendor/newfold-labs/wp-module-solutions/tests/cypress/fixtures/entitlements.json';
const entitlementsFixture = require( '../fixtures/entitlements.json' );

describe( 'My Plugins and Tools in Plugin App', { testIsolation: true }, () => {
beforeEach( () => {
Expand All @@ -13,14 +11,11 @@ describe( 'My Plugins and Tools in Plugin App', { testIsolation: true }, () => {

after( () => {
wpCli( `transient delete nfd_site_capabilities` );
wpCli( `transient delete newfold_solutions` );
wpCli( `option delete nfd_data_token` );
} );

// check that it does not display when capabilities.hasSolution is false
it( 'My Plugins & Tools tab does not display without solution', () => {
wpCli( `transient delete nfd_site_capabilities` );
wpCli( `transient delete newfold_solutions` );

// need a cli command to set a capability before a test
cy.visit( '/wp-admin/plugin-install.php' );
Expand All @@ -34,28 +29,22 @@ describe( 'My Plugins and Tools in Plugin App', { testIsolation: true }, () => {
} );

it( 'My Plugins & Tools exists', () => {
// set transient expiration to one hour from now
const expiry = Math.floor( new Date().getTime() / 1000.0 ) + 3600;

// spoof hiive connection
wpCli( `option update nfd_data_token 'xyc123'` );
// Set hasSolution:true in capabilities
wpCli(
`option update _transient_nfd_site_capabilities '{"hasSolution": true}' --format=json`
);
// add test entitlements into transient
wpCli(
`option update _transient_newfold_solutions --format=json < ${ entitlementsjson }`
);
// manually set expiration for the transients
wpCli(
`option update _transient_timeout_nfd_site_capabilities ${ expiry }`
);
wpCli( `option update _transient_timeout_newfold_solutions ${ expiry }` );
setCapability( { hasSolution: true } );

cy.intercept(
{
method: 'GET',
url: /newfold-solutions(\/|%2F)v1(\/|%2F)entitlements/,
},
{
body: entitlementsFixture,
delay: 100,
}
).as( 'getEntitlements' );

// load plugin install page
cy.visit( '/wp-admin/plugin-install.php' );
cy.reload();

cy.window().then( ( win ) => {
cy.log(
Expand All @@ -66,11 +55,14 @@ describe( 'My Plugins and Tools in Plugin App', { testIsolation: true }, () => {
// check that my plugins and tools tab displays when capabilities.hasSolution is true
cy.get(
'#adminmenu a[href="plugin-install.php?tab=nfd_my_plugins_and_tools"]'
).should( 'be.visible' );
)
.should( 'be.visible' )
.click();

cy.wait( '@getEntitlements' );

// check that entitlement plugins load
cy.visit( '/wp-admin/plugin-install.php?tab=nfd_my_plugins_and_tools' );
cy.reload( true );
cy.url().should( 'contain', 'nfd_my_plugins_and_tools' );

cy.get( '.plugin-install-nfd_my_plugins_and_tools' ).should(
'be.visible'
Expand Down
34 changes: 32 additions & 2 deletions tests/cypress/wp-module-support/utils.cy.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
// login
/**
* Loginto WordPress.
*/
export const wpLogin = () => {
cy.login( Cypress.env( 'wpUsername' ), Cypress.env( 'wpPassword' ) );
};

// wp cli wrapper
/**
* wp-cli helper
*
* This wraps the command in the required npx wp-env run cli wp
*
* @param {string} cmd the command to send to wp-cli
*/
export const wpCli = ( cmd ) => {
cy.exec( `npx wp-env run cli wp ${ cmd }`, {
env: {
Expand All @@ -15,3 +23,25 @@ export const wpCli = ( cmd ) => {
}
} );
};

/**
* Set capability helper
*
* This calls performs a cli command to set a specific capability
*
* @param {*} capJSON json of capabilities
* @param {number} expiration seconds for transient to expire, defualt 3600 (1 hour)
*/
export const setCapability = ( capJSON, expiration = 3600 ) => {
wpCli(
`option update _transient_nfd_site_capabilities '${ JSON.stringify(
capJSON
) }' --format=json`
);
// set transient expiration to one hour (default) from now
const expiry = Math.floor( new Date().getTime() / 1000.0 ) + expiration;
// manually set expiration for the transients
wpCli(
`option update _transient_timeout_nfd_site_capabilities ${ expiry }`
);
};

0 comments on commit 396133a

Please sign in to comment.