|
| 1 | +import {describe, it} from "node:test"; |
| 2 | +import * as assert from "node:assert/strict"; |
| 3 | +import {isIOSPlatform, isIPadOSPlatform, type TBrowserNavigator} from "./browserCompatibility"; |
| 4 | + |
| 5 | +const browserNavigator = ( |
| 6 | + userAgent: string, |
| 7 | + platform = "", |
| 8 | + maxTouchPoints = 0, |
| 9 | +): TBrowserNavigator => ({userAgent, platform, maxTouchPoints}); |
| 10 | + |
| 11 | +describe("iOS browser detection", () => { |
| 12 | + it("detects iPhone browsers independently of the browser brand", () => { |
| 13 | + assert.equal(isIOSPlatform(browserNavigator( |
| 14 | + "Mozilla/5.0 (iPhone; CPU iPhone OS 17_6 like Mac OS X) AppleWebKit/605.1.15 " + |
| 15 | + "(KHTML, like Gecko) Version/17.6 Mobile/15E148 Safari/604.1", |
| 16 | + "iPhone", |
| 17 | + 5, |
| 18 | + )), true); |
| 19 | + assert.equal(isIOSPlatform(browserNavigator( |
| 20 | + "Mozilla/5.0 (iPhone; CPU iPhone OS 17_6 like Mac OS X) AppleWebKit/605.1.15 " + |
| 21 | + "(KHTML, like Gecko) CriOS/128.0.6613.69 Mobile/15E148 Safari/604.1", |
| 22 | + "iPhone", |
| 23 | + 5, |
| 24 | + )), true); |
| 25 | + }); |
| 26 | + |
| 27 | + it("detects iPad and iPod user agents", () => { |
| 28 | + assert.equal(isIOSPlatform(browserNavigator( |
| 29 | + "Mozilla/5.0 (iPad; CPU OS 17_6 like Mac OS X) AppleWebKit/605.1.15 " + |
| 30 | + "(KHTML, like Gecko) CriOS/128.0.6613.69 Mobile/15E148 Safari/604.1", |
| 31 | + "iPad", |
| 32 | + 5, |
| 33 | + )), true); |
| 34 | + assert.equal(isIOSPlatform(browserNavigator( |
| 35 | + "Mozilla/5.0 (iPod touch; CPU iPhone OS 15_7 like Mac OS X) AppleWebKit/605.1.15 " + |
| 36 | + "(KHTML, like Gecko) Version/15.6 Mobile/15E148 Safari/604.1", |
| 37 | + "iPod", |
| 38 | + 5, |
| 39 | + )), true); |
| 40 | + }); |
| 41 | + |
| 42 | + it("detects iPadOS when it uses a desktop user agent", () => { |
| 43 | + const iPadOSNavigator = browserNavigator( |
| 44 | + "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15) AppleWebKit/605.1.15 " + |
| 45 | + "(KHTML, like Gecko) Version/17.6 Mobile/15E148 Safari/604.1", |
| 46 | + "MacIntel", |
| 47 | + 5, |
| 48 | + ); |
| 49 | + assert.equal(isIPadOSPlatform(iPadOSNavigator), true); |
| 50 | + assert.equal(isIOSPlatform(iPadOSNavigator), true); |
| 51 | + }); |
| 52 | + |
| 53 | + it("does not classify desktop or Android browsers as iOS", () => { |
| 54 | + assert.equal(isIOSPlatform(browserNavigator( |
| 55 | + "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 " + |
| 56 | + "(KHTML, like Gecko) Version/17.6 Safari/605.1.15", |
| 57 | + "MacIntel", |
| 58 | + 0, |
| 59 | + )), false); |
| 60 | + assert.equal(isIOSPlatform(browserNavigator( |
| 61 | + "Mozilla/5.0 (Linux; Android 15; Pixel 9) AppleWebKit/537.36 " + |
| 62 | + "(KHTML, like Gecko) Chrome/128.0.0.0 Mobile Safari/537.36", |
| 63 | + "Linux armv8l", |
| 64 | + 5, |
| 65 | + )), false); |
| 66 | + }); |
| 67 | +}); |
0 commit comments