From 499f5baa60efc1a826e13fc5a153a05737e60917 Mon Sep 17 00:00:00 2001 From: Jacob Peattie Date: Tue, 9 Apr 2024 17:39:04 +1000 Subject: [PATCH 1/4] Fix search terms clearing post type filter from forms. --- assets/js/api-search/src/reducer.js | 9 ++- assets/js/instant-results/apps/modal.js | 4 +- .../features/instant-results.cy.js | 69 +++++++++++++++++++ 3 files changed, 80 insertions(+), 2 deletions(-) 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..1b9688187d 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('.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('.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', () => { From 9a3333d64102e385289e3859834cdab6a4893b57 Mon Sep 17 00:00:00 2001 From: Jacob Peattie Date: Wed, 10 Apr 2024 15:31:57 +1000 Subject: [PATCH 2/4] Add WP 6.5 selectors to tests. --- tests/cypress/support/commands/block-editor.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/cypress/support/commands/block-editor.js b/tests/cypress/support/commands/block-editor.js index cc0fa84629..33676db93d 100644 --- a/tests/cypress/support/commands/block-editor.js +++ b/tests/cypress/support/commands/block-editor.js @@ -4,7 +4,9 @@ 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(); @@ -21,7 +23,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(); } }); }); From 31f4465f78db1ffd517acb476f79c9ccde6a6672 Mon Sep 17 00:00:00 2001 From: Jacob Peattie Date: Wed, 10 Apr 2024 15:51:07 +1000 Subject: [PATCH 3/4] Add WP 6.5 class to tests. --- tests/cypress/support/commands/block-editor.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/cypress/support/commands/block-editor.js b/tests/cypress/support/commands/block-editor.js index 33676db93d..0c6754f3ad 100644 --- a/tests/cypress/support/commands/block-editor.js +++ b/tests/cypress/support/commands/block-editor.js @@ -9,7 +9,9 @@ Cypress.Commands.add('openBlockSettingsSidebar', () => { .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(); } }); }); From 82f40d321a5d917e28a0b2f809dae79350cd9784 Mon Sep 17 00:00:00 2001 From: Jacob Peattie Date: Wed, 10 Apr 2024 21:20:21 +1000 Subject: [PATCH 4/4] Fix test compatibility with WooCommerce 6.4. --- tests/cypress/integration/features/instant-results.cy.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/cypress/integration/features/instant-results.cy.js b/tests/cypress/integration/features/instant-results.cy.js index 1b9688187d..d863d62da7 100644 --- a/tests/cypress/integration/features/instant-results.cy.js +++ b/tests/cypress/integration/features/instant-results.cy.js @@ -400,7 +400,7 @@ describe('Instant Results Feature', { tags: '@slow' }, () => { cy.visit('/'); cy.intercept('*search=heavy*').as('apiRequest'); - cy.get('.wp-block-search').last().as('productSearchBlock'); + 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'); @@ -425,7 +425,7 @@ describe('Instant Results Feature', { tags: '@slow' }, () => { cy.visit('/'); cy.intercept('*search=ergo*').as('apiRequest'); - cy.get('.wp-block-search').last().as('productSearchBlock'); + 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');