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

Improvements on end-to-end tests #2840

Merged
merged 4 commits into from
Jun 14, 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
2 changes: 1 addition & 1 deletion tests/cypress/integration/dashboard-sync.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe('Dashboard Sync', () => {

// Sync and remove, so EP doesn't think it is a fresh install.
cy.wpCli('wp elasticpress index --setup --yes');
cy.wpCli('wp elasticpress delete-index --yes');
cy.wpCli('wp elasticpress delete-index --yes --network-wide');

cy.visitAdminPage('network/admin.php?page=elasticpress-health');
cy.get('.wrap').should(
Expand Down
77 changes: 42 additions & 35 deletions tests/cypress/integration/features/facets.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,58 +6,65 @@ describe('Facets Feature', () => {
* @param {string} category The category slug.
*/
function createWidget(title, category) {
cy.intercept('/wp-json/wp/v2/widget-types/*/encode*').as('legacyWidgets');
cy.openWidgetsPage();

cy.get('.edit-widgets-header-toolbar__inserter-toggle').click();
cy.get('.block-editor-inserter__panel-content [class*="legacy-widget/ep-facet"]').click({
force: true,
let createdWidgetsLength = 0;

cy.get('.is-opened').then(($openedWidgetArea) => {
createdWidgetsLength = $openedWidgetArea.find('.wp-block-legacy-widget').length;

cy.get('.edit-widgets-header-toolbar__inserter-toggle').click();
cy.get('.block-editor-inserter__panel-content [class*="legacy-widget/ep-facet"]')
.click({
force: true,
})
.then(() => {
cy.get(
`.is-opened .wp-block-legacy-widget:eq(${createdWidgetsLength}) .wp-block-legacy-widget__edit-form:visible .widefat:visible`,
{ timeout: 10000 },
)
.closest('.widget-ep-facet')
.within(() => {
cy.get('input[name^="widget-ep-facet"][name$="[title]"]').clearThenType(
title,
true,
);
cy.get('select[name^="widget-ep-facet"][name$="[facet]"]').select(
category,
);
});

/**
* Wait for WordPress to recognize the title typed.
*
* @todo investigate why this is needed.
*/
// eslint-disable-next-line cypress/no-unnecessary-waiting
cy.wait(2000);

cy.get('.edit-widgets-header__actions .components-button.is-primary').click();
cy.get('body').should('contain.text', 'Widgets saved.');
});
});
cy.wait('@legacyWidgets');
// eslint-disable-next-line cypress/no-unnecessary-waiting -- JS processing
cy.wait(1000);

cy.get('.is-opened .widget-ep-facet')
.last()
.within(() => {
cy.get('input[name^="widget-ep-facet"][name$="[title]"]').clearThenType(
title,
true,
);
cy.get('select[name^="widget-ep-facet"][name$="[facet]"]').select(category);
});

/**
* Wait for WordPress to recognize the title typed.
*
* @todo investigate why this is needed.
*/
// eslint-disable-next-line cypress/no-unnecessary-waiting
cy.wait(2000);

cy.get('.edit-widgets-header__actions .components-button.is-primary').click();
cy.get('body').should('contain.text', 'Widgets saved.');
}

before(() => {
cy.maybeEnableFeature('facets');

cy.wpCli('widget reset --all');
cy.wpCli('elasticpress index --setup --yes');

// Initial widget that will be used for all tests.
createWidget('Facet (categories)', 'category');
});

it('Can see the widget in the frontend', () => {
cy.wpCli('widget reset --all');

createWidget('Facet (categories)', 'category');

cy.visit('/');

// Check if the widget is visible.
// Check if the first widget is visible.
cy.get('.widget_ep-facet').should('be.visible');
cy.contains('.widget-title', 'Facet (categories)').should('be.visible');
});

it('Can use widgets', () => {
// Create a second widget, so we can test both working together.
createWidget('Facet (Tags)', 'post_tag');

Expand Down
32 changes: 1 addition & 31 deletions tests/cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,37 +158,7 @@ Cypress.Commands.add('publishPost', (postData) => {
});

Cypress.Commands.add('updateFeatures', (newFeaturesValues = {}) => {
const features = {
search: {
active: 1,
highlight_enabled: true,
highlight_excerpt: true,
highlight_tag: 'mark',
highlight_color: '#157d84',
},
related_posts: {
active: 1,
},
facets: {
active: 1,
},
searchordering: {
active: 1,
},
autosuggest: {
active: 1,
},
woocommerce: {
active: 0,
},
protected_content: {
active: 0,
},
users: {
active: 0,
},
...newFeaturesValues,
};
const features = Object.assign({}, cy.elasticPress.defaultFeatures, ...newFeaturesValues);

const escapedFeatures = JSON.stringify(features);

Expand Down
54 changes: 21 additions & 33 deletions tests/cypress/support/global-hooks.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,29 @@
window.indexNames = null;

let setFeatures = false;

before(() => {
// Clear sync from previous tests.
cy.wpCli('wp elasticpress clear-index');
cy.wpCliEval(
`
// Clear any stuck sync process.
\\ElasticPress\\IndexHelper::factory()->clear_index_meta();

if (!window.indexNames) {
cy.wpCli('wp elasticpress get-indexes').then((wpCliResponse) => {
window.indexNames = JSON.parse(wpCliResponse.stdout);
});
}
$features = json_decode( '${JSON.stringify(cy.elasticPress.defaultFeatures)}', true );

if ( ! \\ElasticPress\\Utils\\is_epio() ) {
$host = \\ElasticPress\\Utils\\get_host();
$host = str_replace( '172.17.0.1', 'localhost', $host );
$index_name = \\ElasticPress\\Indexables::factory()->get( 'post' )->get_index_name();
$as_endpoint_url = $host . $index_name . '/_search';

$features['autosuggest']['endpoint_url'] = $as_endpoint_url;
}

if (!setFeatures) {
cy.wpCliEval(
`if ( \\ElasticPress\\Utils\\is_epio() ) {
exit;
}
$host = \\ElasticPress\\Utils\\get_host();
$host = str_replace( '172.17.0.1', 'localhost', $host );
$index_name = \\ElasticPress\\Indexables::factory()->get( 'post' )->get_index_name();
echo $host . $index_name . '/_search';
`,
).then((searchEndpointUrl) => {
if (searchEndpointUrl.stdout === '') {
cy.updateFeatures();
} else {
cy.updateFeatures({
autosuggest: {
active: 1,
endpoint_url: searchEndpointUrl.stdout,
},
});
}
});
setFeatures = true;
}
update_option( 'ep_feature_settings', $features );

WP_CLI::runcommand('elasticpress get-indexes');
`,
).then((wpCliResponse) => {
window.indexNames = JSON.parse(wpCliResponse.stdout);
});
});

afterEach(() => {
Expand Down
33 changes: 33 additions & 0 deletions tests/cypress/support/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,36 @@ import './global-hooks';

// Alternatively you can use CommonJS syntax:
// require('./commands')

cy.elasticPress = {
defaultFeatures: {
search: {
active: 1,
highlight_enabled: true,
highlight_excerpt: true,
highlight_tag: 'mark',
highlight_color: '#157d84',
},
related_posts: {
active: 1,
},
facets: {
active: 1,
},
searchordering: {
active: 1,
},
autosuggest: {
active: 1,
},
woocommerce: {
active: 0,
},
protected_content: {
active: 0,
},
users: {
active: 0,
},
},
};