File size: 1,990 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
export class FakeOverlayView {
  setMap = jest.fn();

  getPanes = jest.fn(() => ({
    overlayMouseTarget: {
      appendChild: jest.fn(),
    },
  }));

  getProjection = jest.fn(() => ({
    fromLatLngToDivPixel: jest.fn(() => ({
      x: 0,
      y: 0,
    })),
  }));
}

export const MockLatLngBounds = jest.fn((ne, sw) => ({
  northEast: ne,
  southWest: sw,
  equals(oldBounds) {
    if (!oldBounds) {
      return false;
    }
    return (
      oldBounds.northEast.lat === this.northEast.lat &&
      oldBounds.northEast.lng === this.northEast.lng &&
      oldBounds.southWest.lat === this.southWest.lat &&
      oldBounds.southWest.lng === this.southWest.lng
    );
  },
}));

export const createFakeMapInstance = () => ({
  addListener: jest.fn(() => ({
    remove: jest.fn(),
  })),
  getCenter: jest.fn(),
  setCenter: jest.fn(),
  getZoom: jest.fn(),
  setZoom: jest.fn(),
  getBounds: jest.fn(
    () => new MockLatLngBounds({ lat: 0, lng: 0 }, { lat: 0, lng: 0 })
  ),
  getProjection: jest.fn(() => ({
    fromPointToLatLng: jest.fn(() => ({
      lat: jest.fn(),
      lng: jest.fn(),
    })),
    fromLatLngToPoint: jest.fn(() => ({
      x: 0,
      y: 0,
    })),
  })),
  fitBounds: jest.fn(),
});

export const createFakeMarkerInstance = () => ({
  setMap: jest.fn(),
  getPosition: jest.fn(),
  addListener: jest.fn(),
});

export const createFakeHTMLMarkerInstance = () => ({
  element: document.createElement('div'),
  setMap: jest.fn(),
  draw: jest.fn(),
});

export const createFakeGoogleReference = ({
  mapInstance = createFakeMapInstance(),
  markerInstance = createFakeMarkerInstance(),
} = {}) => ({
  maps: {
    LatLng: jest.fn((x) => x),
    LatLngBounds: MockLatLngBounds,
    Map: jest.fn(() => mapInstance),
    Marker: jest.fn(() => markerInstance),
    ControlPosition: {
      LEFT_TOP: 'left:top',
    },
    event: {
      addListenerOnce: jest.fn(() => ({
        remove: jest.fn(),
      })),
    },
    OverlayView: FakeOverlayView,
  },
});