forked from janhq/jan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseDeleteModel.test.ts
73 lines (59 loc) · 2.05 KB
/
useDeleteModel.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
import { renderHook, act } from '@testing-library/react'
import { extensionManager } from '@/extension/ExtensionManager'
import useDeleteModel from './useDeleteModel'
import { toaster } from '@/containers/Toast'
import { useSetAtom } from 'jotai'
// Mock the dependencies
jest.mock('@/extension/ExtensionManager')
jest.mock('@/containers/Toast')
jest.mock('jotai', () => ({
useSetAtom: jest.fn(() => jest.fn()),
atom: jest.fn(),
}))
describe('useDeleteModel', () => {
const mockModel: any = {
id: 'test-model',
name: 'Test Model',
// Add other required properties of ModelFile
}
const mockDeleteModel = jest.fn()
beforeEach(() => {
jest.clearAllMocks()
;(extensionManager.get as jest.Mock).mockReturnValue({
deleteModel: mockDeleteModel,
})
})
it('should delete a model successfully', async () => {
const { result } = renderHook(() => useDeleteModel())
await act(async () => {
await result.current.deleteModel(mockModel)
})
expect(mockDeleteModel).toHaveBeenCalledWith(mockModel)
expect(toaster).toHaveBeenCalledWith({
title: 'Model Deletion Successful',
description: `Model ${mockModel.name} has been successfully deleted.`,
type: 'success',
})
})
it('should call removeDownloadedModel with the model id', async () => {
const { result } = renderHook(() => useDeleteModel())
await act(async () => {
await result.current.deleteModel(mockModel)
})
// Assuming useSetAtom returns a mock function
;(useSetAtom as jest.Mock).mockReturnValue(jest.fn())
expect(useSetAtom).toHaveBeenCalled()
})
it('should handle errors during model deletion', async () => {
const error = new Error('Deletion failed')
mockDeleteModel.mockRejectedValue(error)
const { result } = renderHook(() => useDeleteModel())
await act(async () => {
await expect(result.current.deleteModel(mockModel)).rejects.toThrow(
'Deletion failed'
)
})
expect(mockDeleteModel).toHaveBeenCalledWith(mockModel)
expect(toaster).not.toHaveBeenCalled()
})
})