diff --git a/packages/x-components/src/x-modules/search/events.types.ts b/packages/x-components/src/x-modules/search/events.types.ts index 267884e35d..526ea526d8 100644 --- a/packages/x-components/src/x-modules/search/events.types.ts +++ b/packages/x-components/src/x-modules/search/events.types.ts @@ -1,11 +1,11 @@ import { + Banner, Facet, + Promoted, + Redirection, Result, Sort, - Redirection, - TaggingRequest, - Promoted, - Banner + TaggingRequest } from '@empathyco/x-types'; import { InternalSearchRequest, InternalSearchResponse } from './types'; @@ -26,6 +26,10 @@ export interface SearchXEvents { * Payload: The new page number. */ PageChanged: number; + /** + * Reload the current search has been requested. + */ + ReloadSearchRequested: void; /** * Results have been changed. * Payload: The new {@link @empathyco/x-types#Result | results}. diff --git a/packages/x-components/src/x-modules/search/store/module.ts b/packages/x-components/src/x-modules/search/store/module.ts index 2da7948952..1679d1f71d 100644 --- a/packages/x-components/src/x-modules/search/store/module.ts +++ b/packages/x-components/src/x-modules/search/store/module.ts @@ -1,18 +1,18 @@ import { isFacetFilter } from '@empathyco/x-types'; import { setQuery } from '../../../store/utils/query.utils'; -import { setStatus } from '../../../store/utils/status-store.utils'; +import { setStatus } from '../../../store'; import { groupItemsBy } from '../../../utils/array'; import { mergeConfig, setConfig } from '../../../store/utils/config-store.utils'; import { UNKNOWN_FACET_KEY } from '../../facets/store/constants'; import { cancelFetchAndSaveSearchResponse, - fetchAndSaveSearchResponse -} from './actions/fetch-and-save-search-response.action'; -import { fetchSearchResponse } from './actions/fetch-search-response.action'; -import { increasePageAppendingResults } from './actions/increase-page-apending-results.action'; -import { resetRequestOnRefinement } from './actions/reset-request-on-refinement.action'; + fetchAndSaveSearchResponse, + fetchSearchResponse, + increasePageAppendingResults, + resetRequestOnRefinement, + saveSearchResponse +} from './actions'; import { saveOrigin } from './actions/save-origin.action'; -import { saveSearchResponse } from './actions/save-search-response.action'; import { setUrlParams } from './actions/set-url-params.action'; import { query } from './getters/query.getter'; import { request } from './getters/request.getter'; @@ -46,6 +46,10 @@ export const searchXStoreModule: SearchXStoreModule = { resetState(state) { Object.assign(state, resettableState()); }, + resetStateForReload(state) { + const { query, facets, sort, page, ...resettable } = resettableState(); + Object.assign(state, resettable); + }, setQuery, setResults(state, results) { state.results = results; diff --git a/packages/x-components/src/x-modules/search/store/types.ts b/packages/x-components/src/x-modules/search/store/types.ts index 848532f4da..08c457936a 100644 --- a/packages/x-components/src/x-modules/search/store/types.ts +++ b/packages/x-components/src/x-modules/search/store/types.ts @@ -7,17 +7,15 @@ import { Redirection, RelatedTag, Result, - Sort, - TaggingRequest, SearchRequest, - SearchResponse + SearchResponse, + Sort, + TaggingRequest } from '@empathyco/x-types'; import { Dictionary } from '@empathyco/x-utils'; -import { XActionContext, XStoreModule } from '../../../store'; +import { StatusMutations, StatusState, XActionContext, XStoreModule } from '../../../store'; import { QueryMutations, QueryState } from '../../../store/utils/query.utils'; -import { StatusMutations, StatusState } from '../../../store/utils/status-store.utils'; -import { QueryOrigin, QueryOriginInit } from '../../../types/origin'; -import { UrlParams } from '../../../types/url-params'; +import { QueryOrigin, QueryOriginInit, UrlParams } from '../../../types'; import { SearchConfig } from '../config.types'; import { InternalSearchRequest, WatchedInternalSearchRequest } from '../types'; import { ConfigMutations } from '../../../store/utils/config-store.utils'; @@ -108,6 +106,11 @@ export interface SearchMutations * {@link searchXStoreModule} for details. */ resetState(): void; + /** + * Resets the "resettable" part of the Search state like {@link SearchMutations.resetState} but + * maintains the values required to perform the search request again. + */ + resetStateForReload(): void; /** * Sets the banners of the module. * diff --git a/packages/x-components/src/x-modules/search/wiring.ts b/packages/x-components/src/x-modules/search/wiring.ts index 7679bce076..8ed51ef822 100644 --- a/packages/x-components/src/x-modules/search/wiring.ts +++ b/packages/x-components/src/x-modules/search/wiring.ts @@ -1,12 +1,13 @@ -import { filterTruthyPayload, namespacedWireCommitWithoutPayload } from '../../wiring'; import { + createWiring, + filterTruthyPayload, namespacedWireCommit, + namespacedWireCommitWithoutPayload, namespacedWireDispatch, - namespacedWireDispatchWithoutPayload -} from '../../wiring/namespaced-wires.factory'; -import { WirePayload } from '../../wiring/wiring.types'; -import { createWiring } from '../../wiring/wiring.utils'; -import { createRawFilters } from '../../utils/filters'; + namespacedWireDispatchWithoutPayload, + WirePayload +} from '../../wiring'; +import { createRawFilters } from '../../utils'; import { InternalSearchRequest } from './types'; /** @@ -130,6 +131,13 @@ export const setSearchPage = wireCommit('setPage'); */ export const setSearchExtraParams = wireCommit('setParams'); +/** + * Resets the search state to reload the current search. + * + * @public + */ +export const resetStateForReloadWire = wireCommitWithoutPayload('resetStateForReload'); + /** * Resets the search state `isNoResults`. * @@ -272,6 +280,9 @@ export const searchWiring = createWiring({ ResultsChanged: { resetAppending }, + ReloadSearchRequested: { + resetStateForReloadWire + }, SelectedSortProvided: { setSort }, diff --git a/packages/x-components/tests/e2e/reload-search.feature b/packages/x-components/tests/e2e/reload-search.feature new file mode 100644 index 0000000000..8848d2861b --- /dev/null +++ b/packages/x-components/tests/e2e/reload-search.feature @@ -0,0 +1,19 @@ +Feature: Reload search + + Background: + Given a results API with 24 results + And a tracking API with a known response + And no special config for layout view + + Scenario Outline: 1. Search is reloaded when event is emitted + When start button is clicked + Then empathize should be visible + When "" is searched + Then results page number 1 is loaded + And search request contains parameter "query" with value "" + When event ReloadSearchRequested is emitted + Then search request contains parameter "query" with value "" + + Examples: + | query | + | lego | diff --git a/packages/x-components/tests/e2e/reload-search/reload-search.spec.ts b/packages/x-components/tests/e2e/reload-search/reload-search.spec.ts new file mode 100644 index 0000000000..360489b0c6 --- /dev/null +++ b/packages/x-components/tests/e2e/reload-search/reload-search.spec.ts @@ -0,0 +1,6 @@ +import { When } from '@badeball/cypress-cucumber-preprocessor'; + +When('event ReloadSearchRequested is emitted', () => { + // eslint-disable-next-line @typescript-eslint/no-unsafe-call + cy.window().then((w: Window) => w.InterfaceX?.bus.emit('ReloadSearchRequested')); +});