Skip to content

Commit f171b8a

Browse files
samoussbobylito
authored andcommitted
fix(geosearch): avoid reset map when it already moved (#2870)
* refactor(GeoSearchTest): update wording * fix(GeoSearch): avoid to reset the map when it already moved
1 parent e8c58cc commit f171b8a

File tree

3 files changed

+84
-7
lines changed

3 files changed

+84
-7
lines changed

dev/app/builtin/stories/geo-search.stories.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,5 +750,29 @@ export default () => {
750750
],
751751
}
752752
)
753+
)
754+
.add(
755+
'without results',
756+
wrapWithHitsAndConfiguration(
757+
(container, start) =>
758+
injectGoogleMaps(() => {
759+
container.style.height = '600px';
760+
761+
window.search.addWidget(
762+
instantsearch.widgets.geoSearch({
763+
googleReference: window.google,
764+
container,
765+
initialPosition,
766+
initialZoom,
767+
paddingBoundingBox,
768+
})
769+
);
770+
771+
start();
772+
}),
773+
{
774+
query: 'dsdsdsds',
775+
}
776+
)
753777
);
754778
};

src/widgets/geo-search/GeoSearchRenderer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ const renderer = (
155155
return;
156156
}
157157

158-
if (!items.length && !isRefinedWithMap()) {
158+
if (!items.length && !isRefinedWithMap() && !hasMapMoveSinceLastRefine()) {
159159
const initialMapPosition = position || initialPosition;
160160

161161
renderState.isUserInteraction = false;

src/widgets/geo-search/__tests__/geo-search-test.js

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ describe('GeoSearch', () => {
774774
});
775775

776776
describe('initial position', () => {
777-
it('expect to init the position from "initialPosition" when no items are available & map is not yet render', () => {
777+
it('expect to init the position from "initialPosition"', () => {
778778
const container = createContainer();
779779
const instantSearchInstance = createFakeInstantSearch();
780780
const helper = createFakeHelper();
@@ -812,7 +812,7 @@ describe('GeoSearch', () => {
812812
expect(mapInstance.setZoom).toHaveBeenCalledWith(8);
813813
});
814814

815-
it('expect to init the position from "position" when no items are available & map is not yet render', () => {
815+
it('expect to init the position from "position"', () => {
816816
const container = createContainer();
817817
const instantSearchInstance = createFakeInstantSearch();
818818
const helper = createFakeHelper();
@@ -833,9 +833,8 @@ describe('GeoSearch', () => {
833833
},
834834
});
835835

836-
// Simulate the configuration
837-
const initialState = widget.getConfiguration({});
838-
helper.setState(initialState);
836+
// Simulate the configuration for the position
837+
helper.setState(widget.getConfiguration({}));
839838

840839
widget.init({
841840
helper,
@@ -896,7 +895,7 @@ describe('GeoSearch', () => {
896895
expect(mapInstance.setZoom).not.toHaveBeenCalled();
897896
});
898897

899-
it('expect to not init the position when the refinement is coming from the map', () => {
898+
it('expect to not init the position when the refinement is from the map', () => {
900899
const container = createContainer();
901900
const instantSearchInstance = createFakeInstantSearch();
902901
const helper = createFakeHelper();
@@ -948,6 +947,60 @@ describe('GeoSearch', () => {
948947
expect(mapInstance.setCenter).not.toHaveBeenCalled();
949948
expect(mapInstance.setZoom).not.toHaveBeenCalled();
950949
});
950+
951+
it('expect to not init the position when the map has moved', () => {
952+
const container = createContainer();
953+
const instantSearchInstance = createFakeInstantSearch();
954+
const helper = createFakeHelper();
955+
const mapInstance = createFakeMapInstance();
956+
const googleReference = createFakeGoogleReference({ mapInstance });
957+
958+
const widget = geoSearch({
959+
googleReference,
960+
container,
961+
enableRefineOnMapMove: false,
962+
initialZoom: 8,
963+
initialPosition: {
964+
lat: 10,
965+
lng: 12,
966+
},
967+
});
968+
969+
widget.init({
970+
helper,
971+
instantSearchInstance,
972+
state: helper.state,
973+
});
974+
975+
simulateMapReadyEvent(googleReference);
976+
977+
expect(mapInstance.setCenter).not.toHaveBeenCalled();
978+
expect(mapInstance.setZoom).not.toHaveBeenCalled();
979+
980+
widget.render({
981+
helper,
982+
instantSearchInstance,
983+
results: {
984+
hits: [{ objectID: 123, _geoloc: true }],
985+
},
986+
});
987+
988+
// Simulate a refinement
989+
simulateEvent(mapInstance, 'dragstart');
990+
simulateEvent(mapInstance, 'center_changed');
991+
simulateEvent(mapInstance, 'idle');
992+
993+
widget.render({
994+
helper,
995+
instantSearchInstance,
996+
results: {
997+
hits: [],
998+
},
999+
});
1000+
1001+
expect(mapInstance.setCenter).not.toHaveBeenCalled();
1002+
expect(mapInstance.setZoom).not.toHaveBeenCalled();
1003+
});
9511004
});
9521005

9531006
describe('markers creation', () => {

0 commit comments

Comments
 (0)