Skip to content

Commit

Permalink
Finish js-submission tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxwellGarceau committed Jan 10, 2025
1 parent 7177f13 commit d8bfe80
Showing 1 changed file with 44 additions and 53 deletions.
97 changes: 44 additions & 53 deletions tests/cypress/e2e/submission/js-submission.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* eslint-disable no-undef */
import { generateRandomEmail } from '../../support/functions/utility';

describe('JavaScript submission', () => {
let blockPostPostURL;
let mergeFields;
Expand All @@ -15,48 +17,47 @@ describe('JavaScript submission', () => {
blockPostPostURL = urls.blockPostPostURL;
});

// Load the post URLs from the JSON file
cy.fixture('mergeFields').then((fields) => {
mergeFields = Object.values(fields); // Extract field selectors as an array
});

cy.login(); // WP
cy.mailchimpLoginIfNotAlreadyLoggedIn();

cy.selectList('10up'); // Ensure list is selected, refreshes Mailchimp data with WP

// Set JS support to enabled
cy.setJavaScriptOption(true);

// Disable double opt-in
cy.setDoubleOptInOption(false);

// Disable all merge fields
cy.toggleMergeFields('uncheck');
});

beforeEach(() => {
cy.visit(blockPostPostURL);
setUpForm();
});

function setUpForm() {
cy.visit(blockPostPostURL);

function setUpForm() {
// Step 1: Assert form contains setup elements
// Email
cy.get('#mc_signup').should('exist');
cy.get('#mc_mv_EMAIL').should('exist');
cy.get('#mc_signup_submit').should('exist');

// // Other merge fields (loaded from fixture)
// mergeFields.forEach((field) => {
// cy.get(field.selector).should('exist');
// cy.get(field.selector).type(field.value);
// });
}

it('Disables the submit button before attempting submission', () => {
// Step 1: Visit the form page
cy.visit(blockPostPostURL);
function submitEmail(email) {
// Step 2: Fill in the required fields (email and other merge fields)
cy.get('#mc_mv_EMAIL').type(email);

// Step 2: Assert the submit button exists and is enabled initially
// Step 3: Assert that the submit button is enabled and exists
cy.get('#mc_signup_submit').should('exist').and('be.enabled');

// Step 3: Submit the form
// Step 4: Submit the form
cy.get('#mc_signup_submit').click();
}

it('Disables the submit button before attempting submission', () => {
submitEmail('invalidemail@--'); // Submit blank email

// Step 4: Assert that the submit button is disabled after submitting the form
cy.get('#mc_signup_submit').should('be.disabled');
Expand All @@ -66,47 +67,31 @@ describe('JavaScript submission', () => {
});

it('Perform post submit actions after successful submission', () => {
const email = cy.generateRandomEmail('javascript-submission');
const email = generateRandomEmail('javascript-submission-post-submit');
submitEmail(email);

// Step 1: Visit the form page
cy.visit(blockPostPostURL);

// Step 2: Fill in the required fields (email and other merge fields)
cy.get('#mc_mv_EMAIL').type(email);

// Fill other merge fields if necessary
mergeFields.forEach((field) => {
cy.get(field.selector).type(field.value);
});

// Step 3: Assert that the submit button is enabled and exists
cy.get('#mc_signup_submit').should('exist').and('be.enabled');

// Step 4: Submit the form
cy.get('#mc_signup_submit').click();

// Step 5: Assert that the success message is displayed
cy.get('.mc_success_msg').should('exist').and('contain.text', 'success');
cy.get('.mc_success_msg').should('exist').contains('success', { matchCase: false });

// Step 6: Verify that the form fields are cleared
cy.get('#mc_mv_EMAIL').should('have.value', '');
mergeFields.forEach((field) => {
cy.get(field.selector).should('have.value', '');
});


// Step 7: Verify that the submit button is re-enabled
cy.get('#mc_signup_submit').should('be.enabled');


cy.wait(1000);

// Step 8: Assert that the form scrolled to the top
cy.window().then((win) => {
const scrollTop = win.pageYOffset || win.document.documentElement.scrollTop;
expect(scrollTop).to.eq(0);
expect(scrollTop).to.be.lessThan(500); // Doesn't scroll all the way to the top
});

// Step 9: Cleanup and delete contact
cy.deleteContactFrom10UpList(email);
});

// TODO: This is a bug and is currently broken
it.skip('Persist form data on Mailchimp API validation failure', () => {

// Confirm that we received an error
Expand All @@ -115,13 +100,19 @@ describe('JavaScript submission', () => {
cy.get('.mc_error_msg').contains('Email Address:');
});

// TODO: BUG: Single opt-in is currently broken, but a fix is scheduled for 1.7.0
it.skip('Success submission with JS support adds email to Mailchimp account as contact', () => {
// // TODO: This is failing because we need to confirm the test email address subscription
// // TODO: We will also have to delete the contact before each form submission via the Mailchimp API
// Step 6: Verify that the form was submitted successfully
// cy.submitFormAndVerifyWPSuccess();

// // Step 7: Verify that the contact was added to the Mailchimp account via the Mailchimp API
// cy.verifyContactAddedToMailchimp(email, '10up');
})
const email = generateRandomEmail('javascript-submission-verify-submission');
submitEmail(email);

// Step 5: Assert Mailchimp WP Success
cy.get('.mc_success_msg').should('exist').contains('success', { matchCase: false });

// Step 6: Verify that the contact was added to the Mailchimp account via the Mailchimp API
cy.wait(5000)
.verifyContactAddedToMailchimp(email, '10up');

// Step 7: Cleanup and delete contact
cy.deleteContactFrom10UpList(email);
});
});

0 comments on commit d8bfe80

Please sign in to comment.