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

Classic Create Post command #65

Merged
merged 2 commits into from
Jun 9, 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
58 changes: 58 additions & 0 deletions src/commands/classic-create-post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import PostData from '../interface/post-data';

/**
* Create Post in Classic Editor
*
* @param postData - Post data.
*
* @example
* ```
* cy.classicCreatePost({
* title: 'Post title',
* content: 'Post content',
* beforeSave: () => {
* // Do something before save.
* },
* postType: 'page',
* status: 'draft'
* }).then(postID => {
* cy.log(postID);
* })
* ```
*/
export const classicCreatePost = ({
postType = 'post',
title = 'Test Post',
content = 'Test content',
status = 'publish',
beforeSave,
}: PostData): void => {
cy.visit(`/wp-admin/post-new.php?post_type=${postType}`);

cy.get('#title').click().clear().type(title);

cy.get('#content_ifr').then($iframe => {
const doc = $iframe.contents().find('body#tinymce');
cy.wrap(doc).find('p:last-child').type(content);
});

if ('undefined' !== typeof beforeSave) {
beforeSave();
}

cy.intercept('POST', '/wp-admin/post.php', req => {
req.alias = 'savePost';
});

if ('draft' === status) {
cy.get('#save-post').click();
} else {
cy.get('#publish').click();
}

cy.wait('@savePost').then(response => {
const body = new URLSearchParams(response.request?.body);
const id = body.get('post_ID');
cy.wrap(id);
});
};
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/// <reference types="cypress" />

// Import commands.
import { classicCreatePost } from './commands/classic-create-post';
import { insertBlock } from './commands/insert-block';
import { closeWelcomeGuide } from './commands/close-welcome-guide';
import { wpCliEval } from './commands/wp-cli-eval';
Expand All @@ -22,6 +23,7 @@ import { createPost } from './commands/create-post';
declare global {
namespace Cypress {
interface Chainable<Subject> {
classicCreatePost: typeof classicCreatePost;
insertBlock: typeof insertBlock;
closeWelcomeGuide: typeof closeWelcomeGuide;
wpCliEval: typeof wpCliEval;
Expand All @@ -43,6 +45,7 @@ declare global {
}

// Register commands
Cypress.Commands.add('classicCreatePost', classicCreatePost);
Cypress.Commands.add('insertBlock', insertBlock);
Cypress.Commands.add('closeWelcomeGuide', closeWelcomeGuide);
Cypress.Commands.add('wpCliEval', wpCliEval);
Expand Down
29 changes: 29 additions & 0 deletions src/interface/post-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Post Data
*/
export default interface PostData {
/**
* Post title
*/
title: string;

/**
* Post type
*/
postType?: string;

/**
* Post content
*/
content?: string;

/**
* Post status
*/
status?: string;

/**
* Before save callback
*/
beforeSave?: CallableFunction;
}
87 changes: 87 additions & 0 deletions tests/cypress/integration/classic-create-post.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
const { randomName } = require('../support/functions');

describe('Command: classicCreatePost', () => {
before(() => {
cy.login();
cy.activatePlugin('classic-editor');
iamdharmesh marked this conversation as resolved.
Show resolved Hide resolved
});

it('Should be able to Classic Create Post', () => {
const title = 'Title ' + randomName();
const content = 'Content ' + randomName();
cy.classicCreatePost({
title: title,
content: content,
});

cy.get('.notice-success').should('contain.text', 'Post published');
cy.get('#title').should('have.value', title);
cy.get('#content_ifr').then($iframe => {
const doc = $iframe.contents().find('body#tinymce');
cy.wrap(doc).should('contain.text', content);
});
});

it('Should wrap post ID', () => {
const title = 'Title ' + randomName();
const content = 'Content ' + randomName();
cy.classicCreatePost({
title: title,
content: content,
}).then(id => {
cy.visit(`/wp-admin/post.php?post=${id}&action=edit`);
cy.get('#title').should('have.value', title);
});
});

it('Should perform beforeSave', () => {
const title = 'Title ' + randomName();
const content = 'Content ' + randomName();
const additional = 'Content ' + randomName();
cy.classicCreatePost({
title: title,
content: content,
beforeSave: () => {
cy.get('#content_ifr').then($iframe => {
const doc = $iframe.contents().find('body#tinymce');
cy.wrap(doc)
.find('p:last-child')
.type('{enter}' + additional);
});
},
});

cy.get('.notice-success').should('contain.text', 'Post published');
cy.get('#title').should('have.value', title);
cy.get('#content_ifr').then($iframe => {
const doc = $iframe.contents().find('body#tinymce');
cy.wrap(doc)
.should('contain.text', content)
.should('contain.text', additional);
});
});

it('Should save draft', () => {
const title = 'Title ' + randomName();
const content = 'Content ' + randomName();
cy.classicCreatePost({
title: title,
content: content,
status: 'draft',
});

cy.get('.notice-success').should('contain.text', 'Post draft updated');
});

it('Should be able to create page', () => {
const title = 'Title ' + randomName();
const content = 'Content ' + randomName();
cy.classicCreatePost({
title: title,
content: content,
postType: 'page',
});

cy.get('.notice-success').should('contain.text', 'Page published');
});
});
2 changes: 2 additions & 0 deletions tests/cypress/integration/close-welcome-guide.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ describe('Command: closeWelcomeGuide', () => {
before(() => {
cy.login();

cy.deactivatePlugin('classic-editor');

// Disable Classic Editor if it's enabled
cy.visit('/wp-admin/options-writing.php');
cy.get('body').then($body => {
Expand Down
2 changes: 2 additions & 0 deletions tests/cypress/integration/open-document-settings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ describe('Commands: openDocumentSettings*', () => {
before(() => {
cy.login();

cy.deactivatePlugin('classic-editor');

// Disable Classic Editor if it's enabled
cy.visit('/wp-admin/options-writing.php');
cy.get('body').then($body => {
Expand Down