-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Expand file tree
/
Copy pathuseDetectOS.test.mjs
More file actions
79 lines (64 loc) · 2.02 KB
/
useDetectOS.test.mjs
File metadata and controls
79 lines (64 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import assert from 'node:assert/strict';
import { describe, it, afterEach } from 'node:test';
import { renderHook, waitFor } from '@testing-library/react';
import useDetectOS from '#site/hooks/react-client/useDetectOS';
const windowsUserAgent =
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36';
const macUserAgent =
'Mozilla/5.0 (Macintosh; Intel Mac OS X 13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36';
const originalNavigator = global.navigator;
describe('useDetectOS', () => {
afterEach(() => {
Object.defineProperty(global, 'navigator', {
value: originalNavigator,
writable: true,
});
});
it('should detect WIN OS and 64 bitness', async () => {
Object.defineProperty(global, 'navigator', {
value: {
userAgent: windowsUserAgent,
userAgentData: {
getHighEntropyValues: () => ({ bitness: '64' }),
},
},
writable: true,
});
const { result } = renderHook(() => useDetectOS());
await waitFor(() => {
assert.deepEqual(result.current, {
os: 'WIN',
bitness: '64',
architecture: 'x86',
});
});
});
it('should detect WIN OS and 64 bitness from user agent', async () => {
Object.defineProperty(global, 'navigator', {
value: { userAgent: windowsUserAgent },
writable: true,
});
const { result } = renderHook(() => useDetectOS());
await waitFor(() => {
assert.deepEqual(result.current, {
os: 'WIN',
bitness: '64',
architecture: 'x86',
});
});
});
it('should detect MAC OS and default bitness', async () => {
Object.defineProperty(global, 'navigator', {
value: { userAgent: macUserAgent },
writable: true,
});
const { result } = renderHook(() => useDetectOS());
await waitFor(() => {
assert.deepEqual(result.current, {
os: 'MAC',
bitness: '64',
architecture: 'arm',
});
});
});
});