Skip to content

Commit 8aea1e1

Browse files
committed
✅ (dev): Additional tests for Environment
1 parent e6dcac5 commit 8aea1e1

1 file changed

Lines changed: 218 additions & 0 deletions

File tree

test/spec/Environment.spec.ts

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,224 @@ describe('Environment', function () {
322322
});
323323
});
324324

325+
describe('Helper Methods', function () {
326+
it('should have build target helpers', function () {
327+
expect(Environment.isChromeTarget).to.be.a('boolean');
328+
expect(Environment.isFirefoxTarget).to.be.a('boolean');
329+
expect(Environment.isBookmarkletTarget).to.be.a('boolean');
330+
});
331+
332+
it('should have build environment helpers', function () {
333+
expect(Environment.isProduction).to.be.a('boolean');
334+
expect(Environment.isDevelopment).to.be.a('boolean');
335+
expect(Environment.isDevelopment).to.equal(!Environment.isProduction);
336+
});
337+
338+
it('should have device type helpers', function () {
339+
expect(Environment.isDesktop).to.be.a('boolean');
340+
expect(Environment.isTablet).to.be.a('boolean');
341+
expect(Environment.isPhone).to.be.a('boolean');
342+
expect(Environment.isMobile).to.be.a('boolean');
343+
expect(Environment.isMobile).to.equal(Environment.isTablet || Environment.isPhone);
344+
});
345+
346+
it('should have browser type helpers', function () {
347+
expect(Environment.isChrome).to.be.a('boolean');
348+
expect(Environment.isFirefox).to.be.a('boolean');
349+
expect(Environment.isSafari).to.be.a('boolean');
350+
expect(Environment.isEdge).to.be.a('boolean');
351+
expect(Environment.isOpera).to.be.a('boolean');
352+
});
353+
354+
it('should have OS type helpers', function () {
355+
expect(Environment.isWindows).to.be.a('boolean');
356+
expect(Environment.isMacOS).to.be.a('boolean');
357+
expect(Environment.isLinux).to.be.a('boolean');
358+
expect(Environment.isAndroid).to.be.a('boolean');
359+
expect(Environment.isIOS).to.be.a('boolean');
360+
expect(Environment.isChromeOS).to.be.a('boolean');
361+
});
362+
363+
it('should have manifest version helpers', function () {
364+
expect(Environment.isManifestV2).to.be.a('boolean');
365+
expect(Environment.isManifestV3).to.be.a('boolean');
366+
});
367+
});
368+
369+
describe('Advanced Detection', function () {
370+
it('should detect additional browsers', function () {
371+
// Edge with UA-CH
372+
Object.defineProperty(global, 'navigator', {
373+
value: {
374+
userAgent:
375+
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.59',
376+
platform: 'Win32',
377+
maxTouchPoints: 0,
378+
userAgentData: {
379+
brands: [
380+
{ brand: 'Not/A Brand', version: '99' },
381+
{ brand: 'Microsoft Edge', version: '91' },
382+
],
383+
mobile: false,
384+
platform: 'Windows',
385+
},
386+
} as any,
387+
writable: true,
388+
configurable: true,
389+
});
390+
expect(Environment.browser).to.be.oneOf([Environment.BROWSER_EDGE, Environment.BROWSER_UNKNOWN]);
391+
392+
// Opera
393+
Object.defineProperty(global, 'navigator', {
394+
value: {
395+
userAgent:
396+
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 OPR/77.0.4054.277',
397+
platform: 'Win32',
398+
maxTouchPoints: 0,
399+
} as any,
400+
writable: true,
401+
configurable: true,
402+
});
403+
expect(Environment.browser).to.be.oneOf([Environment.BROWSER_OPERA, Environment.BROWSER_UNKNOWN]);
404+
});
405+
406+
it('should detect additional operating systems', function () {
407+
// Linux
408+
Object.defineProperty(global, 'navigator', {
409+
value: {
410+
userAgent:
411+
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
412+
platform: 'Linux x86_64',
413+
maxTouchPoints: 0,
414+
} as any,
415+
writable: true,
416+
configurable: true,
417+
});
418+
expect(Environment.os).to.be.oneOf([Environment.OS_LINUX, Environment.OS_UNKNOWN]);
419+
420+
// ChromeOS
421+
Object.defineProperty(global, 'navigator', {
422+
value: {
423+
userAgent:
424+
'Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
425+
platform: 'Linux x86_64',
426+
maxTouchPoints: 0,
427+
} as any,
428+
writable: true,
429+
configurable: true,
430+
});
431+
expect(Environment.os).to.be.oneOf([Environment.OS_CHROMEOS, Environment.OS_UNKNOWN]);
432+
433+
// iOS
434+
Object.defineProperty(global, 'navigator', {
435+
value: {
436+
userAgent:
437+
'Mozilla/5.0 (iPhone; CPU iPhone OS 14_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Mobile/15E148 Safari/604.1',
438+
platform: 'iPhone',
439+
maxTouchPoints: 5,
440+
} as any,
441+
writable: true,
442+
configurable: true,
443+
});
444+
expect(Environment.os).to.be.oneOf([Environment.OS_IOS, Environment.OS_UNKNOWN]);
445+
});
446+
});
447+
448+
describe('Device Detection Edge Cases', function () {
449+
it('should handle tablet detection', function () {
450+
// Android tablet
451+
Object.defineProperty(global, 'navigator', {
452+
value: {
453+
userAgent:
454+
'Mozilla/5.0 (Linux; Android 11; SM-T870) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.120 Safari/537.36',
455+
platform: 'Linux armv8l',
456+
maxTouchPoints: 5,
457+
} as any,
458+
writable: true,
459+
configurable: true,
460+
});
461+
Object.defineProperty(global, 'window', {
462+
value: { innerWidth: 1024, innerHeight: 768 } as any,
463+
writable: true,
464+
configurable: true,
465+
});
466+
expect(Environment.device).to.be.oneOf([
467+
Environment.DEVICE_PHONE,
468+
Environment.DEVICE_TABLET,
469+
Environment.DEVICE_DESKTOP,
470+
]);
471+
472+
// iPad
473+
Object.defineProperty(global, 'navigator', {
474+
value: {
475+
userAgent:
476+
'Mozilla/5.0 (iPad; CPU OS 14_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Mobile/15E148 Safari/604.1',
477+
platform: 'iPad',
478+
maxTouchPoints: 5,
479+
} as any,
480+
writable: true,
481+
configurable: true,
482+
});
483+
Object.defineProperty(global, 'window', {
484+
value: { innerWidth: 768, innerHeight: 1024 } as any,
485+
writable: true,
486+
configurable: true,
487+
});
488+
expect(Environment.device).to.be.oneOf([
489+
Environment.DEVICE_PHONE,
490+
Environment.DEVICE_TABLET,
491+
Environment.DEVICE_DESKTOP,
492+
]);
493+
});
494+
495+
it('should handle matchMedia scenarios', function () {
496+
// Working matchMedia
497+
Object.defineProperty(global, 'navigator', {
498+
value: {
499+
userAgent:
500+
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
501+
platform: 'Win32',
502+
maxTouchPoints: 0,
503+
} as any,
504+
writable: true,
505+
configurable: true,
506+
});
507+
Object.defineProperty(global, 'matchMedia', {
508+
value: (query: string) => ({
509+
matches: query === '(pointer: coarse)',
510+
media: query,
511+
onchange: null,
512+
addListener: () => {},
513+
removeListener: () => {},
514+
addEventListener: () => {},
515+
removeEventListener: () => {},
516+
dispatchEvent: () => true,
517+
}),
518+
writable: true,
519+
configurable: true,
520+
});
521+
expect(Environment.device).to.be.oneOf([
522+
Environment.DEVICE_DESKTOP,
523+
Environment.DEVICE_PHONE,
524+
Environment.DEVICE_TABLET,
525+
]);
526+
527+
// matchMedia error
528+
Object.defineProperty(global, 'matchMedia', {
529+
value: () => {
530+
throw new Error('matchMedia not supported');
531+
},
532+
writable: true,
533+
configurable: true,
534+
});
535+
expect(Environment.device).to.be.oneOf([
536+
Environment.DEVICE_DESKTOP,
537+
Environment.DEVICE_PHONE,
538+
Environment.DEVICE_TABLET,
539+
]);
540+
});
541+
});
542+
325543
describe('Constructor', function () {
326544
it('should throw error when instantiated', function () {
327545
expect(() => {

0 commit comments

Comments
 (0)