Skip to content

Commit

Permalink
update support command functions
Browse files Browse the repository at this point in the history
  • Loading branch information
sksaju committed Nov 24, 2024
1 parent c4f166f commit a721bde
Showing 1 changed file with 13 additions and 62 deletions.
75 changes: 13 additions & 62 deletions tests/cypress/support/commands.js
Original file line number Diff line number Diff line change
@@ -1,77 +1,28 @@
/* eslint-disable no-undef */

// Add local storage item
Cypress.Commands.add('setLocalStorage', (key, value) => {
cy.window().then((win) => {
win.localStorage.setItem(key, value);
win.localStorage.setItem(key, JSON.stringify(value));
});
});

Cypress.Commands.add('createPost', (options = {}) => {
const { postType = 'post', title = '' } = options;
const userId = '1';

cy.setLocalStorage(
`WP_DATA_USER_${userId}`,
JSON.stringify({
'core/edit-post': {
preferences: {
panels: {
'post-status': {
opened: true,
},
},
features: {
fixedToolbar: false,
welcomeGuide: false,
fullscreenMode: true,
showIconLabels: false,
themeStyles: true,
showBlockBreadcrumbs: true,
welcomeGuideTemplate: false,
},
editorMode: 'visual',
hiddenBlockTypes: [],
preferredStyleVariations: {},
localAutosaveInterval: 15,
},
},
'core/editor': {
preferences: {
isPublishSidebarEnabled: false,
},
},
}),
);

cy.get('#wp-admin-bar-new-content-default')
.contains(postType, { matchCase: false })
.click({ force: true });

cy.wait(100);

// close the Yoast SEO metabox
cy.get('.wpseo-metabox .handlediv').then((button) => {
const isExpanded = button[0].getAttribute('aria-expanded') === 'true';
if (isExpanded) {
cy.get('.wpseo-metabox .handlediv').click();
}
});

cy.wait(100);

if (title !== '') {
cy.get('.editor-post-title__input').type(
`Cypress - ${title} - ${new Date(Date.now()).toLocaleDateString()}`,
);
// Create a new post
Cypress.Commands.add('createPost', ({ title = '' } = {}) => {
cy.visit('/wp-admin/post-new.php');
if (title) {
cy.get('.editor-post-title__input').type(`${title} - ${Date.now()}`);
}
});

// Save the post
Cypress.Commands.add('savePost', () => {
cy.get('[type="button"]').contains('Publish').click();
cy.get('button').contains('Publish').click();
});

// Insert a Gutenberg block
Cypress.Commands.add('insertBlock', (blockName) => {
cy.get('button[aria-label="Add block"]').first().click();
cy.focused().type(blockName);
cy.get('button').contains(blockName).click();
cy.get('button[aria-label="Add block"]').click();
cy.get('.block-editor-inserter__search input').type(blockName);
cy.contains('.block-editor-block-types-list__item', blockName).click();
});

0 comments on commit a721bde

Please sign in to comment.