diff --git a/package-lock.json b/package-lock.json index 339a28b..d00687e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,6 +7,9 @@ "": { "name": "@deriv-com/utils", "version": "0.0.11", + "dependencies": { + "ua-parser-js": "^1.0.37" + }, "devDependencies": { "@commitlint/cli": "^19.2.1", "@commitlint/config-conventional": "^19.1.0", @@ -11629,6 +11632,28 @@ "node": ">=14.17" } }, + "node_modules/ua-parser-js": { + "version": "1.0.37", + "resolved": "https://registry.npmjs.org/ua-parser-js/-/ua-parser-js-1.0.37.tgz", + "integrity": "sha512-bhTyI94tZofjo+Dn8SN6Zv8nBDvyXTymAdM3LDI/0IboIUwTu1rEhW7v2TfiVsoYWgkQ4kOVqnI8APUFbIQIFQ==", + "funding": [ + { + "type": "opencollective", + "url": "https://opencollective.com/ua-parser-js" + }, + { + "type": "paypal", + "url": "https://paypal.me/faisalman" + }, + { + "type": "github", + "url": "https://github.com/sponsors/faisalman" + } + ], + "engines": { + "node": "*" + } + }, "node_modules/ufo": { "version": "1.5.3", "resolved": "https://registry.npmjs.org/ufo/-/ufo-1.5.3.tgz", diff --git a/package.json b/package.json index 1b69031..92afda5 100644 --- a/package.json +++ b/package.json @@ -69,5 +69,8 @@ "vite": "^5.1.2", "vite-plugin-dts": "^3.7.2", "vitest": "^1.2.2" + }, + "dependencies": { + "ua-parser-js": "^1.0.37" } } diff --git a/src/utils/__test__/mobileOSDetectAsync.utils.spec.ts b/src/utils/__test__/mobileOSDetectAsync.utils.spec.ts new file mode 100644 index 0000000..7b700b4 --- /dev/null +++ b/src/utils/__test__/mobileOSDetectAsync.utils.spec.ts @@ -0,0 +1,44 @@ +import { mobileOSDetectAsync, getOSNameWithUAParser } from "../mobileOSDetectAsync.utils"; +import UAParser from "ua-parser-js"; + +describe("mobileOSDetectAsync", () => { + test('should return "Windows Phone" for Windows Phone user agent', async () => { + jest.spyOn(window.navigator, "userAgent", "get").mockReturnValue("windows phone"); + expect(await mobileOSDetectAsync()).toBe("Windows Phone"); + }); + + test('should return "Android" for Android user agent', async () => { + jest.spyOn(window.navigator, "userAgent", "get").mockReturnValue("android"); + expect(await mobileOSDetectAsync()).toBe("Android"); + }); + + test('should return "iOS" for iOS user agent', async () => { + jest.spyOn(window.navigator, "userAgent", "get").mockReturnValue("iPhone"); + jest.spyOn(window, "MSStream", "get").mockReturnValue({ + msClose: () => {}, + msDetachStream: () => {}, + type: "someType", + }); + expect(await mobileOSDetectAsync()).toBe("iOS"); + }); + + test('should return "iOS" for iOS user agent', async () => { + jest.spyOn(window.navigator, "userAgent", "get").mockReturnValue("iPhone"); + jest.spyOn(window, "MSStream", "get").mockReturnValue({ + msClose: () => {}, + msDetachStream: () => {}, + type: "someType", + }); + expect(await mobileOSDetectAsync()).toBe("iOS"); + }); + + test('should return "unknown" for unknown user agent', async () => { + jest.spyOn(window.navigator, "userAgent", "get").mockReturnValue("unknown"); + expect(await mobileOSDetectAsync()).toBe("unknown"); + }); + + test("should return the OS name", () => { + jest.spyOn(UAParser, "OS", "get").mockReturnValue({ NAME: "name", VERSION: "version" }); + expect(getOSNameWithUAParser()).toBe("name"); + }); +});