Skip to content

Commit

Permalink
update tests with flexible selectors and add testIsolation
Browse files Browse the repository at this point in the history
  • Loading branch information
circlecube committed Dec 12, 2024
1 parent 4242ab2 commit 57617eb
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 110 deletions.
216 changes: 106 additions & 110 deletions tests/cypress/integration/deactivation-survey.cy.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,11 @@
// <reference types="Cypress" />
import { wpLogin } from '../wp-module-support/utils.cy';

describe( 'Plugin Deactivation Survey', () => {
describe( 'Plugin Deactivation Survey', { testIsolation: true }, () => {
let newfoldDeactivationSurvey;

before( () => {
cy.visit( '/wp-admin/plugins.php' );

cy.window()
.its( 'newfoldDeactivationSurvey' )
.then( ( data ) => {
newfoldDeactivationSurvey = data;
} );
// ignore notifications errors if there are any
cy.intercept(
{
method: 'GET',
url: /newfold-notifications/,
},
{ body: {} }
);

// define commands for repetitive commands
Cypress.Commands.add( 'triggerDeactivationModal', () => {
cy.get(
'.deactivate a[id*="' + Cypress.env( 'pluginId' ) + '"]'
Expand All @@ -42,16 +28,61 @@ describe( 'Plugin Deactivation Survey', () => {
).should( 'exist' );
} );

Cypress.Commands.add( 'reactivatePlugin', () => {
Cypress.Commands.add( 'activatePlugin', () => {
cy.get('body')
.then(($body) => {
// check if activate link is present
if ( $body.find('.activate a[id*="' + Cypress.env( 'pluginId' ) + '"]').length ) {
cy.get(
'.activate a[id*="' + Cypress.env( 'pluginId' ) + '"]'
).click();
}
// else assume it was textarea
return false;
})
cy.wait( 500 );
} );

Cypress.Commands.add( 'verifyPluginActive', () => {
cy.get(
'.deactivate a[id*="' + Cypress.env( 'pluginId' ) + '"]'
).should( 'exist' );
cy.get(
'.activate a[id*="' + Cypress.env( 'pluginId' ) + '"]'
).click();
cy.wait( 500 );
).should( 'not.exist' );
} );

});

beforeEach( () => {
wpLogin();
cy.visit( '/wp-admin/plugins.php', {
onLoad() {
cy.window().then( ( win ) => {
// load module data into var
newfoldDeactivationSurvey = win.newfoldDeactivationSurvey;
} );
},
} );

// ignore notifications errors if there are any
cy.intercept(
{
method: 'GET',
url: /newfold-notifications/,
},
{ body: {} }
);

cy.intercept( {
method: 'POST',
url: /newfold-data(\/|%2F)v1(\/|%2F)events/,
} ).as( 'surveyEvent' );

} );

it( 'Plugin deactivate link opens modal', () => {
// click "Deactivate" link from plugins list
it( 'Modal opens and closes properly and Skip action works', () => {
// On overlay click
cy.triggerDeactivationModal();

// body has no scroll class
Expand All @@ -64,9 +95,44 @@ describe( 'Plugin Deactivation Survey', () => {

// overlay exists
cy.get( '.nfd-deactivation-survey__overlay' ).should( 'exist' );
} );

it( 'Step 1 content renders correctly', () => {
cy.get( '.nfd-deactivation-survey__overlay' ).click( { force: true } );
cy.verifyModalClosed();

// On step 1 cancel button click
cy.triggerDeactivationModal();
cy.get(
'.nfd-deactivation-sure button[nfd-deactivation-survey-destroy]'
).click();
cy.verifyModalClosed();

// On ESC key press (step 1)
cy.triggerDeactivationModal();
cy.get( 'body' ).type( '{esc}' );
cy.verifyModalClosed();

// On step 2 cancel button click
cy.triggerDeactivationModal();
cy.get(
'.nfd-deactivation-sure button[nfd-deactivation-survey-next]'
).click();
cy.get(
'.nfd-deactivation-survey button[nfd-deactivation-survey-destroy]'
).click();
cy.verifyModalClosed();

// On ESC key press (step 2)
cy.triggerDeactivationModal();
cy.get(
'.nfd-deactivation-sure button[nfd-deactivation-survey-next]'
).click();
cy.get( 'body' ).type( '{esc}' );
cy.verifyModalClosed();

// Step 1 content renders correctly
// click "Deactivate" link from plugins list
cy.triggerDeactivationModal();

cy.get(
'.nfd-deactivation-sure .nfd-deactivation__header-title'
).should( 'be.visible' );
Expand All @@ -91,13 +157,10 @@ describe( 'Plugin Deactivation Survey', () => {
'.nfd-deactivation-sure button[nfd-deactivation-survey-next]'
).should( 'be.visible' );

// Go to next step
cy.get(
'.nfd-deactivation-sure button[nfd-deactivation-survey-next]'
).click();
} );

it( 'Step 2 content renders correctly', () => {
// Step 2 content renders correctly
// Go to next step
cy.get( 'button[nfd-deactivation-survey-next]' ).click();

cy.get(
'.nfd-deactivation-survey .nfd-deactivation__header-title'
).should( 'be.visible' );
Expand Down Expand Up @@ -130,51 +193,25 @@ describe( 'Plugin Deactivation Survey', () => {
cy.get(
'.nfd-deactivation-survey button[nfd-deactivation-survey-skip]'
).should( 'be.visible' );
} );

it( 'Skip action works', () => {
// ignore notifications errors if there are any
cy.intercept(
{
method: 'GET',
url: /newfold-notifications/,
},
{ body: {} }
);
cy.intercept( {
method: 'POST',
url: /newfold-data(\/|%2F)v1(\/|%2F)events/,
} ).as( 'surveyEvent' );


// Skip action works
// skip & deactivate functions
cy.get( 'button[nfd-deactivation-survey-skip]' ).should( 'be.visible' );
cy.get( 'button[nfd-deactivation-survey-skip]' ).click();
cy.wait( '@surveyEvent' )
// .its('request.body.action').should('eq', 'deactivation_survey_freeform')
.its( 'request.body.data.survey_input' )
.should( 'eq', '(Skipped)' );
// verify modal closed
cy.verifyModalClosed();
// verify plugin is deactivated
cy.verifyPluginDeactivated();
// reactivate plugin
cy.reactivatePlugin();
// activate plugin
cy.activatePlugin();
// verify plugin is activated
cy.verifyPluginActive();
} );

it( 'Submit action works', () => {
// ignore notifications errors if there are any
cy.intercept(
{
method: 'GET',
url: /newfold-notifications/,
},
{ body: {} }
);
cy.intercept( {
method: 'POST',
url: /newfold-data(\/|%2F)v1(\/|%2F)events/,
} ).as( 'surveyEvent' );

it( 'Modal Submit survey action works', () => {
// click link to open modal
cy.triggerDeactivationModal();

Expand All @@ -190,56 +227,15 @@ describe( 'Plugin Deactivation Survey', () => {
);
cy.get( 'input[nfd-deactivation-survey-submit]' ).click();
cy.wait( '@surveyEvent' )
// .its('request.body.action').should('eq', 'deactivation_survey_freeform')
.its( 'request.body.data.survey_input' )
.should( 'eq', ugcReason );
cy.wait( 500 );
// verify plugin is deactivated
cy.verifyPluginDeactivated();
// reactivate plugin
cy.reactivatePlugin();
} );

it( 'Modal closes properly', () => {
// On overlay click
cy.triggerDeactivationModal();
cy.get( '.nfd-deactivation-survey__overlay' ).click( { force: true } );
cy.verifyModalClosed();

// On step 1 cancel button click
cy.triggerDeactivationModal();
cy.get(
'.nfd-deactivation-sure button[nfd-deactivation-survey-destroy]'
).click();
cy.verifyModalClosed();

// On ESC key press (step 1)
cy.triggerDeactivationModal();
cy.get( 'body' ).type( '{esc}' );
cy.verifyModalClosed();

// On step 2 cancel button click
cy.triggerDeactivationModal();
cy.get(
'.nfd-deactivation-sure button[nfd-deactivation-survey-next]'
).click();
cy.get(
'.nfd-deactivation-survey button[nfd-deactivation-survey-destroy]'
).click();
cy.verifyModalClosed();

// On ESC key press (step 2)
cy.triggerDeactivationModal();
cy.get(
'.nfd-deactivation-sure button[nfd-deactivation-survey-next]'
).click();
cy.get( 'body' ).type( '{esc}' );
cy.verifyModalClosed();
// activate plugin
cy.activatePlugin();
// verify plugin is activated
cy.verifyPluginActive();
} );
} );

after( () => {
cy.exec(
`npx wp-env run cli wp plugin activate ${ Cypress.env( 'pluginSlug' ) }`
);
} );
} );
6 changes: 6 additions & 0 deletions tests/cypress/wp-module-support/utils.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Loginto WordPress.
*/
export const wpLogin = () => {
cy.login( Cypress.env( 'wpUsername' ), Cypress.env( 'wpPassword' ) );
};

0 comments on commit 57617eb

Please sign in to comment.