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

Fix search terms clearing post type filter from forms. #3891

Merged
merged 5 commits into from
Apr 10, 2024
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
9 changes: 8 additions & 1 deletion assets/js/api-search/src/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,15 @@ export default (state, action) => {
break;
}
case 'SEARCH': {
newState.args = { ...newState.args, ...action.args, offset: 0 };
const { updateDefaults, ...args } = action.args;

newState.args = { ...newState.args, ...args, offset: 0 };
newState.isOn = true;

if (updateDefaults && args.post_type.length) {
newState.argsSchema.post_type.default = args.post_type;
}

break;
}
case 'SEARCH_FOR': {
Expand Down
4 changes: 3 additions & 1 deletion assets/js/instant-results/apps/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { __ } from '@wordpress/i18n';
* Internal dependencies.
*/
import { useApiSearch } from '../../api-search';
import { facets } from '../config';
import { getPostTypesFromForm } from '../utilities';
import Modal from '../components/common/modal';
import Layout from '../components/layout';
Expand Down Expand Up @@ -60,8 +61,9 @@ export default () => {

const { value } = inputRef.current;
const post_type = getPostTypesFromForm(inputRef.current.form);
const updateDefaults = !facets.some((f) => f.name === 'post_type');

search({ post_type, search: value });
search({ post_type, search: value, updateDefaults });
},
[inputRef, search],
);
Expand Down
69 changes: 69 additions & 0 deletions tests/cypress/integration/features/instant-results.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ describe('Instant Results Feature', { tags: '@slow' }, () => {
cy.wait('@sidebarsRest');
}

/**
* Create a Product Search widget.
*/
function createProductSearchWidget() {
cy.openWidgetsPage();
cy.openBlockInserter();
cy.getBlocksList().should('contain.text', 'Product Search'); // Checking if it exists give JS time to process the full list.
cy.insertBlock('Product Search');
cy.intercept('/wp-json/wp/v2/sidebars/*').as('sidebarsRest');
cy.get('.edit-widgets-header__actions button').contains('Update').click();
cy.wait('@sidebarsRest');
}

before(() => {
cy.deactivatePlugin('classic-widgets woocommerce', 'wpCli');
createSearchWidget();
Expand Down Expand Up @@ -366,6 +379,62 @@ describe('Instant Results Feature', { tags: '@slow' }, () => {
cy.wait('@apiRequest');
cy.get('.ep-search-suggestion a').should('have.text', 'wordpress');
});

it('Is possible to set the default post type from a search form', () => {
cy.maybeEnableFeature('instant-results');

createProductSearchWidget();

/**
* If the Post Type filter is in use, entering a new search
* term should reset post type the filter.
*/
cy.visitAdminPage('admin.php?page=elasticpress');
cy.intercept('/wp-json/elasticpress/v1/features*').as('apiRequest');
cy.contains('button', 'Instant Results').click();
cy.get('.components-form-token-field__input').type(
'{backspace}{backspace}{backspace}post type{downArrow}{enter}{esc}',
);
cy.contains('button', 'Save changes').click();
cy.wait('@apiRequest');

cy.visit('/');
cy.intercept('*search=heavy*').as('apiRequest');
cy.get('.wc-block-product-search,.wp-block-search').last().as('productSearchBlock');
cy.get('@productSearchBlock').find('input[type="search"]').type('heavy{enter}');
cy.get('.ep-search-modal').should('be.visible');
cy.wait('@apiRequest');
cy.url().should('include', 'post_type=product');
cy.get('.ep-search-input').type(' duty');
cy.wait(300); // eslint-disable-line
cy.wait('@apiRequest');
cy.url().should('not.include', 'post_type=product');

/**
* If the Post Type filter is not in use, entering a new search
* term should not reset the post type filter.
*/
cy.visitAdminPage('admin.php?page=elasticpress');
cy.intercept('/wp-json/elasticpress/v1/features*').as('apiRequest');
cy.contains('button', 'Instant Results').click();
cy.contains('.components-form-token-field__token', 'Post type')
.find('button')
.click();
cy.contains('button', 'Save changes').click();
cy.wait('@apiRequest');

cy.visit('/');
cy.intercept('*search=ergo*').as('apiRequest');
cy.get('.wc-block-product-search,.wp-block-search').last().as('productSearchBlock');
cy.get('@productSearchBlock').find('input[type="search"]').type('heavy{enter}');
cy.get('.ep-search-modal').should('be.visible');
cy.wait('@apiRequest');
cy.url().should('include', 'post_type=product');
cy.get('.ep-search-input').type(' duty');
cy.wait(300); // eslint-disable-line
cy.wait('@apiRequest');
cy.url().should('include', 'post_type=product');
});
});

it('Is possible to filter the arguments schema', () => {
Expand Down
12 changes: 9 additions & 3 deletions tests/cypress/support/commands/block-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ Cypress.Commands.add('openBlockSettingsSidebar', () => {
cy.get('body').then(($el) => {
if ($el.hasClass('widgets-php')) {
cy.get('.edit-widgets-header__actions button[aria-label="Settings"]').click();
cy.get('.edit-widgets-sidebar__panel-tab').contains('Block').click();
cy.get('.edit-widgets-sidebar__panel-tab,.edit-widgets-sidebar__panel-tabs button')
.contains('Block')
.click();
} else {
cy.get('.edit-post-header__settings button[aria-label="Settings"]').click();
cy.get('.edit-post-sidebar__panel-tab').contains('Block').click();
cy.get('.edit-post-sidebar__panel-tab,.edit-post-sidebar__panel-tabs button')
.contains('Block')
.click();
}
});
});
Expand All @@ -21,7 +25,9 @@ Cypress.Commands.add('openBlockInserter', () => {
if ($body.hasClass('widgets-php')) {
cy.get('.edit-widgets-header-toolbar__inserter-toggle').click();
} else {
cy.get('.edit-post-header-toolbar__inserter-toggle').click();
cy.get(
'.edit-post-header-toolbar__inserter-toggle,.editor-document-tools__inserter-toggle',
).click();
}
});
});
Expand Down
Loading