From 8fb30fb9dc6771ca0e0c2ca8a37d13ee37a503da Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Fri, 23 Dec 2022 16:26:00 +0100 Subject: Add unit tests for OS detection --- tests/test.browser.js | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/tests/test.browser.js b/tests/test.browser.js index f80b12e..4fdc084 100644 --- a/tests/test.browser.js +++ b/tests/test.browser.js @@ -1,9 +1,69 @@ /* eslint-disable no-console */ const expect = chai.expect; -import { isSafari, isFirefox, isChrome, isChromium, isOpera, isEdge, +import { isMac, isWindows, isIOS, + isSafari, isFirefox, isChrome, isChromium, isOpera, isEdge, isGecko, isWebKit, isBlink } from '../core/util/browser.js'; +describe('OS detection', function () { + let origNavigator; + beforeEach(function () { + // window.navigator is a protected read-only property in many + // environments, so we need to redefine it whilst running these + // tests. + origNavigator = Object.getOwnPropertyDescriptor(window, "navigator"); + + Object.defineProperty(window, "navigator", {value: {}}); + }); + + afterEach(function () { + Object.defineProperty(window, "navigator", origNavigator); + }); + + it('should handle macOS', function () { + const platforms = [ + "MacIntel", + "MacPPC", + ]; + + platforms.forEach((platform) => { + navigator.platform = platform; + expect(isMac()).to.be.true; + expect(isWindows()).to.be.false; + expect(isIOS()).to.be.false; + }); + }); + + it('should handle Windows', function () { + const platforms = [ + "Win32", + "Win64", + ]; + + platforms.forEach((platform) => { + navigator.platform = platform; + expect(isMac()).to.be.false; + expect(isWindows()).to.be.true; + expect(isIOS()).to.be.false; + }); + }); + + it('should handle iOS', function () { + const platforms = [ + "iPhone", + "iPod", + "iPad", + ]; + + platforms.forEach((platform) => { + navigator.platform = platform; + expect(isMac()).to.be.false; + expect(isWindows()).to.be.false; + expect(isIOS()).to.be.true; + }); + }); +}); + describe('Browser detection', function () { let origNavigator; beforeEach(function () { -- cgit v1.2.1