diff --git a/assets/js/api-search/src/reducer.js b/assets/js/api-search/src/reducer.js index baa3193727..20e924ba37 100644 --- a/assets/js/api-search/src/reducer.js +++ b/assets/js/api-search/src/reducer.js @@ -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': { diff --git a/assets/js/instant-results/apps/modal.js b/assets/js/instant-results/apps/modal.js index 11e5ed82e9..648b807bcd 100644 --- a/assets/js/instant-results/apps/modal.js +++ b/assets/js/instant-results/apps/modal.js @@ -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'; @@ -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], ); diff --git a/tests/cypress/integration/features/instant-results.cy.js b/tests/cypress/integration/features/instant-results.cy.js index 999c808eb9..d863d62da7 100644 --- a/tests/cypress/integration/features/instant-results.cy.js +++ b/tests/cypress/integration/features/instant-results.cy.js @@ -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(); @@ -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', () => {