@osimatic/helpers-js 1.5.15 → 1.5.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -30,16 +30,16 @@ class OpenStreetMap {
30
30
 
31
31
  static createMap(mapContainer, options={}) {
32
32
  mapContainer = toEl(mapContainer);
33
- if (!mapContainer.length) {
33
+ if (!mapContainer) {
34
34
  return null;
35
35
  }
36
36
 
37
- const container = L.DomUtil.get(mapContainer[0]);
37
+ const container = L.DomUtil.get(mapContainer);
38
38
  if (container != null) {
39
39
  container._leaflet_id = null;
40
40
  }
41
41
 
42
- const map = L.map(mapContainer[0], options);
42
+ const map = L.map(mapContainer, options);
43
43
 
44
44
  L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
45
45
  attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@osimatic/helpers-js",
3
- "version": "1.5.15",
3
+ "version": "1.5.16",
4
4
  "main": "main.js",
5
5
  "scripts": {
6
6
  "test": "jest",
@@ -215,6 +215,100 @@ describe('OpenStreetMap', () => {
215
215
  });
216
216
  });
217
217
 
218
+ describe('createMap', () => {
219
+ let mockMap;
220
+ let mockTileLayer;
221
+ let domUtilGetSpy;
222
+ let mapSpy;
223
+ let tileLayerSpy;
224
+
225
+ beforeEach(() => {
226
+ mockMap = {
227
+ setView: jest.fn(),
228
+ getZoom: jest.fn(() => 6),
229
+ setZoom: jest.fn(),
230
+ fitBounds: jest.fn(),
231
+ invalidateSize: jest.fn(),
232
+ };
233
+ mockTileLayer = { addTo: jest.fn() };
234
+ domUtilGetSpy = jest.spyOn(L.DomUtil, 'get').mockReturnValue(null);
235
+ mapSpy = jest.spyOn(L, 'map').mockReturnValue(mockMap);
236
+ tileLayerSpy = jest.spyOn(L, 'tileLayer').mockReturnValue(mockTileLayer);
237
+ });
238
+
239
+ afterEach(() => {
240
+ domUtilGetSpy.mockRestore();
241
+ mapSpy.mockRestore();
242
+ tileLayerSpy.mockRestore();
243
+ });
244
+
245
+ test('should return null when mapContainer is null', () => {
246
+ const result = OpenStreetMap.createMap(null);
247
+
248
+ expect(result).toBeNull();
249
+ expect(mapSpy).not.toHaveBeenCalled();
250
+ });
251
+
252
+ test('should return null when mapContainer is undefined', () => {
253
+ const result = OpenStreetMap.createMap(undefined);
254
+
255
+ expect(result).toBeNull();
256
+ expect(mapSpy).not.toHaveBeenCalled();
257
+ });
258
+
259
+ test('should create and return a Leaflet map for a valid DOM element', () => {
260
+ const div = document.createElement('div');
261
+ document.body.appendChild(div);
262
+
263
+ const result = OpenStreetMap.createMap(div);
264
+
265
+ expect(mapSpy).toHaveBeenCalledWith(div, {});
266
+ expect(tileLayerSpy).toHaveBeenCalled();
267
+ expect(mockTileLayer.addTo).toHaveBeenCalledWith(mockMap);
268
+ expect(result).toBe(mockMap);
269
+
270
+ document.body.removeChild(div);
271
+ });
272
+
273
+ test('should reset _leaflet_id when container already has one', () => {
274
+ const div = document.createElement('div');
275
+ document.body.appendChild(div);
276
+ const existingContainer = { _leaflet_id: 42 };
277
+ domUtilGetSpy.mockReturnValue(existingContainer);
278
+
279
+ OpenStreetMap.createMap(div);
280
+
281
+ expect(existingContainer._leaflet_id).toBeNull();
282
+
283
+ document.body.removeChild(div);
284
+ });
285
+
286
+ test('should pass options to L.map', () => {
287
+ const div = document.createElement('div');
288
+ document.body.appendChild(div);
289
+ const options = { zoomControl: false };
290
+
291
+ OpenStreetMap.createMap(div, options);
292
+
293
+ expect(mapSpy).toHaveBeenCalledWith(div, options);
294
+
295
+ document.body.removeChild(div);
296
+ });
297
+
298
+ test('should center on France by default', () => {
299
+ const div = document.createElement('div');
300
+ document.body.appendChild(div);
301
+ const fitBoundsSpy = jest.spyOn(L, 'latLngBounds').mockReturnValue({ _bounds: [] });
302
+
303
+ OpenStreetMap.createMap(div);
304
+
305
+ expect(mockMap.fitBounds).toHaveBeenCalled();
306
+
307
+ fitBoundsSpy.mockRestore();
308
+ document.body.removeChild(div);
309
+ });
310
+ });
311
+
218
312
  describe('centerOnFrance', () => {
219
313
  test('should call setView with France coordinates', () => {
220
314
  const mockMap = {