• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

visgl / react-map-gl / 6152798188

11 Sep 2023 11:16PM UTC coverage: 81.193% (-0.2%) from 81.438%
6152798188

Pull #2280

github

web-flow
Merge ac5386748 into f903e4459
Pull Request #2280: Use shadow transform for queryRenderedFeatures call

270 of 335 branches covered (0.0%)

Branch coverage included in aggregate %.

11 of 11 new or added lines in 2 files covered. (100.0%)

2031 of 2499 relevant lines covered (81.27%)

12.43 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.95
/src/mapbox/create-ref.ts
1
import type {
1✔
2
  MapInstance,
1✔
3
  MapInstanceInternal,
1✔
4
  MapStyle,
1✔
5
  Callbacks,
1✔
6
  LngLatLike,
1✔
7
  PointLike
1✔
8
} from '../types';
1✔
9
import type Mapbox from './mapbox';
1✔
10

1✔
11
/** These methods may break the react binding if called directly */
1✔
12
const skipMethods = [
1✔
13
  'setMaxBounds',
1✔
14
  'setMinZoom',
1✔
15
  'setMaxZoom',
1✔
16
  'setMinPitch',
1✔
17
  'setMaxPitch',
1✔
18
  'setRenderWorldCopies',
1✔
19
  'setProjection',
1✔
20
  'setStyle',
1✔
21
  'addSource',
1✔
22
  'removeSource',
1✔
23
  'addLayer',
1✔
24
  'removeLayer',
1✔
25
  'setLayerZoomRange',
1✔
26
  'setFilter',
1✔
27
  'setPaintProperty',
1✔
28
  'setLayoutProperty',
1✔
29
  'setLight',
1✔
30
  'setTerrain',
1✔
31
  'setFog',
1✔
32
  'remove'
1✔
33
] as const;
1✔
34

1✔
35
export type MapRef<MapT extends MapInstance> = {
1✔
36
  getMap(): MapT;
1✔
37
} & Omit<MapT, typeof skipMethods[number]>;
1✔
38

1✔
39
export default function createRef<
11✔
40
  StyleT extends MapStyle,
11✔
41
  CallbacksT extends Callbacks,
11✔
42
  MapT extends MapInstance
11✔
43
>(mapInstance: Mapbox<StyleT, CallbacksT, MapT>): MapRef<MapT> | null {
11✔
44
  if (!mapInstance) {
11!
45
    return null;
×
46
  }
×
47

11✔
48
  const map = mapInstance.map as MapInstanceInternal<MapT>;
11✔
49
  const result: any = {
11✔
50
    getMap: () => map,
11✔
51

11✔
52
    // Overwrite getters to use our shadow transform
11✔
53
    getCenter: () => mapInstance.transform.center,
11✔
54
    getZoom: () => mapInstance.transform.zoom,
11✔
55
    getBearing: () => mapInstance.transform.bearing,
11✔
56
    getPitch: () => mapInstance.transform.pitch,
11✔
57
    getPadding: () => mapInstance.transform.padding,
11✔
58
    getBounds: () => mapInstance.transform.getBounds(),
11✔
59
    project: (lnglat: LngLatLike) => {
11✔
60
      const tr = map.transform;
×
61
      map.transform = mapInstance.transform;
×
62
      const result = map.project(lnglat);
×
63
      map.transform = tr;
×
64
      return result;
×
65
    },
11✔
66
    unproject: (point: PointLike) => {
11✔
67
      const tr = map.transform;
×
68
      map.transform = mapInstance.transform;
×
69
      const result = map.unproject(point);
×
70
      map.transform = tr;
×
71
      return result;
×
72
    },
11✔
73
    // options diverge between mapbox and maplibre
11✔
74
    queryTerrainElevation: (lnglat: LngLatLike, options?: any) => {
11✔
75
      const tr = map.transform;
×
76
      map.transform = mapInstance.transform;
×
77
      const result = map.queryTerrainElevation(lnglat, options);
×
78
      map.transform = tr;
×
79
      return result;
×
80
    },
11✔
81
    queryRenderedFeatures: (geometry?: any, options?: any) => {
11✔
82
      const tr = map.transform;
×
83
      map.transform = mapInstance.transform;
×
84
      const result = map.queryRenderedFeatures(geometry, options);
×
85
      map.transform = tr;
×
86
      return result;
×
87
    }
×
88
  };
11✔
89

11✔
90
  for (const key of getMethodNames(map)) {
11✔
91
    // @ts-expect-error
418✔
92
    if (!(key in result) && !skipMethods.includes(key)) {
418✔
93
      result[key] = map[key].bind(map);
176✔
94
    }
176✔
95
  }
418✔
96

11✔
97
  return result;
11✔
98
}
11✔
99

1✔
100
function getMethodNames(obj: Object) {
11✔
101
  const result = new Set<string>();
11✔
102

11✔
103
  let proto = obj;
11✔
104
  while (proto) {
11✔
105
    for (const key of Object.getOwnPropertyNames(proto)) {
44✔
106
      if (
715✔
107
        key[0] !== '_' &&
715✔
108
        typeof obj[key] === 'function' &&
715✔
109
        key !== 'fire' &&
715✔
110
        key !== 'setEventedParent'
451✔
111
      ) {
715✔
112
        result.add(key);
440✔
113
      }
440✔
114
    }
715✔
115
    proto = Object.getPrototypeOf(proto);
44✔
116
  }
44✔
117
  return Array.from(result);
11✔
118
}
11✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc