@osimatic/helpers-js 1.5.14 → 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.
package/http_client.js CHANGED
@@ -502,6 +502,4 @@ class HTTPClient {
502
502
 
503
503
  }
504
504
 
505
- require('whatwg-fetch'); //fetch polyfill loaded in window.fetch
506
-
507
505
  module.exports = { HTTPClient };
package/media.js CHANGED
@@ -182,7 +182,7 @@ class UserMedia {
182
182
  if (errName === 'NotAllowedError') {
183
183
  errorType = "UserPermissionDenied";
184
184
  }
185
- } else if (browserName === 'Microsoft Edge') {
185
+ } else if (browserName === 'Edge') {
186
186
  if (errName === 'NotAllowedError') {
187
187
  errorType = "UserPermissionDenied";
188
188
  } else if (errName === 'NotReadableError') {
@@ -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.14",
3
+ "version": "1.5.16",
4
4
  "main": "main.js",
5
5
  "scripts": {
6
6
  "test": "jest",
@@ -24,8 +24,7 @@
24
24
  },
25
25
  "devDependencies": {
26
26
  "jest": "^30.2.0",
27
- "jest-environment-jsdom": "^30.2.0",
28
- "whatwg-fetch": "^3.6.20"
27
+ "jest-environment-jsdom": "^30.2.0"
29
28
  },
30
29
  "publishConfig": {
31
30
  "access": "public"
@@ -482,29 +482,6 @@ describe('FormHelper', () => {
482
482
  expect(passwordInput.type).toBe('password');
483
483
  });
484
484
 
485
- test('should set max-width on date inputs when Modernizr.inputtypes.date is false', () => {
486
- const form = setupForm();
487
- const dateInput = addInput(form, 'mydate', 'date');
488
- global.Modernizr = { inputtypes: { date: false, time: true } };
489
-
490
- FormHelper.initTypeFields(form);
491
-
492
- expect(dateInput.style.maxWidth).toBe('120px');
493
- delete global.Modernizr;
494
- });
495
-
496
- test('should set placeholder on time inputs when Modernizr.inputtypes.time is false', () => {
497
- const form = setupForm();
498
- const timeInput = addInput(form, 'mytime', 'time');
499
- global.Modernizr = { inputtypes: { date: true, time: false } };
500
-
501
- FormHelper.initTypeFields(form);
502
-
503
- expect(timeInput.style.maxWidth).toBe('100px');
504
- expect(timeInput.placeholder).toBe('hh:mm');
505
- delete global.Modernizr;
506
- });
507
-
508
485
  test('should skip Modernizr block when Modernizr not defined', () => {
509
486
  const form = setupForm();
510
487
  addInput(form, 'password', 'password');
@@ -1,10 +1,3 @@
1
- // Mock bowser module before requiring media.js (bowser is used inside requestMediaPermissions)
2
- jest.mock('bowser', () => ({
3
- getParser: jest.fn(() => ({
4
- getBrowserName: jest.fn(() => 'Chrome')
5
- }))
6
- }), { virtual: true });
7
-
8
1
  const { AudioMedia, VideoMedia, UserMedia } = require('../media');
9
2
 
10
3
  describe('media.js', () => {
@@ -100,9 +93,13 @@ describe('media.js', () => {
100
93
  });
101
94
  });
102
95
 
103
- describe('requestMediaPermissions', () => {
104
- const bowser = require('bowser');
96
+ const UA_CHROME = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36';
97
+ const UA_FIREFOX = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:120.0) Gecko/20100101 Firefox/120.0';
98
+ const UA_SAFARI = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 14_0) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Safari/605.1.15';
99
+ const UA_EDGE = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0';
100
+ const UA_OPERA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 OPR/106.0.0.0';
105
101
 
102
+ describe('requestMediaPermissions', () => {
106
103
  beforeEach(() => {
107
104
  global.navigator = {
108
105
  mediaDevices: {
@@ -110,18 +107,13 @@ describe('media.js', () => {
110
107
  }
111
108
  };
112
109
  global.window = {
113
- navigator: {
114
- userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
115
- }
110
+ navigator: { userAgent: UA_CHROME }
116
111
  };
117
112
  });
118
113
 
119
114
  test('should resolve with stream on success', async () => {
120
115
  const mockStream = { id: 'test-stream' };
121
116
  global.navigator.mediaDevices.getUserMedia.mockResolvedValue(mockStream);
122
- bowser.getParser.mockReturnValue({
123
- getBrowserName: jest.fn().mockReturnValue('Chrome')
124
- });
125
117
 
126
118
  const result = await UserMedia.requestMediaPermissions({ audio: true, video: true });
127
119
 
@@ -132,9 +124,6 @@ describe('media.js', () => {
132
124
  test('should use default constraints when not provided', async () => {
133
125
  const mockStream = { id: 'test-stream' };
134
126
  global.navigator.mediaDevices.getUserMedia.mockResolvedValue(mockStream);
135
- bowser.getParser.mockReturnValue({
136
- getBrowserName: jest.fn().mockReturnValue('Chrome')
137
- });
138
127
 
139
128
  await UserMedia.requestMediaPermissions();
140
129
 
@@ -145,9 +134,6 @@ describe('media.js', () => {
145
134
  const error = new Error('Permission denied by system');
146
135
  error.name = 'NotAllowedError';
147
136
  global.navigator.mediaDevices.getUserMedia.mockRejectedValue(error);
148
- bowser.getParser.mockReturnValue({
149
- getBrowserName: jest.fn().mockReturnValue('Chrome')
150
- });
151
137
 
152
138
  await expect(UserMedia.requestMediaPermissions({ audio: true }))
153
139
  .rejects.toEqual({
@@ -161,9 +147,6 @@ describe('media.js', () => {
161
147
  const error = new Error('Permission denied');
162
148
  error.name = 'NotAllowedError';
163
149
  global.navigator.mediaDevices.getUserMedia.mockRejectedValue(error);
164
- bowser.getParser.mockReturnValue({
165
- getBrowserName: jest.fn().mockReturnValue('Chrome')
166
- });
167
150
 
168
151
  await expect(UserMedia.requestMediaPermissions({ audio: true }))
169
152
  .rejects.toEqual({
@@ -177,9 +160,6 @@ describe('media.js', () => {
177
160
  const error = new Error('Could not start video source');
178
161
  error.name = 'NotReadableError';
179
162
  global.navigator.mediaDevices.getUserMedia.mockRejectedValue(error);
180
- bowser.getParser.mockReturnValue({
181
- getBrowserName: jest.fn().mockReturnValue('Chrome')
182
- });
183
163
 
184
164
  await expect(UserMedia.requestMediaPermissions({ video: true }))
185
165
  .rejects.toEqual({
@@ -193,9 +173,7 @@ describe('media.js', () => {
193
173
  const error = new Error('Permission denied');
194
174
  error.name = 'NotAllowedError';
195
175
  global.navigator.mediaDevices.getUserMedia.mockRejectedValue(error);
196
- bowser.getParser.mockReturnValue({
197
- getBrowserName: jest.fn().mockReturnValue('Safari')
198
- });
176
+ global.window.navigator.userAgent = UA_SAFARI;
199
177
 
200
178
  await expect(UserMedia.requestMediaPermissions({ audio: true }))
201
179
  .rejects.toEqual({
@@ -209,9 +187,7 @@ describe('media.js', () => {
209
187
  const error = new Error('Permission denied');
210
188
  error.name = 'NotAllowedError';
211
189
  global.navigator.mediaDevices.getUserMedia.mockRejectedValue(error);
212
- bowser.getParser.mockReturnValue({
213
- getBrowserName: jest.fn().mockReturnValue('Microsoft Edge')
214
- });
190
+ global.window.navigator.userAgent = UA_EDGE;
215
191
 
216
192
  await expect(UserMedia.requestMediaPermissions({ audio: true }))
217
193
  .rejects.toEqual({
@@ -225,9 +201,7 @@ describe('media.js', () => {
225
201
  const error = new Error('Could not start video source');
226
202
  error.name = 'NotReadableError';
227
203
  global.navigator.mediaDevices.getUserMedia.mockRejectedValue(error);
228
- bowser.getParser.mockReturnValue({
229
- getBrowserName: jest.fn().mockReturnValue('Microsoft Edge')
230
- });
204
+ global.window.navigator.userAgent = UA_EDGE;
231
205
 
232
206
  await expect(UserMedia.requestMediaPermissions({ video: true }))
233
207
  .rejects.toEqual({
@@ -241,9 +215,7 @@ describe('media.js', () => {
241
215
  const error = new Error('Requested device not found');
242
216
  error.name = 'NotFoundError';
243
217
  global.navigator.mediaDevices.getUserMedia.mockRejectedValue(error);
244
- bowser.getParser.mockReturnValue({
245
- getBrowserName: jest.fn().mockReturnValue('Firefox')
246
- });
218
+ global.window.navigator.userAgent = UA_FIREFOX;
247
219
 
248
220
  await expect(UserMedia.requestMediaPermissions({ audio: true }))
249
221
  .rejects.toEqual({
@@ -257,9 +229,7 @@ describe('media.js', () => {
257
229
  const error = new Error('Media device not readable');
258
230
  error.name = 'NotReadableError';
259
231
  global.navigator.mediaDevices.getUserMedia.mockRejectedValue(error);
260
- bowser.getParser.mockReturnValue({
261
- getBrowserName: jest.fn().mockReturnValue('Firefox')
262
- });
232
+ global.window.navigator.userAgent = UA_FIREFOX;
263
233
 
264
234
  await expect(UserMedia.requestMediaPermissions({ audio: true }))
265
235
  .rejects.toEqual({
@@ -273,9 +243,7 @@ describe('media.js', () => {
273
243
  const error = new Error('Permission denied');
274
244
  error.name = 'NotAllowedError';
275
245
  global.navigator.mediaDevices.getUserMedia.mockRejectedValue(error);
276
- bowser.getParser.mockReturnValue({
277
- getBrowserName: jest.fn().mockReturnValue('Firefox')
278
- });
246
+ global.window.navigator.userAgent = UA_FIREFOX;
279
247
 
280
248
  await expect(UserMedia.requestMediaPermissions({ audio: true }))
281
249
  .rejects.toEqual({
@@ -289,9 +257,7 @@ describe('media.js', () => {
289
257
  const error = new Error('Operation aborted');
290
258
  error.name = 'AbortError';
291
259
  global.navigator.mediaDevices.getUserMedia.mockRejectedValue(error);
292
- bowser.getParser.mockReturnValue({
293
- getBrowserName: jest.fn().mockReturnValue('Firefox')
294
- });
260
+ global.window.navigator.userAgent = UA_FIREFOX;
295
261
 
296
262
  await expect(UserMedia.requestMediaPermissions({ audio: true }))
297
263
  .rejects.toEqual({
@@ -305,9 +271,7 @@ describe('media.js', () => {
305
271
  const error = new Error('Unknown error');
306
272
  error.name = 'UnknownError';
307
273
  global.navigator.mediaDevices.getUserMedia.mockRejectedValue(error);
308
- bowser.getParser.mockReturnValue({
309
- getBrowserName: jest.fn().mockReturnValue('Opera')
310
- });
274
+ global.window.navigator.userAgent = UA_OPERA;
311
275
 
312
276
  await expect(UserMedia.requestMediaPermissions({ audio: true }))
313
277
  .rejects.toEqual({
@@ -321,9 +285,6 @@ describe('media.js', () => {
321
285
  const error = new Error('Weird error');
322
286
  error.name = 'WeirdError';
323
287
  global.navigator.mediaDevices.getUserMedia.mockRejectedValue(error);
324
- bowser.getParser.mockReturnValue({
325
- getBrowserName: jest.fn().mockReturnValue('Chrome')
326
- });
327
288
 
328
289
  await expect(UserMedia.requestMediaPermissions({ audio: true }))
329
290
  .rejects.toEqual({
@@ -342,14 +303,8 @@ describe('media.js', () => {
342
303
  }
343
304
  };
344
305
  global.window = {
345
- navigator: {
346
- userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
347
- }
306
+ navigator: { userAgent: UA_CHROME }
348
307
  };
349
- const bowser = require('bowser');
350
- bowser.getParser.mockReturnValue({
351
- getBrowserName: jest.fn().mockReturnValue('Chrome')
352
- });
353
308
  });
354
309
 
355
310
  test('should call requestMediaPermissions with audio:true, video:false', async () => {
@@ -387,14 +342,8 @@ describe('media.js', () => {
387
342
  }
388
343
  };
389
344
  global.window = {
390
- navigator: {
391
- userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
392
- }
345
+ navigator: { userAgent: UA_CHROME }
393
346
  };
394
- const bowser = require('bowser');
395
- bowser.getParser.mockReturnValue({
396
- getBrowserName: jest.fn().mockReturnValue('Chrome')
397
- });
398
347
  });
399
348
 
400
349
  test('should call requestMediaPermissions with audio and video', async () => {
@@ -433,14 +382,8 @@ describe('media.js', () => {
433
382
  }
434
383
  };
435
384
  global.window = {
436
- navigator: {
437
- userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
438
- }
385
+ navigator: { userAgent: UA_CHROME }
439
386
  };
440
- const bowser = require('bowser');
441
- bowser.getParser.mockReturnValue({
442
- getBrowserName: jest.fn().mockReturnValue('Chrome')
443
- });
444
387
  });
445
388
 
446
389
  test('should call requestMediaPermissions with audio:false, video:true', async () => {
@@ -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 = {