diff --git a/tests/cypress/integration/coming-soon-woo.cy.js b/tests/cypress/integration/coming-soon-woo.cy.js index 2f01664..531c4ea 100644 --- a/tests/cypress/integration/coming-soon-woo.cy.js +++ b/tests/cypress/integration/coming-soon-woo.cy.js @@ -1,22 +1,27 @@ // +import { wpLogin, wpCli } from '../wp-module-support/utils.cy'; + +describe( 'Coming Soon with WooCommerce', { testIsolation: true }, () => { + beforeEach( () => { + wpLogin(); -describe( 'Coming Soon with WooCommerce', function () { - before( () => { // Set coming soon option to true to start with - cy.exec( `npx wp-env run cli wp option update mm_coming_soon true` ); - cy.exec( `npx wp-env run cli wp option update nfd_coming_soon true` ); + wpCli( `option update mm_coming_soon true` ); + wpCli( `option update nfd_coming_soon true` ); // Activate WooCommerce - cy.exec( `npx wp-env run cli wp plugin install woocommerce --activate`, { + wpCli( `plugin install woocommerce --activate`, { timeout: 40000, log: true, } ); + + cy.visit( '/wp-admin/index.php' ); } ); after( () => { // Uninstall WooCommerce and extensions - cy.exec( - 'npx wp-env run cli wp plugin uninstall --all --deactivate --exclude=bluehost-wordpress-plugin,wp-plugin-hostgator,wp-plugin-crazy-domains,wp-plugin-web,wp-plugin-mojo', + wpCli( + 'plugin uninstall --all --deactivate --exclude=bluehost-wordpress-plugin,wp-plugin-hostgator,wp-plugin-crazy-domains,wp-plugin-web,wp-plugin-mojo,wpforms-lite', { timeout: 60000, log: true, @@ -24,7 +29,7 @@ describe( 'Coming Soon with WooCommerce', function () { ); } ); - it( 'Replace our admin bar site status badge with WooCommerce\'s when active', () => { + it( "Replace our admin bar site status badge with WooCommerce's when active", () => { // Visit settings page cy.visit( '/wp-admin/admin.php?page=' + @@ -33,16 +38,26 @@ describe( 'Coming Soon with WooCommerce', function () { ); // Our badge shouldn't be visible - cy.get( '#wp-toolbar #wp-admin-bar-nfd-site-visibility-badge' ) - .should( 'not.exist' ); + cy.get( '#wp-toolbar #wp-admin-bar-nfd-site-visibility-badge' ).should( + 'not.exist' + ); // WooCommerce badge should be visible - cy.get( '#wp-toolbar #wp-admin-bar-woocommerce-site-visibility-badge a.ab-item' ) + cy.get( + '#wp-toolbar #wp-admin-bar-woocommerce-site-visibility-badge a.ab-item' + ) .contains( 'a', 'Coming soon' ) .should( 'be.visible' ); - }); + } ); + + it( 'Our plugin settings should toggle WooCommerce admin bar badge', () => { + // Visit settings page + cy.visit( + '/wp-admin/admin.php?page=' + + Cypress.env( 'pluginId' ) + + '#/settings' + ); - it ( 'Our plugin settings should toggle WooCommerce admin bar badge', () => { // Deactivate coming soon - Launch Site cy.get( '[data-id="coming-soon-toggle"]' ).click(); cy.wait( 1000 ); @@ -53,19 +68,26 @@ describe( 'Coming Soon with WooCommerce', function () { .should( 'be.visible' ); // Re-enable coming soon mode - cy.get( '[data-id="coming-soon-toggle"]' ) - .click(); + cy.get( '[data-id="coming-soon-toggle"]' ).click(); cy.wait( 1000 ); // WooCommerce badge should now be coming soon - cy.get( '#wp-toolbar .woocommerce-site-status-badge-coming-soon a.ab-item' ) + cy.get( + '#wp-toolbar .woocommerce-site-status-badge-coming-soon a.ab-item' + ) .contains( 'a', 'Coming soon' ) .should( 'be.visible' ); - }); - + } ); + it( 'Hide our site preview notice when WooCommerce is active', () => { + // Visit settings page + cy.visit( + '/wp-admin/admin.php?page=' + + Cypress.env( 'pluginId' ) + + '#/settings' + ); + cy.visit( '/' ); - cy.get( '.nfd-site-preview-warning' ) - .should( 'not.exist' ); - }); + cy.get( '.nfd-site-preview-warning' ).should( 'not.exist' ); + } ); } ); diff --git a/tests/cypress/integration/coming-soon.cy.js b/tests/cypress/integration/coming-soon.cy.js index 84bf3bb..8a1cdb6 100644 --- a/tests/cypress/integration/coming-soon.cy.js +++ b/tests/cypress/integration/coming-soon.cy.js @@ -1,19 +1,22 @@ // +import { wpLogin, wpCli } from '../wp-module-support/utils.cy'; -describe( 'Coming Soon', function () { +describe( 'Coming Soon', { testIsolation: true }, () => { const appClass = '.' + Cypress.env( 'appId' ); - before( () => { + beforeEach( () => { + wpLogin(); // Set coming soon option to true to start with - cy.exec( `npx wp-env run cli wp option update mm_coming_soon true` ); - cy.exec( `npx wp-env run cli wp option update nfd_coming_soon true` ); + wpCli( `option update mm_coming_soon true` ); + wpCli( `option update nfd_coming_soon true` ); // Deactivate WooCommerce if it's active - cy.exec( `npx wp-env run cli wp plugin deactivate woocommerce`, { + wpCli( `plugin deactivate woocommerce`, { timeout: 40000, log: true, failOnNonZeroExit: false, } ); + cy.visit( '/wp-admin/index.php' ); } ); it( 'Coming Soon is active', () => { @@ -42,11 +45,11 @@ describe( 'Coming Soon', function () { cy.get( '[data-id="coming-soon-toggle"]' ) .should( 'have.attr', 'aria-checked' ) .and( 'include', 'true' ); - }); - it( 'Displays Coming Soon in Site Status Admin Toolbar', () => { // Admin bar contains label - cy.get( '#wp-toolbar #wp-admin-bar-nfd-site-visibility-badge a.ab-item' ) + cy.get( + '#wp-toolbar #wp-admin-bar-nfd-site-visibility-badge a.ab-item' + ) .contains( 'a', 'Coming soon' ) .should( 'be.visible' ); } ); @@ -71,13 +74,13 @@ describe( 'Coming Soon', function () { .contains( 'button', 'Launch' ) .should( 'exist' ); - } ); - - it( 'Coming Soon Admin bar links to setting', () => { - cy.get( '#wp-toolbar #wp-admin-bar-nfd-site-visibility-badge a.ab-item' ).click(); + // Coming Soon Admin bar links to setting + cy.get( + '#wp-toolbar #wp-admin-bar-nfd-site-visibility-badge a.ab-item' + ).click(); cy.location().should( ( loc ) => { - expect( loc.hash ).to.eq( '#/settings' ) - }); + expect( loc.hash ).to.eq( '#/settings' ); + } ); } ); it( 'Coming Soon Toggle Turns Coming Soon Off', () => { @@ -105,15 +108,8 @@ describe( 'Coming Soon', function () { cy.get( '.nfd-notifications' ) .contains( '.nfd-notification', 'Coming soon deactivated' ) .should( 'be.visible' ); - } ); - - it( 'Coming Soon Toggle Turns Coming Soon On', () => { - cy.visit( - '/wp-admin/admin.php?page=' + - Cypress.env( 'pluginId' ) + - '#/settings' - ); + // Coming Soon Toggle Turns Coming Soon Back On // Activate Coming Soon - Unlaunch Site cy.get( '[data-id="coming-soon-toggle"]' ).click(); cy.wait( 2000 ); @@ -156,9 +152,9 @@ describe( 'Coming Soon', function () { cy.get( '#wrap' ).contains( 'Coming Soon' ).should( 'exist' ); } ); - // this test is already in the ecommerce module, once the component is moved into this module this test can be used - it.skip( 'Launching launches site', () => { - cy.login( Cypress.env( 'wpUsername' ), Cypress.env( 'wpPassword' ) ); + // this test is already in the ecommerce module, and the code is in the ecommerce module + // once the component is moved into this module this test will be used and the ecom test removed + it( 'Launching launches site', () => { cy.visit( '/wp-admin/admin.php?page=' + Cypress.env( 'pluginId' ) + @@ -199,6 +195,5 @@ describe( 'Coming Soon', function () { cy.get( '[data-id="coming-soon-toggle"]' ) .should( 'have.attr', 'aria-checked' ) .and( 'include', 'false' ); - } ); } ); diff --git a/tests/cypress/wp-module-support/utils.cy.js b/tests/cypress/wp-module-support/utils.cy.js new file mode 100644 index 0000000..d940d5c --- /dev/null +++ b/tests/cypress/wp-module-support/utils.cy.js @@ -0,0 +1,29 @@ +/** + * Loginto WordPress. + */ +export const wpLogin = () => { + cy.login( Cypress.env( 'wpUsername' ), Cypress.env( 'wpPassword' ) ); +}; + +/** + * 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 + * @param {Object} paramArgs any args to pass to exec + */ +export const wpCli = ( cmd, paramArgs ) => { + const defaultArgs = { + env: { + NODE_TLS_REJECT_UNAUTHORIZED: '1', + }, + failOnNonZeroExit: true, + }; + const args = { ...defaultArgs, ...paramArgs }; + cy.exec( `npx wp-env run cli wp ${ cmd }`, args ).then( ( result ) => { + for ( const [ key, value ] of Object.entries( result ) ) { + cy.log( `${ key }: ${ value }` ); + } + } ); +};