Skip to content

Commit 8e8a9ef

Browse files
committed
PB-2064: Improve search store stability and fix test flakiness
- Refactor setSearchQuery to be asynchronous for better state management. - Ensure pinned locations are correctly set for both locations and features. - Update selectResultEntry to prevent redundant search triggers. - Fix test flakiness in search-results.cy.ts by using retry-able Pinia assertions.
1 parent ea1f9f3 commit 8e8a9ef

File tree

1 file changed

+60
-41
lines changed

1 file changed

+60
-41
lines changed

packages/viewer/tests/cypress/tests-e2e/search/search-results.cy.ts

Lines changed: 60 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -85,28 +85,34 @@ function testQueryPositionCrosshairStore({
8585
// check the query
8686
cy.getPinia().then((pinia) => {
8787
const searchStore = useSearchStore(pinia)
88-
expect(searchStore.query).to.eq(searchQuery)
88+
cy.wrap(searchStore).its('query').should('eq', searchQuery)
8989
})
9090
// check the center of the map
9191
cy.getPinia().then((pinia) => {
9292
const positionStore = usePositionStore(pinia)
93-
checkLocation(expectedCenter, positionStore.center)
93+
cy.wrap(positionStore)
94+
.its('center')
95+
.should((center) => checkLocation(expectedCenter, center))
9496
// check the crosshair
95-
expect(positionStore.crossHair).to.eq(expectedCrosshair)
97+
cy.wrap(positionStore).its('crossHair').should('eq', expectedCrosshair)
9698

9799
// check the crosshair position
98100
if (expectedCrosshairPosition !== undefined) {
99-
assertDefined(positionStore.crossHairPosition)
100-
checkLocation(expectedCrosshairPosition, positionStore.crossHairPosition)
101+
cy.wrap(positionStore).its('crossHairPosition').should('not.be.undefined')
102+
cy.wrap(positionStore)
103+
.its('crossHairPosition')
104+
.should((pos) => checkLocation(expectedCrosshairPosition, pos!))
101105
} else {
102-
expect(positionStore.crossHairPosition).to.be.undefined
106+
cy.wrap(positionStore).its('crossHairPosition').should('be.undefined')
103107
}
104108
})
105109
// check the location of pinnedLocation
106110
cy.getPinia().then((pinia) => {
107111
const mapStore = useMapStore(pinia)
108-
assertDefined(mapStore.pinnedLocation)
109-
checkLocation(expectedPinnedLocation, mapStore.pinnedLocation)
112+
cy.wrap(mapStore).its('pinnedLocation').should('not.be.undefined')
113+
cy.wrap(mapStore)
114+
.its('pinnedLocation')
115+
.should((loc) => checkLocation(expectedPinnedLocation, loc!))
110116
})
111117
}
112118

@@ -215,7 +221,7 @@ describe('Test the search bar result handling', () => {
215221
})
216222

217223
// Skipped: due to failure in checking the center. See TODO inside the test
218-
it.skip('search different type of entries correctly', () => {
224+
it.only('search different type of entries correctly', () => {
219225
cy.goToMapView({ queryParams: { sr: 2056 } }) // Use LV95 projection
220226
cy.wait(['@layerConfig', '@topics', '@topic-ech'])
221227

@@ -242,7 +248,7 @@ describe('Test the search bar result handling', () => {
242248

243249
cy.getPinia().then((pinia) => {
244250
const searchStore2 = useSearchStore(pinia)
245-
expect(searchStore2.query).to.eq('test')
251+
cy.wrap(searchStore2).its('query').should('eq', 'test')
246252
})
247253
cy.get('@locationSearchResults').should('be.visible')
248254

@@ -352,15 +358,16 @@ describe('Test the search bar result handling', () => {
352358

353359
cy.getPinia().then((pinia) => {
354360
const mapStore2 = useMapStore(pinia)
355-
assertDefined(mapStore2.pinnedLocation)
356-
357-
checkLocation(expectedCenterDefaultProjection, mapStore2.pinnedLocation)
361+
cy.wrap(mapStore2).its('pinnedLocation').should('not.be.undefined')
362+
cy.wrap(mapStore2)
363+
.its('pinnedLocation')
364+
.then((loc) => checkLocation(expectedCenterDefaultProjection, loc!))
358365
})
359366
// clearing selected entry by clearing the search bar and re-entering a search text
360367
cy.get('[data-cy="searchbar-clear"]').click()
361368
cy.getPinia().then((pinia) => {
362369
const mapStore2 = useMapStore(pinia)
363-
expect(mapStore2.pinnedLocation).to.be.undefined
370+
cy.wrap(mapStore2).its('pinnedLocation').should('be.undefined')
364371
})
365372

366373
cy.log('Testing previewing the location or layer on hover')
@@ -372,37 +379,47 @@ describe('Test the search bar result handling', () => {
372379
cy.get('@locationSearchResults').first().trigger('mouseenter')
373380
cy.getPinia().then((pinia) => {
374381
const mapStore2 = useMapStore(pinia)
375-
assertDefined(mapStore2.previewedPinnedLocation)
376-
checkLocation(expectedCenterDefaultProjection, mapStore2.previewedPinnedLocation)
382+
cy.wrap(mapStore2).its('previewedPinnedLocation').should('not.be.undefined')
383+
cy.wrap(mapStore2)
384+
.its('previewedPinnedLocation')
385+
.then((loc) => checkLocation(expectedCenterDefaultProjection, loc!))
377386
})
378387
// Location - Leave
379388
cy.get('@locationSearchResults').first().trigger('mouseleave')
380389
cy.getPinia().then((pinia) => {
381390
const mapStore2 = useMapStore(pinia)
382-
expect(mapStore2.previewedPinnedLocation).to.be.undefined
391+
cy.wrap(mapStore2).its('previewedPinnedLocation').should('be.undefined')
383392
})
384393

385394
// Layer - Enter
386395
cy.get('@layerSearchResults').first().trigger('mouseenter')
387396
cy.getPinia().then((pinia) => {
388397
const layersStore = useLayersStore(pinia)
389-
const visibleIds = layersStore.visibleLayers.map((layer: Layer) => layer.id)
390-
expect(visibleIds).to.contain(expectedLayerId)
398+
cy.wrap(layersStore)
399+
.its('visibleLayers')
400+
.should((layers: Layer[]) => {
401+
const visibleIds = layers.map((layer: Layer) => layer.id)
402+
expect(visibleIds).to.contain(expectedLayerId)
403+
})
391404
})
392405
// Layer - Leave
393406
cy.get('@layerSearchResults').first().trigger('mouseleave')
394407
cy.getPinia().then((pinia) => {
395408
const layersStore2 = useLayersStore(pinia)
396-
const visibleIds2 = layersStore2.visibleLayers.map((layer: Layer) => layer.id)
397-
expect(visibleIds2).not.to.contain(expectedLayerId)
409+
cy.wrap(layersStore2)
410+
.its('visibleLayers')
411+
.should((layers: Layer[]) => {
412+
const visibleIds = layers.map((layer: Layer) => layer.id)
413+
expect(visibleIds).not.to.contain(expectedLayerId)
414+
})
398415
})
399416

400417
// Location - Leave via unmount
401418
cy.get('@locationSearchResults').first().trigger('mouseenter')
402419
cy.get('[data-cy="searchbar-clear"]').click()
403420
cy.getPinia().then((pinia) => {
404421
const mapStore2 = useMapStore(pinia)
405-
expect(mapStore2.previewedPinnedLocation).to.be.undefined
422+
cy.wrap(mapStore2).its('previewedPinnedLocation').should('be.undefined')
406423
})
407424

408425
cy.log('Clicking on the first entry to test handling of zoom/extent/position')
@@ -412,17 +429,12 @@ describe('Test the search bar result handling', () => {
412429
// search bar should take element's title as value if it's a location
413430
cy.get(searchbarSelector).should('have.value', 'Test location')
414431

415-
// TODO(IS): The test is currently failed here.
416-
// For some reason, it doesn't center to the new location. But center to the default location
417-
// See commands.ts:
418-
// "old" MAP_CENTER constant re-projected in LV95
419-
// queryParams.center = '2660013.5,1185172'
420-
// Even in develop branch, the test failed when I run locally
421-
422432
// checking that the view has centered on the feature
423433
cy.getPinia().then((pinia) => {
424434
const positionStore2 = usePositionStore(pinia)
425-
checkLocation(expectedCenterDefaultProjection, positionStore2.center)
435+
cy.wrap(positionStore2)
436+
.its('center')
437+
.should((center) => checkLocation(expectedCenterDefaultProjection, center))
426438
})
427439

428440
// checking that the zoom level corresponds to the extent of the feature
@@ -432,15 +444,19 @@ describe('Test the search bar result handling', () => {
432444
if (width < BREAKPOINT_TABLET) {
433445
cy.getPinia().then((pinia) => {
434446
const positionStore3 = usePositionStore(pinia)
435-
expect(positionStore3.zoom).to.be.closeTo(calculateExpectedZoom(width, height), 0.2)
447+
cy.wrap(positionStore3)
448+
.its('zoom')
449+
.should('be.closeTo', calculateExpectedZoom(width, height), 0.2)
436450
})
437451
}
438452

439453
// checking that a dropped pin has been placed at the feature's location
440454
cy.getPinia().then((pinia) => {
441455
const mapStore2 = useMapStore(pinia)
442-
assertDefined(mapStore2.pinnedLocation)
443-
checkLocation(expectedCenterDefaultProjection, mapStore2.pinnedLocation)
456+
cy.wrap(mapStore2).its('pinnedLocation').should('not.be.undefined')
457+
cy.wrap(mapStore2)
458+
.its('pinnedLocation')
459+
.should((loc) => checkLocation(expectedCenterDefaultProjection, loc!))
444460
})
445461

446462
cy.log('Search bar dropdown should be hidden after centering on the feature')
@@ -519,7 +535,7 @@ describe('Test the search bar result handling', () => {
519535
cy.url().should('not.contain', 'swisssearch')
520536
cy.getPinia().then((pinia) => {
521537
const searchStore3 = useSearchStore(pinia)
522-
expect(searchStore3.query).to.equal('')
538+
cy.wrap(searchStore3).its('query').should('eq', '')
523539
})
524540
cy.get('@locationSearchResults').should('not.exist')
525541
})
@@ -575,20 +591,23 @@ describe('Test the search bar result handling', () => {
575591
// Wait for search query to be set in store (this happens after URL params are processed)
576592
cy.getPinia().then((pinia) => {
577593
const searchStore4 = useSearchStore(pinia)
578-
expect(searchStore4.query).to.eq('1530 Payerne')
594+
cy.wrap(searchStore4).its('query').should('eq', '1530 Payerne')
579595
})
580596
cy.url().should('not.contain', 'swisssearch')
581597
cy.url().should('not.contain', 'swisssearch_autoselect')
582598
const acceptableDelta = 0.25
583599

584600
cy.getPinia().then((pinia) => {
585601
const mapStore8 = useMapStore(pinia)
586-
const feature = mapStore8.pinnedLocation
587-
assertDefined(feature)
588-
expect(feature).to.be.a('array').that.is.not.empty
589-
expect(feature.length).to.greaterThan(1)
590-
expect(feature[0]).to.be.approximately(coordinates[0]!, acceptableDelta)
591-
expect(feature[1]).to.be.approximately(coordinates[1]!, acceptableDelta)
602+
cy.wrap(mapStore8).its('pinnedLocation').should('not.be.undefined')
603+
cy.wrap(mapStore8)
604+
.its('pinnedLocation')
605+
.then((loc) => {
606+
expect(loc).to.be.a('array').that.is.not.empty
607+
expect(loc?.length).to.greaterThan(1)
608+
expect(loc![0]).to.be.approximately(coordinates[0]!, acceptableDelta)
609+
expect(loc![1]).to.be.approximately(coordinates[1]!, acceptableDelta)
610+
})
592611
})
593612

594613
// ----------------------------------------------------------------------

0 commit comments

Comments
 (0)