Skip to content

Commit

Permalink
Search e2e: Fix tests to make 6.7 compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
rebeccahum committed Nov 22, 2024
1 parent d566621 commit 4500479
Show file tree
Hide file tree
Showing 3 changed files with 248 additions and 59 deletions.
106 changes: 59 additions & 47 deletions tests/search/e2e/integration/features/custom-results.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,30 @@ describe('Custom Results', () => {
const testPost = 'test-post';

before(() => {
cy.wpCli("wp post list --post_type='ep-pointer' --format=ids", true).then(
(wpCliResponse) => {
if (wpCliResponse.code === 0) {
cy.wpCli(`wp post delete ${wpCliResponse.stdout} --force`, true);
}
},
);

cy.wpCli(`wp post list --s='${testPost}' --ep_integrate='false' --format=ids`, true).then(
(wpCliResponse) => {
if (wpCliResponse.code === 0) {
cy.wpCli(`wp post delete ${wpCliResponse.stdout} --force`, true);
}
},
cy.wpCliEval(
`
$ep_pointers = get_posts(
[
'post_type' => 'ep-pointer',
'numberposts' => 999,
]
);
foreach( $ep_pointers as $pointer ) {
wp_delete_post( $pointer->ID, true );
}
$posts = new \\WP_Query(
[
's' => '${testPost}',
'ep_integrate' => false,
'fields' => 'ids',
'numberposts' => 999,
]
);
foreach( $posts->posts as $post ) {
wp_delete_post( $post->ID, true );
}
`
);
});

Expand All @@ -25,18 +35,20 @@ describe('Custom Results', () => {

cy.login();
cy.visitAdminPage('post-new.php?post_type=ep-pointer');
cy.intercept('GET', 'wp-json/elasticpress/v1/pointer_preview*').as('ajaxRequest');
cy.intercept('GET', 'wp-json/elasticpress/v1/pointer_preview*').as(
'ajaxRequest'
);

cy.get('#titlewrap input').type(searchTerm);
cy.wait('@ajaxRequest').its('response.statusCode').should('eq', 200);

// change the position of the post
// eslint-disable-next-line jest/valid-expect-in-promise
cy.dragAndDrop(
'.pointers .pointer:first-of-type .dashicons-menu',
'.pointers .pointer:last-of-type .dashicons-menu',
'.pointers .pointer:last-of-type .dashicons-menu'
).then(() => {
// save the posts positions in a list
// eslint-disable-next-line cypress/unsafe-to-chain-command
cy.get('.pointers .pointer .title')
.each((post) => {
searchResult.push(post[0].innerText);
Expand All @@ -48,11 +60,11 @@ describe('Custom Results', () => {
cy.visit(`?s=${searchTerm}`);

// verify the result of the search is in the same position.
cy.get('h2.entry-title').each(
(post, index) => {
expect(post[0].innerText).to.equal(searchResult[index]);
},
);
cy.get(
`article:nth-child(-n+${searchResult.length}) .entry-title`
).each((post, index) => {
expect(post[0].innerText).to.equal(searchResult[index]);
});
});
});
});
Expand All @@ -66,12 +78,16 @@ describe('Custom Results', () => {
});

cy.visitAdminPage('post-new.php?post_type=ep-pointer');
cy.intercept('GET', 'wp-json/elasticpress/v1/pointer_preview*').as('ajaxRequest');
cy.intercept('GET', 'wp-json/elasticpress/v1/pointer_preview*').as(
'ajaxRequest'
);

cy.get('#titlewrap input').type(searchTerm);
cy.wait('@ajaxRequest').its('response.statusCode').should('eq', 200);

cy.intercept('GET', 'wp-json/elasticpress/v1/pointer_search*').as('ajaxRequest');
cy.intercept('GET', 'wp-json/elasticpress/v1/pointer_search*').as(
'ajaxRequest'
);

// search for the post.
cy.get('.search-pointers').type(testPost);
Expand All @@ -81,35 +97,31 @@ describe('Custom Results', () => {
cy.get('.pointer-result:first-of-type .dashicons-plus.add-pointer').click();

// save the posts positions in a list
// eslint-disable-next-line jest/valid-expect-in-promise
// eslint-disable-next-line cypress/unsafe-to-chain-command
cy.get('.pointers .pointer:nth-child(-n+5) .title') // 5 being the number of posts per page, as we will check only the first page.
.each((post) => {
searchResult.push(post[0].innerText);
})
.then(() => {
expect(searchResult.length).to.be.gt(0);
cy.get('#publish')
.click()
.then(() => {
/**
* Give Elasticsearch some time to update the posts in custom results.
*/
// eslint-disable-next-line cypress/no-unnecessary-waiting
cy.wait(4000);

// VIP: Since cron doesn't run, we need to manually force a sync and clear cache since the query will be cached.
cy.get('#publish').click();
cy.wpCli(`wp cache flush`, true);

cy.visit(`?s=${searchTerm}`);

// verify the result of the search is in the same position.
cy.get('h2.entry-title').each(
(post, index) => {
expect(post[0].innerText).to.equal(searchResult[index]);
},
);
});
cy.get('#publish').click();
/**
* Give Elasticsearch some time to update the posts in custom results.
*/
cy.wait(4000);

// VIP: Since cron doesn't run, we need to manually force a sync and clear cache since the query will be cached.
cy.get('#publish').click();
cy.wpCli(`wp cache flush`, true);

// eslint-disable-next-line cypress/no-unnecessary-waiting
cy.wait(1000);
cy.visit(`?s=${searchTerm}`);

// verify the result of the search is in the same position.
cy.get('h2.entry-title').each((post, index) => {
expect(post[0].innerText).to.equal(searchResult[index]);
});
});
});
});
34 changes: 31 additions & 3 deletions tests/search/e2e/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ Cypress.Commands.add('publishPost', (postData, viewPost) => {
const welcomeGuide = $body.find(
'.edit-post-welcome-guide .components-modal__header button',
);
cy.log(welcomeGuide);
if (welcomeGuide.length) {
welcomeGuide.click();
}
Expand All @@ -135,16 +134,45 @@ Cypress.Commands.add('publishPost', (postData, viewPost) => {
cy.get('h1.editor-post-title__input, #post-title-0').clearThenType(newPostData.title);
cy.get('.block-editor-default-block-appender__content').type(newPostData.content);

if (newPostData.password && newPostData.password !== '') {
cy.get('h1.editor-post-title__input').click();
if (wpVersion === '6.0') {
cy.get('body').then(($body) => {
const $button = $body.find('.edit-post-post-visibility__toggle');
if (!$button.is(':visible')) {
cy.get('.edit-post-header__settings button[aria-label="Settings"]').click();
}
});
cy.get('.edit-post-post-visibility__toggle').click();
cy.get('.editor-post-visibility__dialog-radio, .editor-post-visibility__radio').check(
'password',
);
cy.get(
'.editor-post-visibility__dialog-password-input, .editor-post-visibility__password-input',
).type(newPostData.password);
} else {
cy.get('body').then(($body) => {
const $button = $body.find('.components-dropdown.editor-post-status');
if (!$button.is(':visible')) {
cy.get('.editor-header__settings button[aria-label="Settings"]').click();
}
});
cy.get('.components-dropdown.editor-post-status').click();
cy.get('.editor-change-status__password-fieldset input[type="checkbox"]').click();
cy.get('.editor-change-status__password-input input').type(newPostData.password);
}
}

if (newPostData.status && newPostData.status === 'draft') {
cy.get('.editor-post-save-draft').click();
cy.get('.editor-post-saved-state').should('have.text', 'Saved');
} else {
cy.get('.editor-post-publish-panel__toggle').should('be.enabled');
cy.get('.editor-post-publish-panel__toggle').click();
cy.get('.editor-post-publish-panel__toggle').click( { force: true } );

cy.get('.editor-post-publish-button').click();

cy.get('.components-snackbar', { timeout: 6000 } ).should('be.visible');
cy.get('.components-snackbar').should('be.visible');

if (viewPost) {
cy.get('.post-publish-panel__postpublish-buttons a').contains('View Post').click();
Expand Down
Loading

0 comments on commit 4500479

Please sign in to comment.