Skip to content

Commit c1543fa

Browse files
committed
device.ts: add edgecase test and refactor
1 parent e3d6a06 commit c1543fa

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

client/utils/device.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,20 @@ describe('isMac', () => {
2626
});
2727
expect(isMac()).toBe(false);
2828
});
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+
});
2945
});

client/utils/device.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1-
export const isMac = () => navigator.userAgent.toLowerCase().indexOf('mac') !== -1; // eslint-disable-line
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)