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

terrestris / react-geo / 18554106033

16 Oct 2025 07:45AM UTC coverage: 67.411% (+0.2%) from 67.169%
18554106033

push

github

web-flow
Merge pull request #4419 from marcjansen/tackle-more-sq-issues

Tackle issues found by SonarQube

652 of 1060 branches covered (61.51%)

Branch coverage included in aggregate %.

18 of 31 new or added lines in 8 files covered. (58.06%)

1 existing line in 1 file now uncovered.

1220 of 1717 relevant lines covered (71.05%)

12.84 hits per line

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

79.69
/src/Util/TestUtil.tsx
1
import OlFeature from 'ol/Feature';
2
import OlGeomPoint from 'ol/geom/Point';
3
import OlLayerVector from 'ol/layer/Vector';
4
import OlMap from 'ol/Map';
5
import OlMapBrowserEvent from 'ol/MapBrowserEvent';
6
import OlSourceVector from 'ol/source/Vector';
7
import OlView from 'ol/View';
8

9
/**
10
 * A set of some useful static helper methods.
11
 *
12
 * @class
13
 */
14
export class TestUtil {
15

16
  static readonly mapDivId = 'map';
17✔
17
  static readonly mapDivHeight = 256;
17✔
18
  static readonly mapDivWidth = 256;
17✔
19

20
  /**
21
   * Creates and applies a map <div> element to the body.
22
   *
23
   * @return {Element} The mounted <div> element.
24
   */
25
  static readonly mountMapDiv = () => {
17✔
26
    const div = document.createElement('div');
126✔
27
    const style = div.style;
126✔
28

29
    style.position = 'absolute';
126✔
30
    style.left = '-1000px';
126✔
31
    style.top = '-1000px';
126✔
32
    style.width = TestUtil.mapDivWidth + 'px';
126✔
33
    style.height = TestUtil.mapDivHeight + 'px';
126✔
34
    div.id = TestUtil.mapDivId;
126✔
35

36
    document.body.appendChild(div);
126✔
37

38
    return div;
126✔
39
  };
40

41
  /**
42
   * Removes the map div element from the body.
43
   */
44
  static readonly unmountMapDiv = () => {
17✔
45
    let div = document.querySelector(`div#${TestUtil.mapDivId}`);
46✔
46
    if (!div) {
46!
47
      return;
×
48
    }
49
    const parent = div.parentNode;
46✔
50
    if (parent) {
46!
51
      parent.removeChild(div);
46✔
52
    }
53
    div = null;
46✔
54
  };
55

56
  /**
57
   * Creates an ol map.
58
   *
59
   * @param [mapOpts] Additional options for the map to create.
60
   * @return The ol map.
61
   */
62
  static readonly createMap = (mapOpts?: any) => {
17✔
63
    const source = new OlSourceVector();
126✔
64
    const layer = new OlLayerVector({source: source});
126✔
65
    const targetDiv = TestUtil.mountMapDiv();
126✔
66
    const defaultMapOpts = {
126✔
67
      target: targetDiv,
68
      layers: [layer],
69
      view: new OlView({
70
        center: [829729, 6708850],
71
        resolution: 1,
72
        resolutions: mapOpts ? mapOpts.resolutions : undefined
126✔
73
      })
74
    };
75

76
    Object.assign(defaultMapOpts, mapOpts);
126✔
77

78
    const map = new OlMap(defaultMapOpts);
126✔
79

80
    map.renderSync();
126✔
81

82
    return map;
126✔
83
  };
84

85
  /**
86
   * Removes the map.
87
   */
88
  static readonly removeMap =  (map: OlMap) => {
17✔
89
    if (map instanceof OlMap) {
46!
90
      map.dispose();
46✔
91
    }
92
    TestUtil.unmountMapDiv();
46✔
93
  };
94

95
  /**
96
   * Simulates a browser pointer event on the map viewport.
97
   * Origin: https://github.com/openlayers/openlayers/blob/master/test/spec/ol/interaction/Draw.test.js#L67
98
   *
99
   * @param map The map to use.
100
   * @param type Event type.
101
   * @param x Horizontal offset from map center.
102
   * @param y Vertical offset from map center.
103
   * @param [optShiftKey] Shift key is pressed
104
   * @param [dragging] Whether the map is being dragged or not.
105
   */
106
  static readonly simulatePointerEvent = ({map, type, x, y, optShiftKey, dragging}:
17✔
107
  {map: OlMap; type: string; x: number; y: number; optShiftKey?: boolean; dragging?: boolean}) => {
108
    const viewport = map.getViewport();
×
109
    // Calculated in case body has top < 0 (test runner with small window).
110
    const position = viewport.getBoundingClientRect();
×
NEW
111
    const shiftKey = optShiftKey ?? false;
×
112
    const event = new PointerEvent(type, {
×
113
      clientX: position.left + x + TestUtil.mapDivWidth / 2,
114
      clientY: position.top + y + TestUtil.mapDivHeight / 2,
115
      shiftKey
116
    });
117
    const olEvt = new OlMapBrowserEvent(type, map, event, dragging);
×
118
    map.handleMapBrowserEvent(olEvt);
×
119
  };
120

121
  /**
122
   * Creates and returns an empty vector layer.
123
   *
124
   * @param [properties] The properties to set.
125
   * @return {ol.layer.Vector} The layer.
126
   */
127
  static readonly createVectorLayer = (properties: any) => {
17✔
128
    const source = new OlSourceVector();
18✔
129
    const layer = new OlLayerVector({source: source});
18✔
130

131
    layer.setProperties(properties);
18✔
132

133
    return layer;
18✔
134
  };
135

136
  /**
137
   * Returns a point feature with a random position.
138
   */
139
  static readonly generatePointFeature = (props: Record<string, any> = {
17!
140
    ATTR_1: Math.random() * 100,
141
    ATTR_2: 'Borsigplatz 9',
142
    ATTR_3: 'Dortmund'
143
  }) => {
144
    const coords = [
66✔
145
      Math.floor(Math.random() * 180) - 180,
146
      Math.floor(Math.random() * 90) - 90
147
    ];
148
    const geom = new OlGeomPoint(coords);
66✔
149
    const feat = new OlFeature({
66✔
150
      geometry: geom
151
    });
152

153
    feat.setProperties(props);
66✔
154

155
    return feat;
66✔
156
  };
157
}
158

159
export default TestUtil;
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