forked from agentic-review-benchmarks/dify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuploading.spec.tsx
More file actions
341 lines (281 loc) · 10.7 KB
/
uploading.spec.tsx
File metadata and controls
341 lines (281 loc) · 10.7 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import type { Dependency, PluginDeclaration } from '../../../types'
import { render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { PluginCategoryEnum } from '../../../types'
import Uploading from './uploading'
// Factory function for test data
const createMockManifest = (overrides: Partial<PluginDeclaration> = {}): PluginDeclaration => ({
plugin_unique_identifier: 'test-plugin-uid',
version: '1.0.0',
author: 'test-author',
icon: 'test-icon.png',
name: 'Test Plugin',
category: PluginCategoryEnum.tool,
label: { 'en-US': 'Test Plugin' } as PluginDeclaration['label'],
description: { 'en-US': 'A test plugin' } as PluginDeclaration['description'],
created_at: '2024-01-01T00:00:00Z',
resource: {},
plugins: [],
verified: true,
endpoint: { settings: [], endpoints: [] },
model: null,
tags: [],
agent_strategy: null,
meta: { version: '1.0.0' },
trigger: {} as PluginDeclaration['trigger'],
...overrides,
})
const createMockDependencies = (): Dependency[] => [
{
type: 'package',
value: {
unique_identifier: 'dep-1',
manifest: createMockManifest({ name: 'Dep Plugin 1' }),
},
},
]
const createMockFile = (name: string = 'test-plugin.difypkg'): File => {
return new File(['test content'], name, { type: 'application/octet-stream' })
}
// Mock external dependencies
const mockUploadFile = vi.fn()
vi.mock('@/service/plugins', () => ({
uploadFile: (...args: unknown[]) => mockUploadFile(...args),
}))
vi.mock('../../../card', () => ({
default: ({ payload, isLoading, loadingFileName }: {
payload: { name: string }
isLoading?: boolean
loadingFileName?: string
}) => (
<div data-testid="card">
<span data-testid="card-name">{payload?.name}</span>
<span data-testid="card-is-loading">{isLoading ? 'true' : 'false'}</span>
<span data-testid="card-loading-filename">{loadingFileName || 'null'}</span>
</div>
),
}))
describe('Uploading', () => {
const defaultProps = {
isBundle: false,
file: createMockFile(),
onCancel: vi.fn(),
onPackageUploaded: vi.fn(),
onBundleUploaded: vi.fn(),
onFailed: vi.fn(),
}
beforeEach(() => {
vi.clearAllMocks()
mockUploadFile.mockReset()
})
// ================================
// Rendering Tests
// ================================
describe('Rendering', () => {
it('should render uploading message with file name', () => {
render(<Uploading {...defaultProps} />)
expect(screen.getByText(/plugin.installModal.uploadingPackage/)).toBeInTheDocument()
})
it('should render loading spinner', () => {
render(<Uploading {...defaultProps} />)
// The spinner has animate-spin-slow class
const spinner = document.querySelector('.animate-spin-slow')
expect(spinner).toBeInTheDocument()
})
it('should render card with loading state', () => {
render(<Uploading {...defaultProps} />)
expect(screen.getByTestId('card-is-loading')).toHaveTextContent('true')
})
it('should render card with file name', () => {
const file = createMockFile('my-plugin.difypkg')
render(<Uploading {...defaultProps} file={file} />)
expect(screen.getByTestId('card-name')).toHaveTextContent('my-plugin.difypkg')
expect(screen.getByTestId('card-loading-filename')).toHaveTextContent('my-plugin.difypkg')
})
it('should render cancel button', () => {
render(<Uploading {...defaultProps} />)
expect(screen.getByRole('button', { name: 'common.operation.cancel' })).toBeInTheDocument()
})
it('should render disabled install button', () => {
render(<Uploading {...defaultProps} />)
const installButton = screen.getByRole('button', { name: 'plugin.installModal.install' })
expect(installButton).toBeDisabled()
})
})
// ================================
// Upload Behavior Tests
// ================================
describe('Upload Behavior', () => {
it('should call uploadFile on mount', async () => {
mockUploadFile.mockResolvedValue({})
render(<Uploading {...defaultProps} />)
await waitFor(() => {
expect(mockUploadFile).toHaveBeenCalledWith(defaultProps.file, false)
})
})
it('should call uploadFile with isBundle=true for bundle files', async () => {
mockUploadFile.mockResolvedValue({})
render(<Uploading {...defaultProps} isBundle />)
await waitFor(() => {
expect(mockUploadFile).toHaveBeenCalledWith(defaultProps.file, true)
})
})
it('should call onFailed when upload fails with error message', async () => {
const errorMessage = 'Upload failed: file too large'
mockUploadFile.mockRejectedValue({
response: { message: errorMessage },
})
const onFailed = vi.fn()
render(<Uploading {...defaultProps} onFailed={onFailed} />)
await waitFor(() => {
expect(onFailed).toHaveBeenCalledWith(errorMessage)
})
})
// NOTE: The uploadFile API has an unconventional contract where it always rejects.
// Success vs failure is determined by whether response.message exists:
// - If response.message exists → treated as failure (calls onFailed)
// - If response.message is absent → treated as success (calls onPackageUploaded/onBundleUploaded)
// This explains why we use mockRejectedValue for "success" scenarios below.
it('should call onPackageUploaded when upload rejects without error message (success case)', async () => {
const mockResult = {
unique_identifier: 'test-uid',
manifest: createMockManifest(),
}
mockUploadFile.mockRejectedValue({
response: mockResult,
})
const onPackageUploaded = vi.fn()
render(
<Uploading
{...defaultProps}
isBundle={false}
onPackageUploaded={onPackageUploaded}
/>,
)
await waitFor(() => {
expect(onPackageUploaded).toHaveBeenCalledWith({
uniqueIdentifier: mockResult.unique_identifier,
manifest: mockResult.manifest,
})
})
})
it('should call onBundleUploaded when upload rejects without error message (success case)', async () => {
const mockDependencies = createMockDependencies()
mockUploadFile.mockRejectedValue({
response: mockDependencies,
})
const onBundleUploaded = vi.fn()
render(
<Uploading
{...defaultProps}
isBundle
onBundleUploaded={onBundleUploaded}
/>,
)
await waitFor(() => {
expect(onBundleUploaded).toHaveBeenCalledWith(mockDependencies)
})
})
})
// ================================
// Cancel Button Tests
// ================================
describe('Cancel Button', () => {
it('should call onCancel when cancel button is clicked', async () => {
const user = userEvent.setup()
const onCancel = vi.fn()
render(<Uploading {...defaultProps} onCancel={onCancel} />)
await user.click(screen.getByRole('button', { name: 'common.operation.cancel' }))
expect(onCancel).toHaveBeenCalledTimes(1)
})
})
// ================================
// File Name Display Tests
// ================================
describe('File Name Display', () => {
it('should display correct file name for package file', () => {
const file = createMockFile('custom-plugin.difypkg')
render(<Uploading {...defaultProps} file={file} />)
expect(screen.getByTestId('card-name')).toHaveTextContent('custom-plugin.difypkg')
})
it('should display correct file name for bundle file', () => {
const file = createMockFile('custom-bundle.difybndl')
render(<Uploading {...defaultProps} file={file} isBundle />)
expect(screen.getByTestId('card-name')).toHaveTextContent('custom-bundle.difybndl')
})
it('should display file name in uploading message', () => {
const file = createMockFile('special-plugin.difypkg')
render(<Uploading {...defaultProps} file={file} />)
// The message includes the file name as a parameter
expect(screen.getByText(/plugin\.installModal\.uploadingPackage/)).toHaveTextContent('special-plugin.difypkg')
})
})
// ================================
// Edge Cases Tests
// ================================
describe('Edge Cases', () => {
it('should handle empty response gracefully', async () => {
mockUploadFile.mockRejectedValue({
response: {},
})
const onPackageUploaded = vi.fn()
render(<Uploading {...defaultProps} onPackageUploaded={onPackageUploaded} />)
await waitFor(() => {
expect(onPackageUploaded).toHaveBeenCalledWith({
uniqueIdentifier: undefined,
manifest: undefined,
})
})
})
it('should handle response with only unique_identifier', async () => {
mockUploadFile.mockRejectedValue({
response: { unique_identifier: 'only-uid' },
})
const onPackageUploaded = vi.fn()
render(<Uploading {...defaultProps} onPackageUploaded={onPackageUploaded} />)
await waitFor(() => {
expect(onPackageUploaded).toHaveBeenCalledWith({
uniqueIdentifier: 'only-uid',
manifest: undefined,
})
})
})
it('should handle file with special characters in name', () => {
const file = createMockFile('my plugin (v1.0).difypkg')
render(<Uploading {...defaultProps} file={file} />)
expect(screen.getByTestId('card-name')).toHaveTextContent('my plugin (v1.0).difypkg')
})
})
// ================================
// Props Variations Tests
// ================================
describe('Props Variations', () => {
it('should work with different file types', () => {
const files = [
createMockFile('plugin-a.difypkg'),
createMockFile('plugin-b.zip'),
createMockFile('bundle.difybndl'),
]
files.forEach((file) => {
const { unmount } = render(<Uploading {...defaultProps} file={file} />)
expect(screen.getByTestId('card-name')).toHaveTextContent(file.name)
unmount()
})
})
it('should pass isBundle=false to uploadFile for package files', async () => {
mockUploadFile.mockResolvedValue({})
render(<Uploading {...defaultProps} isBundle={false} />)
await waitFor(() => {
expect(mockUploadFile).toHaveBeenCalledWith(expect.anything(), false)
})
})
it('should pass isBundle=true to uploadFile for bundle files', async () => {
mockUploadFile.mockResolvedValue({})
render(<Uploading {...defaultProps} isBundle />)
await waitFor(() => {
expect(mockUploadFile).toHaveBeenCalledWith(expect.anything(), true)
})
})
})
})