Skip to content

Commit 24368cc

Browse files
committed
isMac: migrate to typescript and add test
1 parent d80961c commit 24368cc

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

.eslintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
}
3131
],
3232
"react/jsx-filename-extension": [1, { "extensions": [".jsx", ".tsx"] }],
33+
"import/prefer-default-export": "off",
3334
"comma-dangle": 0, // not sure why airbnb turned this on. gross!
3435
"default-param-last": 0,
3536
"no-else-return" :0,

client/utils/device.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

client/utils/device.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { isMac } from './device';
2+
3+
describe('isMac', () => {
4+
const originalUserAgent = navigator.userAgent;
5+
6+
afterEach(() => {
7+
// Restore the original userAgent after each test
8+
Object.defineProperty(navigator, 'userAgent', {
9+
value: originalUserAgent,
10+
configurable: true
11+
});
12+
});
13+
14+
it('returns true when userAgent contains "Mac"', () => {
15+
Object.defineProperty(navigator, 'userAgent', {
16+
value: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)',
17+
configurable: true
18+
});
19+
expect(isMac()).toBe(true);
20+
});
21+
22+
it('returns false when userAgent does not contain "Mac"', () => {
23+
Object.defineProperty(navigator, 'userAgent', {
24+
value: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
25+
configurable: true
26+
});
27+
expect(isMac()).toBe(false);
28+
});
29+
30+
it('returns false when navigator agent is null', () => {
31+
Object.defineProperty(navigator, 'userAgent', {
32+
value: null,
33+
configurable: true
34+
});
35+
expect(isMac()).toBe(false);
36+
});
37+
38+
it('returns false when navigator agent is undefined', () => {
39+
Object.defineProperty(navigator, 'userAgent', {
40+
value: undefined,
41+
configurable: true
42+
});
43+
expect(isMac()).toBe(false);
44+
});
45+
});

client/utils/device.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Checks if the user's OS is macOS based on the user agent string.
3+
* This is the preferred method over navigator.platform, which is now deprecated:
4+
* - see https://developer.mozilla.org/en-US/docs/Web/API/Navigator/platform
5+
*/
6+
export function isMac(): boolean {
7+
return typeof navigator?.userAgent === 'string'
8+
? navigator.userAgent.toLowerCase().includes('mac')
9+
: false;
10+
}

0 commit comments

Comments
 (0)