From 2a822acfdfb4dcd325e4a03d61634ed11f11c574 Mon Sep 17 00:00:00 2001 From: Felix Sommer Date: Tue, 5 Nov 2024 15:27:17 +0100 Subject: [PATCH] PB-877: fix tests --- src/api/search.api.js | 7 ++--- .../storeSync/SearchAutoSelectConfig.class.js | 23 +++++++------- .../storeSync/storeSync.routerPlugin.js | 12 ++++---- src/store/modules/search.store.js | 30 ++++++++++++------- .../cypress/tests-e2e/legacyParamImport.cy.js | 2 +- 5 files changed, 40 insertions(+), 34 deletions(-) diff --git a/src/api/search.api.js b/src/api/search.api.js index 5afe6cedd..65bf6a73e 100644 --- a/src/api/search.api.js +++ b/src/api/search.api.js @@ -197,7 +197,7 @@ function parseLocationResult(result, outputProjection) { } } -async function searchLayers(queryString, lang, cancelToken, limit = 0) { +async function searchLayers(queryString, lang, cancelToken, limit = null) { try { const layerResponse = await generateAxiosSearchRequest( queryString, @@ -226,7 +226,7 @@ async function searchLayers(queryString, lang, cancelToken, limit = 0) { * @param limit * @returns {Promise} */ -async function searchLocation(outputProjection, queryString, lang, cancelToken, limit = 0) { +async function searchLocation(outputProjection, queryString, lang, cancelToken, limit = null) { try { const locationResponse = await generateAxiosSearchRequest( queryString, @@ -439,7 +439,7 @@ export default async function search(config) { queryString = null, lang = null, layersToSearch = [], - limit = 0, + limit = null, } = config if (!(outputProjection instanceof CoordinateSystem)) { const errorMessage = `A valid output projection is required to start a search request` @@ -468,7 +468,6 @@ export default async function search(config) { searchLocation(outputProjection, queryString, lang, cancelToken, limit), ] - // TODO limit also in the local kml and gpx files ? if (layersToSearch.some((layer) => layer.searchable)) { allRequests.push( ...layersToSearch diff --git a/src/router/storeSync/SearchAutoSelectConfig.class.js b/src/router/storeSync/SearchAutoSelectConfig.class.js index 67ed5ed13..9e2bfee4c 100644 --- a/src/router/storeSync/SearchAutoSelectConfig.class.js +++ b/src/router/storeSync/SearchAutoSelectConfig.class.js @@ -1,23 +1,22 @@ import AbstractParamConfig, { STORE_DISPATCHER_ROUTER_PLUGIN, } from '@/router/storeSync/abstractParamConfig.class' +import { URL_PARAM_NAME_SWISSSEARCH } from '@/router/storeSync/SearchParamConfig.class' import { removeQueryParamFromHref } from '@/utils/searchParamUtils' -import { URL_PARAM_NAME_SWISSSEARCH } from './SearchParamConfig.class' - const URL_PARAM_NAME = 'swisssearch_autoselect' /** - * The goal is to stop centering on the search when sharing a position. When we share a position, - * both the center and the crosshair are sets. + * Dispatches the 'setAutoSelect' action to the store if the URL parameter for swisssearch is + * present. * - * @param {Object} to The route object containing the query - * @param {Object} store The store - * @param {String} urlParamValue The search param + * @param {Object} to - The target route object. + * @param {Object} store - The Vuex store instance. + * @param {string} urlParamValue - The value of the URL parameter to be dispatched. */ -function dispatchSearchFromUrl(to, store, urlParamValue) { +function dispatchAutoSelect(to, store, urlParamValue) { // avoiding setting the swisssearch autoselect to the store when there is nothing to autoselect because there is no swisssearch query if (urlParamValue && to.query[URL_PARAM_NAME_SWISSSEARCH]) { - store.dispatch('setSwisssearchAutoSelect', { + store.dispatch('setAutoSelect', { value: urlParamValue, dispatcher: STORE_DISPATCHER_ROUTER_PLUGIN, }) @@ -28,10 +27,10 @@ export default class SearchAutoSelectConfig extends AbstractParamConfig { constructor() { super({ urlParamName: URL_PARAM_NAME, - mutationsToWatch: ['setSwisssearchAutoSelect'], - setValuesInStore: dispatchSearchFromUrl, + mutationsToWatch: ['setAutoSelect'], + setValuesInStore: dispatchAutoSelect, afterSetValuesInStore: () => removeQueryParamFromHref(URL_PARAM_NAME), - extractValueFromStore: (store) => store.state.search.swisssearchAutoSelect, + extractValueFromStore: (store) => store.state.search.autoSelect, keepInUrlWhenDefault: false, valueType: Boolean, defaultValue: false, diff --git a/src/router/storeSync/storeSync.routerPlugin.js b/src/router/storeSync/storeSync.routerPlugin.js index f8d81ea79..39c6d11cf 100644 --- a/src/router/storeSync/storeSync.routerPlugin.js +++ b/src/router/storeSync/storeSync.routerPlugin.js @@ -126,14 +126,12 @@ function urlQueryWatcher(store, to, from) { } if ( - // to call afterSetValuesInStore in SearchAutoSelectConfig if it was set to false in the URL to remove the parameter from the URL - queryValue === false || // when the query value is an empty string, queryValue is false. - ((queryValue || queryValue === '') && - queryValue !== storeValue && - // if the query is undefined and the store is null, we also don't dispatch it, as we want - // to avoid changing the store value for no reason. - !(queryValue === undefined && storeValue === null)) + (queryValue || queryValue === '') && + queryValue !== storeValue && + // if the query is undefined and the store is null, we also don't dispatch it, as we want + // to avoid changing the store value for no reason. + !(queryValue === undefined && storeValue === null) ) { // dispatching URL value to the store log.debug( diff --git a/src/store/modules/search.store.js b/src/store/modules/search.store.js index 3f827993d..6ae93c6a5 100644 --- a/src/store/modules/search.store.js +++ b/src/store/modules/search.store.js @@ -35,11 +35,21 @@ const state = { * * @type {Boolean} */ - swisssearchAutoSelect: false, + autoSelect: false, } const getters = {} +/** + * Returns the appropriate result for autoselection from a list of search results. + * + * If there is only one result, it returns that result. Otherwise, it tries to find a result with + * the resultType of LOCATION. If such a result is found, it returns that result. If no result with + * resultType LOCATION is found, it returns the first result in the list. + * + * @param {SearchResult[]} results - The list of search results. + * @returns {SearchResult} - The selected search result for autoselection. + */ function getResultForAutoselect(results) { if (results.length === 1) { return results[0] @@ -50,12 +60,12 @@ function getResultForAutoselect(results) { ) // If a location result is found, return it; otherwise, return the first result - return locationResult || results[0] + return locationResult ?? results[0] } const actions = { - setSwisssearchAutoSelect: ({ commit }, { value = false, dispatcher }) => { - commit('setSwisssearchAutoSelect', { value, dispatcher }) + setAutoSelect: ({ commit }, { value = false, dispatcher }) => { + commit('setAutoSelect', { value, dispatcher }) }, /** @@ -159,11 +169,11 @@ const actions = { queryString: query, lang: rootState.i18n.lang, layersToSearch: getters.visibleLayers, - limit: state.swisssearchAutoSelect ? 1 : 0, + limit: state.autoSelect ? 1 : null, }) if ( (originUrlParam && results.length === 1) || - (originUrlParam && state.swisssearchAutoSelect && results.length >= 1) + (originUrlParam && state.autoSelect && results.length >= 1) ) { dispatch('selectResultEntry', { dispatcher: `${dispatcher}/setSearchQuery`, @@ -206,7 +216,7 @@ const actions = { queryString: state.query, lang: rootState.i18n.lang, layersToSearch: getters.visibleLayers, - limit: state.swisssearchAutoSelect ? 1 : 0, + limit: state.autoSelect ? 1 : null, }) if (resultIncludingLayerFeatures.length > state.results.length) { commit('setSearchResults', { @@ -270,8 +280,8 @@ const actions = { break } - if (state.swisssearchAutoSelect) { - dispatch('setSwisssearchAutoSelect', { + if (state.autoSelect) { + dispatch('setAutoSelect', { value: false, dispatcher: dispatcherSelectResultEntry, }) @@ -305,7 +315,7 @@ function createLayerFeature(olFeature, layer) { } const mutations = { - setSwisssearchAutoSelect: (state, { value }) => (state.swisssearchAutoSelect = value), + setAutoSelect: (state, { value }) => (state.autoSelect = value), setSearchQuery: (state, { query }) => (state.query = query), setSearchResults: (state, { results }) => (state.results = results ?? []), } diff --git a/tests/cypress/tests-e2e/legacyParamImport.cy.js b/tests/cypress/tests-e2e/legacyParamImport.cy.js index fae067bd3..3a33f566f 100644 --- a/tests/cypress/tests-e2e/legacyParamImport.cy.js +++ b/tests/cypress/tests-e2e/legacyParamImport.cy.js @@ -272,7 +272,7 @@ describe('Test on legacy param import', () => { }, false ) - cy.readStoreValue('state.search.query').should('eq', ' 1530 Payerne') + cy.readStoreValue('state.search.query').should('eq', '1530 Payerne') cy.url().should('not.contain', 'swisssearch') cy.get('[data-cy="searchbar"]').click() const acceptableDelta = 0.25