Skip to content

Commit

Permalink
Merge pull request #2851 from 10up/fix/issue-2788
Browse files Browse the repository at this point in the history
Add tests for instant results feature
  • Loading branch information
felipeelia authored Sep 20, 2022
2 parents 176b4b7 + a2c0906 commit 5afa20d
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .wp-env.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"https://downloads.wordpress.org/plugin/debug-bar.zip",
"https://downloads.wordpress.org/plugin/debug-bar-elasticpress.zip",
"https://downloads.wordpress.org/plugin/wordpress-importer.zip",
"https://downloads.wordpress.org/plugin/woocommerce.zip"
"https://downloads.wordpress.org/plugin/woocommerce.zip",
"10up/elasticpress-proxy#develop"
],
"mappings": {
"wp-content/mu-plugins/unique-index-name.php": "./tests/cypress/wordpress-files/test-mu-plugins/unique-index-name.php",
Expand Down
5 changes: 3 additions & 2 deletions assets/js/autosuggest.js
Original file line number Diff line number Diff line change
Expand Up @@ -676,9 +676,10 @@ function init() {
*/
const prepareInputForAutosuggest = (input) => {
/**
* Skip facet widget search fields.
* Skip facet widget search fields and instant results.
*/
if (input.classList.contains('facet-search')) {
const ignoredClasses = ['facet-search', 'ep-search-input'];
if (ignoredClasses.some((className) => input.classList.contains(className))) {
return;
}

Expand Down
8 changes: 3 additions & 5 deletions bin/setup-cypress-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ fi

./bin/wp-env-cli tests-wordpress "wp --allow-root import /var/www/html/wp-content/uploads/content-example.xml --authors=create"

./bin/wp-env-cli tests-wordpress "wp --allow-root plugin deactivate woocommerce"
./bin/wp-env-cli tests-wordpress "wp --allow-root plugin deactivate woocommerce elasticpress-proxy"

./bin/wp-env-cli tests-wordpress "wp --allow-root plugin activate debug-bar debug-bar-elasticpress wordpress-importer --network"

Expand All @@ -87,7 +87,5 @@ fi
./bin/wp-env-cli tests-wordpress "wp --allow-root user meta update admin edit_post_per_page 5"

# Generate a SQL file that can be imported later to make things faster
SQL_FILENAME=./bin/$(date +'%F-%H-%M').sql
./bin/wp-env-cli tests-wordpress "wp --allow-root db export -" > $SQL_FILENAME

exit 0
# SQL_FILENAME=./bin/$(date +'%F-%H-%M').sql
# npm --silent run env run tests-cli "wp db export -" > $SQL_FILENAME
144 changes: 144 additions & 0 deletions tests/cypress/integration/features/instant-results.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/* global isEpIo */

describe('Instant Results Feature', () => {
/**
* Create a Search widget.
*
* As tests for facets will remove all widgets, we recreate it here.
*/
function createSearchWidget() {
cy.openWidgetsPage();
cy.openBlockInserter();
cy.getBlocksList().should('contain.text', 'Search'); // Checking if it exists give JS time to process the full list.
cy.insertBlock('Search');
cy.intercept('/wp-json/wp/v2/sidebars/*').as('sidebarsRest');
cy.get('.edit-widgets-header__actions button').contains('Update').click();
cy.wait('@sidebarsRest');
}

function maybeEnableProxy() {
if (!isEpIo) {
cy.activatePlugin('elasticpress-proxy');
}
}

before(() => {
createSearchWidget();

// Create some sample posts
cy.publishPost({
title: 'Blog post',
content: 'This is a sample Blog post.',
});
cy.publishPost({
title: 'Test Post',
content: 'This is a sample test post.',
});
});

after(() => {
cy.deactivatePlugin('elasticpress-proxy');
});

/**
* Test that the feature cannot be activated when not in ElasticPress.io nor using a custom PHP proxy.
*/
it("Can't activate the feature if not in ElasticPress.io nor using a custom PHP proxy", () => {
if (isEpIo) {
return;
}

cy.login();
cy.deactivatePlugin('elasticpress-proxy');
cy.visitAdminPage('admin.php?page=elasticpress');
cy.get('.ep-feature-instant-results .settings-button').click();
cy.get('.requirements-status-notice').should(
'contain.text',
'To use this feature you need to be an ElasticPress.io customer or implement a custom proxy',
);
cy.get('.ep-feature-instant-results .input-wrap').should('have.class', 'disabled');
});

/**
* Test that the feature can be activated and it can sync automatically.
* Also, it can show a warning when using a custom PHP proxy
*/
it('Can activate the feature and sync automatically', () => {
cy.login();

// Can see the warning if using custom proxy
maybeEnableProxy();
cy.visitAdminPage('admin.php?page=elasticpress');
cy.get('.ep-feature-instant-results .settings-button').click();

cy.get('.ep-feature-instant-results .input-wrap').should('not.have.class', 'disabled');
cy.get('.requirements-status-notice').should(
isEpIo ? 'not.contain.text' : 'contain.text',
'You are using a custom proxy. Make sure you implement all security measures needed',
);

cy.get('.ep-feature-instant-results [name="settings[active]"][value="1"]').click();
cy.get('.ep-feature-instant-results .button-primary').click();
cy.on('window:confirm', () => {
return true;
});

cy.get('.ep-sync-progress strong', {
timeout: Cypress.config('elasticPressIndexTimeout'),
}).should('contain.text', 'Sync complete');

cy.wpCli('elasticpress list-features').its('stdout').should('contain', 'instant-results');
});

/**
* Test that the instant results list is visible
* It can display the number of test results
* It can show the modal in the same state after a reload
* Can change the URL when search term is changed
*/
it('Can see instant results elements, URL changes, reload, and update after changing search term', () => {
cy.login();
cy.maybeEnableFeature('instant-results');
maybeEnableProxy();

cy.intercept('*search=blog*').as('apiRequest');

cy.visit('/');

cy.get('.wp-block-search').last().as('searchBlock');

cy.get('@searchBlock').find('.wp-block-search__input').type('blog');
cy.get('@searchBlock').find('.wp-block-search__button').click();
cy.get('.ep-search-modal').as('searchModal').should('be.visible'); // Should be visible immediatly
cy.url().should('include', 'search=blog');

cy.wait('@apiRequest');
cy.get('@searchModal').should('contain.text', 'blog');
// Show the number of results
cy.get('@searchModal').find('.ep-search-results__title').contains(/\d+/);

cy.get('.ep-search-sidebar #ep-search-post-type-post')
.click()
.then(() => {
cy.url().should('include', 'ep-post_type=post');
});

// Show the modal in the same state after a reload
cy.reload();
cy.wait('@apiRequest');
cy.get('@searchModal').should('be.visible').should('contain.text', 'blog');

// Update the results when search term is changed
cy.get('@searchModal')
.find('.ep-search-input')
.clearThenType('test')
.then(() => {
cy.wait('@apiRequest');
cy.get('@searchModal').should('be.visible').should('contain.text', 'test');
cy.url().should('include', 'search=test');
});

cy.get('#wpadminbar li#wp-admin-bar-debug-bar').click();
cy.get('#querylist').should('be.visible');
});
});
5 changes: 5 additions & 0 deletions tests/cypress/support/global-hooks.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
window.indexNames = null;
window.isEpIo = false;

before(() => {
cy.wpCliEval(
Expand All @@ -24,6 +25,10 @@ before(() => {
).then((wpCliResponse) => {
window.indexNames = JSON.parse(wpCliResponse.stdout);
});

cy.wpCli('eval "echo (int) \\ElasticPress\\Utils\\is_epio();"').then((response) => {
window.isEpIo = response.stdout === '1';
});
});

afterEach(() => {
Expand Down

0 comments on commit 5afa20d

Please sign in to comment.