Skip to content

Commit

Permalink
PB-877: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sommerfe committed Nov 12, 2024
1 parent 6017f3e commit 2a822ac
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 34 deletions.
7 changes: 3 additions & 4 deletions src/api/search.api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -226,7 +226,7 @@ async function searchLayers(queryString, lang, cancelToken, limit = 0) {
* @param limit
* @returns {Promise<LocationSearchResult[]>}
*/
async function searchLocation(outputProjection, queryString, lang, cancelToken, limit = 0) {
async function searchLocation(outputProjection, queryString, lang, cancelToken, limit = null) {
try {
const locationResponse = await generateAxiosSearchRequest(
queryString,
Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -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
Expand Down
23 changes: 11 additions & 12 deletions src/router/storeSync/SearchAutoSelectConfig.class.js
Original file line number Diff line number Diff line change
@@ -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,
})
Expand All @@ -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,
Expand Down
12 changes: 5 additions & 7 deletions src/router/storeSync/storeSync.routerPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
30 changes: 20 additions & 10 deletions src/store/modules/search.store.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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 })
},

/**
Expand Down Expand Up @@ -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`,
Expand Down Expand Up @@ -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', {
Expand Down Expand Up @@ -270,8 +280,8 @@ const actions = {

break
}
if (state.swisssearchAutoSelect) {
dispatch('setSwisssearchAutoSelect', {
if (state.autoSelect) {
dispatch('setAutoSelect', {
value: false,
dispatcher: dispatcherSelectResultEntry,
})
Expand Down Expand Up @@ -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 ?? []),
}
Expand Down
2 changes: 1 addition & 1 deletion tests/cypress/tests-e2e/legacyParamImport.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2a822ac

Please sign in to comment.