Skip to content

Commit

Permalink
Merge pull request #3891 from 10up/fix/3868
Browse files Browse the repository at this point in the history
Fix search terms clearing post type filter from forms.
  • Loading branch information
felipeelia committed Apr 10, 2024
2 parents 0b3f637 + 82f40d3 commit eb6e7c9
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
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

0 comments on commit eb6e7c9

Please sign in to comment.