Skip to content
This repository was archived by the owner on Dec 30, 2022. It is now read-only.

Commit c430598

Browse files
anikolskiyHaroenv
andauthored
fix(maps): leave zoom in place if the bounds did not change (#3050)
* fix map-zoom after browser-zoom * use .equals and add tests Co-authored-by: Haroen Viaene <hello@haroen.me>
1 parent 4da38b5 commit c430598

File tree

3 files changed

+118
-25
lines changed

3 files changed

+118
-25
lines changed

packages/react-instantsearch-dom-maps/src/GoogleMaps.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,21 @@ class GoogleMaps extends Component {
6767
}
6868

6969
if (boundingBox) {
70-
this.lockUserInteration(() => {
71-
this.instance.fitBounds(
72-
new google.maps.LatLngBounds(
73-
boundingBox.southWest,
74-
boundingBox.northEast
75-
),
76-
boundingBoxPadding
70+
this.lockUserInteraction(() => {
71+
const oldBounds = this.instance.getBounds();
72+
73+
// south west and north east are swapped
74+
const newBounds = new google.maps.LatLngBounds(
75+
boundingBox.southWest,
76+
boundingBox.northEast
7777
);
78+
79+
if (!newBounds.equals(oldBounds)) {
80+
this.instance.fitBounds(newBounds, boundingBoxPadding);
81+
}
7882
});
7983
} else {
80-
this.lockUserInteration(() => {
84+
this.lockUserInteraction(() => {
8185
this.instance.setZoom(initialZoom);
8286
this.instance.setCenter(initialPosition);
8387
});
@@ -124,7 +128,7 @@ class GoogleMaps extends Component {
124128
);
125129
};
126130

127-
lockUserInteration(functionThatAltersTheMapPosition) {
131+
lockUserInteraction(functionThatAltersTheMapPosition) {
128132
this.isUserInteraction = false;
129133
functionThatAltersTheMapPosition();
130134
this.isUserInteraction = true;

packages/react-instantsearch-dom-maps/src/__tests__/GoogleMaps.js

Lines changed: 85 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -358,11 +358,6 @@ describe('GoogleMaps', () => {
358358
mapInstance,
359359
});
360360

361-
google.maps.LatLngBounds.mockImplementation((sw, ne) => ({
362-
northEast: ne,
363-
southWest: sw,
364-
}));
365-
366361
const props = {
367362
...defaultProps,
368363
google,
@@ -384,6 +379,20 @@ describe('GoogleMaps', () => {
384379
wrapper.setProps({
385380
boundingBoxPadding: 0,
386381
boundingBox: {
382+
northEast: {
383+
lat: 14,
384+
lng: 14,
385+
},
386+
southWest: {
387+
lat: 10,
388+
lng: 10,
389+
},
390+
},
391+
});
392+
393+
expect(mapInstance.fitBounds).toHaveBeenCalledTimes(1);
394+
expect(mapInstance.fitBounds).toHaveBeenCalledWith(
395+
expect.objectContaining({
387396
northEast: {
388397
lat: 10,
389398
lng: 10,
@@ -392,12 +401,41 @@ describe('GoogleMaps', () => {
392401
lat: 14,
393402
lng: 14,
394403
},
395-
},
404+
}),
405+
0
406+
);
407+
408+
expect(mapInstance.setZoom).toHaveBeenCalledTimes(1); // cDM
409+
expect(mapInstance.setCenter).toHaveBeenCalledTimes(1); // cDM
410+
});
411+
412+
it('expect not to call fitBounds on didUpdate when boundingBox equal to previous', () => {
413+
const mapInstance = createFakeMapInstance();
414+
const google = createFakeGoogleReference({
415+
mapInstance,
396416
});
397417

398-
expect(mapInstance.fitBounds).toHaveBeenCalledTimes(1);
399-
expect(mapInstance.fitBounds).toHaveBeenCalledWith(
400-
{
418+
const props = {
419+
...defaultProps,
420+
google,
421+
};
422+
423+
const wrapper = shallow(
424+
<GoogleMaps {...props}>
425+
<div>This is the children</div>
426+
</GoogleMaps>
427+
);
428+
429+
simulateMapReadyEvent(google);
430+
431+
expect(mapInstance.fitBounds).toHaveBeenCalledTimes(0);
432+
433+
expect(mapInstance.setZoom).toHaveBeenCalledTimes(1); // cDM
434+
expect(mapInstance.setCenter).toHaveBeenCalledTimes(1); // cDM
435+
436+
wrapper.setProps({
437+
boundingBoxPadding: 0,
438+
boundingBox: {
401439
northEast: {
402440
lat: 10,
403441
lng: 10,
@@ -407,11 +445,49 @@ describe('GoogleMaps', () => {
407445
lng: 14,
408446
},
409447
},
448+
});
449+
450+
expect(mapInstance.fitBounds).toHaveBeenCalledTimes(1);
451+
expect(mapInstance.fitBounds).toHaveBeenCalledWith(
452+
expect.objectContaining({
453+
northEast: {
454+
lat: 14,
455+
lng: 14,
456+
},
457+
southWest: {
458+
lat: 10,
459+
lng: 10,
460+
},
461+
}),
410462
0
411463
);
412464

413465
expect(mapInstance.setZoom).toHaveBeenCalledTimes(1); // cDM
414466
expect(mapInstance.setCenter).toHaveBeenCalledTimes(1); // cDM
467+
468+
mapInstance.getBounds.mockImplementation(
469+
() =>
470+
new google.maps.LatLngBounds(
471+
{ lat: 14, lng: 14 },
472+
{ lat: 10, lng: 10 }
473+
)
474+
);
475+
476+
wrapper.setProps({
477+
boundingBoxPadding: 0,
478+
boundingBox: {
479+
northEast: {
480+
lat: 10,
481+
lng: 10,
482+
},
483+
southWest: {
484+
lat: 14,
485+
lng: 14,
486+
},
487+
},
488+
});
489+
490+
expect(mapInstance.fitBounds).toHaveBeenCalledTimes(1);
415491
});
416492

417493
it('expect to call setCenter & setZoom when boundingBox is not provided', () => {

packages/react-instantsearch-dom-maps/test/mockGoogleMaps.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,22 @@ export class FakeOverlayView {
1515
}));
1616
}
1717

18+
export const MockLatLngBounds = jest.fn((ne, sw) => ({
19+
northEast: ne,
20+
southWest: sw,
21+
equals(oldBounds) {
22+
if (!oldBounds) {
23+
return false;
24+
}
25+
return (
26+
oldBounds.northEast.lat === this.northEast.lat &&
27+
oldBounds.northEast.lng === this.northEast.lng &&
28+
oldBounds.southWest.lat === this.southWest.lat &&
29+
oldBounds.southWest.lng === this.southWest.lng
30+
);
31+
},
32+
}));
33+
1834
export const createFakeMapInstance = () => ({
1935
addListener: jest.fn(() => ({
2036
remove: jest.fn(),
@@ -23,10 +39,9 @@ export const createFakeMapInstance = () => ({
2339
setCenter: jest.fn(),
2440
getZoom: jest.fn(),
2541
setZoom: jest.fn(),
26-
getBounds: jest.fn(() => ({
27-
getNorthEast: jest.fn(),
28-
getSouthWest: jest.fn(),
29-
})),
42+
getBounds: jest.fn(
43+
() => new MockLatLngBounds({ lat: 0, lng: 0 }, { lat: 0, lng: 0 })
44+
),
3045
getProjection: jest.fn(() => ({
3146
fromPointToLatLng: jest.fn(() => ({
3247
lat: jest.fn(),
@@ -58,9 +73,7 @@ export const createFakeGoogleReference = ({
5873
} = {}) => ({
5974
maps: {
6075
LatLng: jest.fn(x => x),
61-
LatLngBounds: jest.fn(() => ({
62-
extend: jest.fn().mockReturnThis(),
63-
})),
76+
LatLngBounds: MockLatLngBounds,
6477
Map: jest.fn(() => mapInstance),
6578
Marker: jest.fn(() => markerInstance),
6679
ControlPosition: {

0 commit comments

Comments
 (0)