Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run E2E tests with Enable by default option checked and unchecked. #167

Merged
merged 7 commits into from
Jun 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
174 changes: 85 additions & 89 deletions tests/cypress/integration/block-editor.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
describe('Test Autoshare for Twitter with Block Editor.', () => {
before(()=>{
before(() => {
cy.login();
// Ignore WP 5.2 Synchronous XHR error.
Cypress.on('uncaught:exception', (err, runnable) => {
if (err.message.includes("Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'http://localhost:8889/wp-admin/admin-ajax.php': Synchronous XHR in page dismissal") ){
if (err.message.includes("Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'http://localhost:8889/wp-admin/admin-ajax.php': Synchronous XHR in page dismissal") ) {
return false;
}
});
Expand All @@ -16,110 +16,106 @@ describe('Test Autoshare for Twitter with Block Editor.', () => {
cy.get('#submit').click();
});

// Run test cases with default Autoshare enabled and disabled both.
const defaultBehaviors = [false, true];
defaultBehaviors.forEach((defaultBehavior) => {
it(`Can ${defaultBehavior ? 'Enable' : 'Disable'} default Autoshare`, () => {
cy.visit('/wp-admin/options-general.php?page=autoshare-for-twitter');

const defaultSelector = 'input:checkbox[name="autoshare-for-twitter[enable_default]"]';
cy.get(defaultSelector).should('exist');
if (true === defaultBehavior) {
cy.get(defaultSelector).check();
} else {
cy.get(defaultSelector).uncheck();
}
cy.get('#submit').click();
});

it('Can disable default Autoshare', () => {
cy.visit('/wp-admin/options-general.php?page=autoshare-for-twitter');
cy.get('input:checkbox[name="autoshare-for-twitter[enable_default]"]').should('exist');
cy.get('input:checkbox[name="autoshare-for-twitter[enable_default]"]').uncheck();
cy.get('#submit').click();
});
it('Tests that new post is not tweeted when box is unchecked', () => {
// Start create new post by enter post title
cy.startCreatePost();

// Open pre-publish Panel.
cy.openPrePublishPanel();

// Check enable checkbox for auto-share.
cy.enableCheckbox('.autoshare-for-twitter-prepublish__checkbox input:checkbox', defaultBehavior, false);

it('Tests that new post is not tweeted when box is unchecked', () => {
// Start create new post by enter post title
cy.startCreatePost();

cy.get('.editor-post-publish-panel__toggle').should('be.visible');
cy.get('.editor-post-publish-panel__toggle').click();
// Publish
cy.get('[aria-disabled="false"].editor-post-publish-button').should('be.visible');
cy.get('.editor-post-publish-button').click();

// Publish
cy.get('.editor-post-publish-button').should('be.visible');
cy.get('.editor-post-publish-button').click();
// Post-publish.
cy.get('.autoshare-for-twitter-post-status').should('be.visible');
cy.get('.autoshare-for-twitter-post-status').contains('This post was not tweeted.');
});

// Post-publish.
cy.get('.autoshare-for-twitter-post-status').should('be.visible');
cy.get('.autoshare-for-twitter-post-status').contains('This post was not tweeted.');
});
it('Tests that new post is tweeted when box is checked', () => {
// Start create new post by enter post title
cy.startCreatePost();

// Open pre-publish Panel.
cy.openPrePublishPanel();

it('Tests that new post is tweeted when box is checked', () => {
// Start create new post by enter post title
cy.startCreatePost();
// Check enable checkbox for auto-share.
cy.enableCheckbox('.autoshare-for-twitter-prepublish__checkbox input:checkbox', defaultBehavior, true);

// Open pre-publish Panel.
cy.get('.editor-post-publish-panel__toggle').should('be.visible');
cy.get('.editor-post-publish-panel__toggle').click();
cy.get('.components-panel__body:contains("Autoshare:")').should('exist');
cy.get('.components-panel__body:contains("Autoshare:")').click();
// Publish.
cy.get('[aria-disabled="false"].editor-post-publish-button').should('be.visible');
cy.get('.editor-post-publish-button').click();

// Check enable checkbox for auto-share.
cy.get('.autoshare-for-twitter-prepublish__checkbox input:checkbox').should('be.visible');
cy.get('.autoshare-for-twitter-prepublish__checkbox input:checkbox').check();
// Post-publish.
cy.get('.autoshare-for-twitter-post-status').should('be.visible');
cy.get('.autoshare-for-twitter-post-status').contains('Tweeted on');
});

// Publish.
cy.get('[aria-disabled="false"].editor-post-publish-button').should('be.visible');
cy.get('.editor-post-publish-button').click();
it('Tests that Draft post is not tweeted when box is unchecked', () => {
// Start create new post by enter post title
cy.startCreatePost();

// Post-publish.
cy.get('.autoshare-for-twitter-post-status').should('be.visible');
cy.get('.autoshare-for-twitter-post-status').contains('Tweeted on');
});
// Save Draft
cy.get('.editor-post-save-draft').should('be.visible');
cy.get('.editor-post-save-draft').click();
cy.get('.editor-post-saved-state').should('have.text', 'Saved');

// Open pre-publish Panel.
cy.openPrePublishPanel();

it('Tests that Draft post is not tweeted when box is unchecked', () => {
// Start create new post by enter post title
cy.startCreatePost();
// Uncheck enable checkbox for auto-share.
cy.enableCheckbox('.autoshare-for-twitter-prepublish__checkbox input:checkbox', defaultBehavior, false);

// Save Draft
cy.get('.editor-post-save-draft').should('be.visible');
cy.get('.editor-post-save-draft').click();
cy.get('.editor-post-saved-state').should('have.text', 'Saved');
// Publish.
cy.get('[aria-disabled="false"].editor-post-publish-button').should('be.visible');
cy.get('.editor-post-publish-button').click();

// Open pre-publish Panel.
cy.get('.editor-post-publish-panel__toggle').should('be.visible');
cy.get('.editor-post-publish-panel__toggle').click();
cy.get('.components-panel__body:contains("Autoshare:")').should('exist');
cy.get('.components-panel__body:contains("Autoshare:")').click();
// Post-publish.
cy.get('.autoshare-for-twitter-post-status').should('be.visible');
cy.get('.autoshare-for-twitter-post-status').contains('This post was not tweeted.');
});

// Uncheck enable checkbox for auto-share.
cy.get('.autoshare-for-twitter-prepublish__checkbox input:checkbox').should('be.visible');
cy.get('.autoshare-for-twitter-prepublish__checkbox input:checkbox').uncheck();
it('Tests that Draft post is tweeted when box is checked', () => {
// Start create new post by enter post title
cy.startCreatePost();

// Publish.
cy.get('[aria-disabled="false"].editor-post-publish-button').should('be.visible');
cy.get('.editor-post-publish-button').click();
// Save Draft
cy.get('.editor-post-save-draft').should('be.visible');
cy.get('.editor-post-save-draft').click();
cy.get('.editor-post-saved-state').should('have.text', 'Saved');

// Post-publish.
cy.get('.autoshare-for-twitter-post-status').should('be.visible');
cy.get('.autoshare-for-twitter-post-status').contains('This post was not tweeted.');
});
// Open pre-publish Panel.
cy.openPrePublishPanel();


it('Tests that Draft post is tweeted when box is checked', () => {
// Start create new post by enter post title
cy.startCreatePost();

// Save Draft
cy.get('.editor-post-save-draft').should('be.visible');
cy.get('.editor-post-save-draft').click();
cy.get('.editor-post-saved-state').should('have.text', 'Saved');

// Open pre-publish Panel.
cy.get('.editor-post-publish-panel__toggle').should('be.visible');
cy.get('.editor-post-publish-panel__toggle').click();
cy.get('.components-panel__body:contains("Autoshare:")').should('exist');
cy.get('.components-panel__body:contains("Autoshare:")').click();

// Check enable checkbox for auto-share.
cy.get('.autoshare-for-twitter-prepublish__checkbox input:checkbox').should('be.visible');
cy.get('.autoshare-for-twitter-prepublish__checkbox input:checkbox').check();

// Publish.
cy.get('[aria-disabled="false"].editor-post-publish-button').should('be.visible');
cy.get('.editor-post-publish-button').click();

// Post-publish.
cy.get('.autoshare-for-twitter-post-status').should('be.visible');
cy.get('.autoshare-for-twitter-post-status').contains('Tweeted on');
// Check enable checkbox for auto-share.
cy.enableCheckbox('.autoshare-for-twitter-prepublish__checkbox input:checkbox', defaultBehavior, true);

// Publish.
cy.get('[aria-disabled="false"].editor-post-publish-button').should('be.visible');
cy.get('.editor-post-publish-button').click();

// Post-publish.
cy.get('.autoshare-for-twitter-post-status').should('be.visible');
cy.get('.autoshare-for-twitter-post-status').contains('Tweeted on');
});
});
});
});
139 changes: 77 additions & 62 deletions tests/cypress/integration/classic-editor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,67 +10,82 @@ describe('Test Autoshare for Twitter with Classic Editor.', () => {
cy.get('#submit').click();
});

it('Tests that new post is not tweeted when box is unchecked', () => {
// Start create post.
cy.classicStartCreatePost();

// publish
cy.get('#publish').click();
cy.get('#wpadminbar').should('be.visible');

// Post-publish.
cy.get('#autoshare_for_twitter_metabox').should('be.visible');
cy.get('#autoshare_for_twitter_metabox').contains('This post was not tweeted');
});


it('Tests that new post is tweeted when box is checked', () => {
// Start create post.
cy.classicStartCreatePost();

// Checkbox
cy.get('#autoshare-for-twitter-enable').should('exist');
cy.get('#autoshare-for-twitter-enable').check();
cy.get('#publish').click();

// Post-publish.
cy.get('#autoshare_for_twitter_metabox',).should('be.visible');
cy.get('#autoshare_for_twitter_metabox',).contains('Tweeted on');
});


it('Tests that draft post is not tweeted when box is unchecked', () => {
// Start create post.
cy.classicStartCreatePost();

// Save Draft
cy.get('#save-post').click();

// Uncheck the checkbox and publish
cy.get('#autoshare-for-twitter-enable').should('exist');
cy.get('#autoshare-for-twitter-enable').uncheck();
cy.get('#publish').click();

// Post-publish.
cy.get('#autoshare_for_twitter_metabox').should('be.visible');
cy.get('#autoshare_for_twitter_metabox').contains('This post was not tweeted');
});


it('Tests that draft post is tweeted when box is checked', () => {
// Start create post.
cy.classicStartCreatePost();

// Save Draft
cy.get('#save-post').click();

// Check the checkbox and publish
cy.get('#autoshare-for-twitter-enable').should('exist');
cy.get('#autoshare-for-twitter-enable').check();
cy.get('#publish').click();

// Post-publish.
cy.get('#autoshare_for_twitter_metabox').should('be.visible');
cy.get('#autoshare_for_twitter_metabox').contains('Tweeted on');
// Run test cases with default Autoshare enabled and disabled both.
const defaultBehaviors = [false, true];
defaultBehaviors.forEach( (defaultBehavior) => {
it(`Can ${(defaultBehavior ? 'Enable': 'Disable')} default Autoshare`, () => {
cy.visit('/wp-admin/options-general.php?page=autoshare-for-twitter');
cy.get('input:checkbox[name="autoshare-for-twitter[enable_default]"]').should('exist');
if (true === defaultBehavior) {
cy.get('input:checkbox[name="autoshare-for-twitter[enable_default]"]').check();
} else {
cy.get('input:checkbox[name="autoshare-for-twitter[enable_default]"]').uncheck();
}
cy.get('#submit').click();
});

it('Tests that new post is not tweeted when box is unchecked', () => {
// Start create post.
cy.classicStartCreatePost();

// Check enable checkbox for auto-share.
cy.enableCheckbox('#autoshare-for-twitter-enable', defaultBehavior, false);

// publish
cy.get('#publish').click();
cy.get('#wpadminbar').should('be.visible');

// Post-publish.
cy.get('#autoshare_for_twitter_metabox').should('be.visible');
cy.get('#autoshare_for_twitter_metabox').contains('This post was not tweeted');
});


it('Tests that new post is tweeted when box is checked', () => {
// Start create post.
cy.classicStartCreatePost();

// Check enable checkbox for auto-share.
cy.enableCheckbox('#autoshare-for-twitter-enable', defaultBehavior, true);
cy.get('#publish').click();

// Post-publish.
cy.get('#autoshare_for_twitter_metabox',).should('be.visible');
cy.get('#autoshare_for_twitter_metabox',).contains('Tweeted on');
});


it('Tests that draft post is not tweeted when box is unchecked', () => {
// Start create post.
cy.classicStartCreatePost();

// Save Draft
cy.get('#save-post').click();

// Uncheck the checkbox and publish
cy.enableCheckbox('#autoshare-for-twitter-enable', defaultBehavior, false);
cy.get('#publish').click();

// Post-publish.
cy.get('#autoshare_for_twitter_metabox').should('be.visible');
cy.get('#autoshare_for_twitter_metabox').contains('This post was not tweeted');
});


it('Tests that draft post is tweeted when box is checked', () => {
// Start create post.
cy.classicStartCreatePost();

// Save Draft
cy.get('#save-post').click();

// Check the checkbox and publish
cy.enableCheckbox('#autoshare-for-twitter-enable', defaultBehavior, true);
cy.get('#publish').click();

// Post-publish.
cy.get('#autoshare_for_twitter_metabox').should('be.visible');
cy.get('#autoshare_for_twitter_metabox').contains('Tweeted on');
});
});
});
37 changes: 37 additions & 0 deletions tests/cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,40 @@ Cypress.Commands.add( 'classicStartCreatePost', () => {
let postTitle = getRandomText(8);
cy.get('input[name="post_title"]').type('Random Post Title' + postTitle );
});

Cypress.Commands.add( 'openPrePublishPanel', () => {
// Open pre-publish Panel.
cy.get('.editor-post-publish-panel__toggle').should('be.visible');
cy.get('.editor-post-publish-panel__toggle').click();
cy.wait(500); // prevent clicking on category assign suggestion panel. ToDo: find more proper way to handle this.
cy.get('.components-panel__body:contains("Autoshare:")').should('exist');
cy.get('.components-panel__body:contains("Autoshare:")').click();
});

Cypress.Commands.add( 'enableCheckbox', ( checkboxSelector, defaultBehavior, check = true ) => {
// Check/Uncheck enable checkbox for auto-share.
cy.get(checkboxSelector).should('be.visible');
if (true === defaultBehavior) {
cy.get(checkboxSelector).should('be.checked');
} else {
cy.get(checkboxSelector).should('not.be.checked');
}
cy.intercept('**/autoshare/v1/post-autoshare-for-twitter-meta/*').as('enableCheckbox');
if (true === check) {
cy.get(checkboxSelector).check();
if(defaultBehavior !== check){
cy.wait('@enableCheckbox').then(response => {
expect(response.response?.body?.enabled).to.equal(check);
});
}
cy.get(checkboxSelector).should('be.checked');
} else {
cy.get(checkboxSelector).uncheck();
if(defaultBehavior !== check){
cy.wait('@enableCheckbox').then(response => {
expect(response.response?.body?.enabled).to.equal(check);
});
}
cy.get(checkboxSelector).should('not.be.checked');
}
});