Skip to content

Commit 6e0a32f

Browse files
committed
Added 2 new tests.
1 parent e993e48 commit 6e0a32f

File tree

5 files changed

+104
-56
lines changed

5 files changed

+104
-56
lines changed

tests/cypress/e2e/settings/list.test.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe('Mailchimp lists ', () => {
1919
});
2020
});
2121

22-
it('All lists from user\'s account populate the WP admin dropdown list', () => {
22+
it("All lists from user's account populate the WP admin dropdown list", () => {
2323
cy.visit('/wp-admin/admin.php?page=mailchimp_sf_options');
2424

2525
const $wpLists = cy.get('#mc_list_id > option[value]:not([value=""])'); // Lists from the WP admin dropdown
@@ -42,11 +42,20 @@ describe('Mailchimp lists ', () => {
4242
cy.selectList('10up');
4343
cy.get('#mc-message .success_msg b').contains('Success!');
4444

45-
// Verify that the settings are visible if a list is saved
46-
cy.get('input[value="Update Subscribe Form Settings"]').should('exist');
45+
// Verify that the settings are visible if a list is saved
46+
cy.get('input[value="Update Subscribe Form Settings"]').should('exist');
4747
});
4848

49-
// This test has been decided to be skipped and marked as a "doing it wrong" site owner scenario
50-
// We are not worried about this testing scenario
51-
it.skip('Admin that has never saved a list can not see the form on the front end', () => {});
49+
it('Admin that has never saved a list can not see the form on the front end', () => {
50+
cy.wpCli('wp option delete mc_list_id').then(() => {
51+
cy.visit(shortcodePostURL);
52+
cy.get('#mc_signup_form').should('not.exist');
53+
54+
cy.visit(blockPostPostURL);
55+
cy.get('#mc_signup_form').should('not.exist');
56+
57+
cy.visit('/wp-admin/admin.php?page=mailchimp_sf_options');
58+
cy.selectList('10up');
59+
});
60+
});
5261
});

tests/cypress/e2e/settings/settings.test.js

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,14 @@ describe('Admin can update plugin settings', () => {
66
before(() => {
77
// Load the post URLs from the JSON file
88
cy.fixture('postUrls').then((urls) => {
9-
shortcodePostURL = urls.shortcodePostURL;
10-
blockPostPostURL = urls.blockPostPostURL;
9+
({ shortcodePostURL, blockPostPostURL } = urls);
1110
});
1211

1312
cy.login(); // WP
1413
cy.mailchimpLoginIfNotAlreadyLoggedIn();
1514
cy.selectList('10up'); // Ensure a list is selected
1615
});
1716

18-
// TODO: Default settings are populated as expected
19-
it.skip('The default settings populate as expected', () => {
20-
// Test here...
21-
});
22-
2317
it('Admin can set content options for signup form', () => {
2418
cy.visit('/wp-admin/admin.php?page=mailchimp_sf_options');
2519

@@ -251,4 +245,65 @@ describe('Admin can update plugin settings', () => {
251245
// Step 5: Ensure the original settings persist
252246
cy.get('#mc_header_content').should('have.value', customHeader);
253247
});
248+
249+
it('The default settings populate as expected', () => {
250+
const options = [
251+
'mc_header_content',
252+
'mc_subheader_content',
253+
'mc_submit_text',
254+
'mc_nuke_all_styles',
255+
'mc_custom_style',
256+
'mc_form_border_width',
257+
'mc_form_border_color',
258+
'mc_form_background',
259+
'mc_form_text_color',
260+
'mc_update_existing',
261+
'mc_double_optin',
262+
'mc_user_id',
263+
'mc_use_javascript',
264+
'mc_use_datepicker',
265+
'mc_use_unsub_link',
266+
'mc_list_id',
267+
'mc_list_name',
268+
'mc_interest_groups',
269+
'mc_merge_vars',
270+
];
271+
272+
// Clear all options
273+
cy.getListId('10up').then((listId) => {
274+
cy.getMergeFields(listId).then((mergeFields) => {
275+
const mergeFieldOptions = mergeFields.map((field) => `mc_mv_${field.tag}`);
276+
options.push(...mergeFieldOptions);
277+
cy.wpCli(`wp option delete ${options.join(' ')}`).then(() => {
278+
cy.visit('/wp-admin/admin.php?page=mailchimp_sf_options');
279+
cy.mailchimpLogout();
280+
cy.mailchimpLogin();
281+
cy.get('.mc-user h3').contains('Logged in as: ');
282+
cy.get('input[value="Logout"]').should('exist');
283+
284+
cy.selectList('10up');
285+
286+
// Verify default settings
287+
cy.get('#mc_header_content').should('have.value', 'Sign up for 10up');
288+
cy.get('#mc_subheader_content').should('have.value', '');
289+
cy.get('#mc_submit_text').should('have.value', 'Subscribe');
290+
cy.get('#mc_nuke_all_styles').should('not.be.checked');
291+
cy.get('#mc_custom_style').should('not.be.checked');
292+
cy.get('#mc_form_border_width').should('have.value', '1');
293+
cy.get('#mc_form_border_color').should('have.value', 'E0E0E0');
294+
cy.get('#mc_form_background').should('have.value', 'FFFFFF');
295+
cy.get('#mc_form_text_color').should('have.value', '3F3F3f');
296+
cy.get('#mc_update_existing').should('not.be.checked');
297+
cy.get('#mc_double_optin').should('be.checked');
298+
cy.get('#mc_use_unsub_link').should('not.be.checked');
299+
cy.get('#mc_mv_FNAME').should('be.checked');
300+
cy.get('#mc_mv_LNAME').should('be.checked');
301+
cy.get('#mc_mv_ADDRESS').should('be.checked');
302+
cy.get('#mc_mv_BIRTHDAY').should('be.checked');
303+
cy.get('#mc_mv_COMPANY').should('be.checked');
304+
cy.get('#mc_mv_PHONE').should('be.checked');
305+
});
306+
});
307+
});
308+
});
254309
});

tests/cypress/support/commands/mailchimpApi.js

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ async function getContactsFromAList(listId, status = null) {
8888
*
8989
* @param {string} listId - The Mailchimp list ID to search within.
9090
* @param {string} email - The email address of the contact to retrieve.
91-
* @returns {Promise<object|null>} - A promise that resolves with the contact's details
91+
* @returns {Promise<object|null>} - A promise that resolves with the contact's details
9292
* if found, or `null` if the contact does not exist.
93-
*
93+
*
9494
* @throws {Error} - Throws an error for unexpected failures (non-404 errors).
95-
*
95+
*
9696
* Example:
9797
* cy.getContact(listId, 'user@example.com').then((contact) => {
9898
* if (contact) {
@@ -142,7 +142,7 @@ function getContactFromList(email, listName = '10up') {
142142
*
143143
* TODO: Configuration this to use the batch endpoint. Is the /batch endpoint worth the lift?
144144
* https://mailchimp.com/developer/marketing/guides/run-async-requests-batch-endpoint/#make-a-batch-operations-request
145-
*
145+
*
146146
* @param {string} listId - The Mailchimp list ID
147147
* @param {object} data - The data to update the merge fields with - Docs: https://mailchimp.com/developer/marketing/api/list-merges/update-merge-field/
148148
* @returns {Promise} - A promise that resolves when all merge fields are updated
@@ -157,35 +157,6 @@ async function updateMergeFieldsByList(listId, data) {
157157
return await Promise.all(updatedMergeFields);
158158
}
159159

160-
// TODO: Can we implement batch synchronously?
161-
// async function updateMergeFieldsByList(listId, data) {
162-
// const mergeFields = await getMergeFields(listId);
163-
164-
// // Prepare batch operations
165-
// const operations = mergeFields.map((field) => ({
166-
// method: "PATCH", // HTTP method for updating merge fields
167-
// path: `/lists/${listId}/merge-fields/${field.merge_id}`, // API path for each merge field
168-
// body: JSON.stringify({
169-
// ...data,
170-
// name: field.name, // Keep existing name
171-
// }),
172-
// }));
173-
174-
// try {
175-
// // Send the batch request
176-
// const response = await mailchimp.batches.start({
177-
// operations, // Array of operations
178-
// });
179-
180-
// console.log("Batch operation initiated:", response);
181-
182-
// return response;
183-
// } catch (error) {
184-
// console.error("Error starting batch operation:", error);
185-
// throw error;
186-
// }
187-
// }
188-
189160
/**
190161
* Update merge field by tag
191162
*
@@ -202,10 +173,23 @@ async function updateMergeFieldByTag(listId, tag, data) {
202173
return response;
203174
}
204175

176+
177+
/**
178+
* Get all merge fields for a list
179+
*
180+
* @param {string} listId - The Mailchimp list ID
181+
* @returns {Promise} - A promise that resolves with all merge fields for the list
182+
*/
183+
Cypress.Commands.add('getMergeFields', async (listId) => {
184+
return getMergeFields(listId);
185+
});
186+
205187
/**
206188
* Get all merge fields for a list
207-
*
189+
*
208190
* Mailchimp paginates merge fields
191+
* @param {string} listId - The Mailchimp list ID
192+
* @returns {Promise} - A promise that resolves with all merge fields for the list
209193
*/
210194
async function getMergeFields(listId) {
211195
let mergeFields = [];
@@ -250,8 +234,8 @@ async function updateMergeField(listId, mergeId, name, data) {
250234
* Wrapper function to delete a contact specifically from the "10up" Mailchimp list.
251235
*
252236
* This function wraps the generic `deleteContact` function and automatically
253-
* retrieves the list ID for the "10up" list. It simplifies the process of
254-
* deleting contacts from this specific list by removing the need to manually
237+
* retrieves the list ID for the "10up" list. It simplifies the process of
238+
* deleting contacts from this specific list by removing the need to manually
255239
* provide the list ID.
256240
*
257241
* @param {string} email - The email address of the contact to delete
@@ -328,4 +312,4 @@ function subscribeToListByName(email, listName = '10up') {
328312
cy.subscribeToList(listId, email);
329313
console.log(`Successfully subscribed ${email} to list ${listName}`);
330314
});
331-
}
315+
}

tests/cypress/support/commands/mailchimpLogin.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ Cypress.Commands.add('mailchimpLogout', () => {
1212

1313
/**
1414
* Log into Mailchimp account
15-
*
15+
*
1616
* Not sure we should put this much logic into one command, but we need
1717
* the Mailchimp login functionality to test settings.test.js independently
1818
*/
1919
Cypress.Commands.add('mailchimpLogin', (username = null, password = null) => {
2020
username = username ?? Cypress.env('MAILCHIMP_USERNAME');
2121
password = password ?? Cypress.env('MAILCHIMP_PASSWORD');
22-
22+
2323
cy.visit('/wp-admin/admin.php?page=mailchimp_sf_options');
2424

2525
// Logout if already connected.
@@ -66,9 +66,9 @@ Cypress.Commands.add('mailchimpLogin', (username = null, password = null) => {
6666
/**
6767
* Adds a wrapper over the mailchimpLogin command to check if
6868
* a user is already logged in.
69-
*
69+
*
7070
* This is to increase testing speed
71-
*
71+
*
7272
* The name is a mouth full, but is named as such to be explicit
7373
*/
7474
Cypress.Commands.add('mailchimpLoginIfNotAlreadyLoggedIn', () => {
@@ -78,8 +78,9 @@ Cypress.Commands.add('mailchimpLoginIfNotAlreadyLoggedIn', () => {
7878
const hasLogout = $body.find('input[value="Logout"]').length > 0;
7979
if (!hasLogout) {
8080
cy.mailchimpLogin();
81+
cy.get('.mc-user h3').contains('Logged in as: ');
8182
} else {
8283
cy.log('Already logged into Mailchimp account');
8384
}
8485
});
85-
});
86+
});

tests/cypress/support/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ before(() => {
2424
// Add global setup logic here
2525
cy.checkMailchimpEnv(); // Example: Check environment variables
2626
cy.checkMailchimpApi(); // Throw error if we can't connect to the API
27-
2827
cy.log('Global setup completed!');
2928

3029
// Default settings for tests

0 commit comments

Comments
 (0)