forked from janhq/jan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
useGetSystemResources.test.ts
103 lines (80 loc) · 2.65 KB
/
useGetSystemResources.test.ts
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// useGetSystemResources.test.ts
import { renderHook, act } from '@testing-library/react'
import useGetSystemResources from './useGetSystemResources'
import { extensionManager } from '@/extension/ExtensionManager'
// Mock the extensionManager
jest.mock('@/extension/ExtensionManager', () => ({
extensionManager: {
get: jest.fn(),
},
}))
// Mock the necessary dependencies
jest.mock('jotai', () => ({
useAtomValue: jest.fn(),
useSetAtom: () => jest.fn(),
useAtom: jest.fn(),
atom: jest.fn(),
}))
describe('useGetSystemResources', () => {
const mockMonitoringExtension = {
getResourcesInfo: jest.fn(),
getCurrentLoad: jest.fn(),
}
beforeEach(() => {
jest.useFakeTimers()
;(extensionManager.get as jest.Mock).mockReturnValue(
mockMonitoringExtension
)
})
afterEach(() => {
jest.clearAllMocks()
jest.useRealTimers()
})
it('should fetch system resources on initial render', async () => {
mockMonitoringExtension.getResourcesInfo.mockResolvedValue({
mem: { usedMemory: 4000, totalMemory: 8000 },
})
mockMonitoringExtension.getCurrentLoad.mockResolvedValue({
cpu: { usage: 50 },
gpu: [],
})
const { result } = renderHook(() => useGetSystemResources())
expect(mockMonitoringExtension.getResourcesInfo).toHaveBeenCalledTimes(1)
})
it('should start watching system resources when watch is called', () => {
const { result } = renderHook(() => useGetSystemResources())
act(() => {
result.current.watch()
})
expect(mockMonitoringExtension.getResourcesInfo).toHaveBeenCalled()
// Fast-forward time by 2 seconds
act(() => {
jest.advanceTimersByTime(2000)
})
expect(mockMonitoringExtension.getResourcesInfo).toHaveBeenCalled()
})
it('should stop watching when stopWatching is called', () => {
const { result } = renderHook(() => useGetSystemResources())
act(() => {
result.current.watch()
})
act(() => {
result.current.stopWatching()
})
// Fast-forward time by 2 seconds
act(() => {
jest.advanceTimersByTime(2000)
})
// Expect no additional calls after stopping
expect(mockMonitoringExtension.getResourcesInfo).toHaveBeenCalled()
})
it('should not fetch resources if monitoring extension is not available', async () => {
;(extensionManager.get as jest.Mock).mockReturnValue(null)
const { result } = renderHook(() => useGetSystemResources())
await act(async () => {
result.current.getSystemResources()
})
expect(mockMonitoringExtension.getResourcesInfo).not.toHaveBeenCalled()
expect(mockMonitoringExtension.getCurrentLoad).not.toHaveBeenCalled()
})
})