-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelCombobox.test.tsx
More file actions
490 lines (383 loc) · 14.7 KB
/
ModelCombobox.test.tsx
File metadata and controls
490 lines (383 loc) · 14.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
import { describe, it, expect, vi, beforeEach, beforeAll, afterAll } from 'vitest'
import { render, screen, fireEvent, waitFor, act } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import '@testing-library/jest-dom/vitest'
import React from 'react'
import { ModelCombobox } from '../ModelCombobox'
// Mock translation hook
vi.mock('@/i18n/react-i18next-compat', () => ({
useTranslation: () => ({
t: (key: string, options?: Record<string, string>) => {
if (key === 'common:failedToLoadModels') return 'Failed to load models'
if (key === 'common:loading') return 'Loading'
if (key === 'common:noModelsFoundFor') return `No models found for "${options?.searchValue}"`
if (key === 'common:noModels') return 'No models available'
return key
},
}),
}))
describe('ModelCombobox', () => {
const mockOnChange = vi.fn()
const mockOnRefresh = vi.fn()
const defaultProps = {
value: '',
onChange: mockOnChange,
models: ['gpt-3.5-turbo', 'gpt-4', 'claude-3-haiku'],
}
let bcrSpy: ReturnType<typeof vi.spyOn>
let scrollSpy: ReturnType<typeof vi.spyOn>
beforeAll(() => {
const mockRect = {
width: 300,
height: 40,
top: 100,
left: 50,
bottom: 140,
right: 350,
x: 50,
y: 100,
toJSON: () => {},
} as unknown as DOMRect
bcrSpy = vi
.spyOn(Element.prototype as any, 'getBoundingClientRect')
.mockReturnValue(mockRect)
Element.prototype.scrollIntoView = () => {}
})
beforeEach(() => {
vi.clearAllMocks()
})
afterAll(() => {
bcrSpy?.mockRestore()
scrollSpy?.mockRestore()
})
it('renders input field with default placeholder', () => {
act(() => {
render(<ModelCombobox {...defaultProps} />)
})
const input = screen.getByRole('textbox')
expect(input).toBeInTheDocument()
expect(input).toHaveAttribute('placeholder', 'Type or select a model...')
})
it('renders custom placeholder', () => {
act(() => {
render(<ModelCombobox {...defaultProps} placeholder="Choose a model" />)
})
const input = screen.getByRole('textbox')
expect(input).toHaveAttribute('placeholder', 'Choose a model')
})
it('renders dropdown trigger button', () => {
act(() => {
render(<ModelCombobox {...defaultProps} />)
})
const button = screen.getByRole('button')
expect(button).toBeInTheDocument()
})
it('displays current value in input', () => {
act(() => {
render(<ModelCombobox {...defaultProps} value="gpt-4" />)
})
const input = screen.getByDisplayValue('gpt-4')
expect(input).toBeInTheDocument()
})
it('applies custom className', () => {
const { container } = render(
<ModelCombobox {...defaultProps} className="custom-class" />
)
const wrapper = container.firstChild as HTMLElement
expect(wrapper).toHaveClass('custom-class')
})
it('disables input when disabled prop is true', () => {
act(() => {
render(<ModelCombobox {...defaultProps} disabled />)
})
const input = screen.getByRole('textbox')
const button = screen.getByRole('button')
expect(input).toBeDisabled()
expect(button).toBeDisabled()
})
it('shows loading spinner in trigger button', () => {
act(() => {
render(<ModelCombobox {...defaultProps} loading />)
})
const button = screen.getByRole('button')
const spinner = button.querySelector('.animate-spin')
expect(spinner).toBeInTheDocument()
})
it('shows loading section when dropdown is opened during loading', async () => {
const user = userEvent.setup()
render(<ModelCombobox {...defaultProps} loading />)
// Click input to trigger dropdown opening
const input = screen.getByRole('textbox')
await user.click(input)
// Wait for dropdown to appear and check loading section
await waitFor(() => {
const dropdown = document.querySelector('[data-dropdown="model-combobox"]')
expect(dropdown).toBeInTheDocument()
expect(screen.getByText('Loading')).toBeInTheDocument()
})
})
it('calls onChange when typing', async () => {
const user = userEvent.setup()
const localMockOnChange = vi.fn()
render(<ModelCombobox {...defaultProps} onChange={localMockOnChange} />)
const input = screen.getByRole('textbox')
await user.type(input, 'g')
expect(localMockOnChange).toHaveBeenCalledWith('g')
})
it('updates input value when typing', async () => {
const user = userEvent.setup()
render(<ModelCombobox {...defaultProps} />)
const input = screen.getByRole('textbox')
await user.type(input, 'test')
expect(input).toHaveValue('test')
})
it('handles input focus', async () => {
const user = userEvent.setup()
render(<ModelCombobox {...defaultProps} />)
const input = screen.getByRole('textbox')
await user.click(input)
expect(input).toHaveFocus()
})
it('renders with empty models array', () => {
act(() => {
render(<ModelCombobox {...defaultProps} models={[]} />)
})
const input = screen.getByRole('textbox')
expect(input).toBeInTheDocument()
})
it('renders with models array', () => {
act(() => {
render(<ModelCombobox {...defaultProps} models={['model1', 'model2']} />)
})
const input = screen.getByRole('textbox')
expect(input).toBeInTheDocument()
})
it('handles mount and unmount without errors', () => {
const { unmount } = render(<ModelCombobox {...defaultProps} />)
expect(screen.getByRole('textbox')).toBeInTheDocument()
unmount()
expect(screen.queryByRole('textbox')).not.toBeInTheDocument()
})
it('handles props changes', () => {
const { rerender } = render(<ModelCombobox {...defaultProps} value="" />)
expect(screen.getByDisplayValue('')).toBeInTheDocument()
rerender(<ModelCombobox {...defaultProps} value="gpt-4" />)
expect(screen.getByDisplayValue('gpt-4')).toBeInTheDocument()
})
it('handles models array changes', () => {
const { rerender } = render(<ModelCombobox {...defaultProps} models={[]} />)
expect(screen.getByRole('textbox')).toBeInTheDocument()
rerender(<ModelCombobox {...defaultProps} models={['model1', 'model2']} />)
expect(screen.getByRole('textbox')).toBeInTheDocument()
})
it('does not open dropdown when clicking input with no models', async () => {
const user = userEvent.setup()
render(<ModelCombobox {...defaultProps} models={[]} />)
const input = screen.getByRole('textbox')
await user.click(input)
// Should focus but not open dropdown
expect(input).toHaveFocus()
const dropdown = document.querySelector('[data-dropdown="model-combobox"]')
expect(dropdown).not.toBeInTheDocument()
})
it('accepts error prop without crashing', () => {
act(() => {
render(<ModelCombobox {...defaultProps} error="Test error message" />)
})
const input = screen.getByRole('textbox')
expect(input).toBeInTheDocument()
expect(input).toHaveAttribute('placeholder', 'Type or select a model...')
})
it('renders with all props', () => {
act(() => {
render(
<ModelCombobox
{...defaultProps}
loading
error="Error message"
onRefresh={mockOnRefresh}
placeholder="Custom placeholder"
disabled
/>
)
})
const input = screen.getByRole('textbox')
expect(input).toBeInTheDocument()
expect(input).toBeDisabled()
})
it('opens dropdown when clicking trigger button', async () => {
const user = userEvent.setup()
render(<ModelCombobox {...defaultProps} />)
const button = screen.getByRole('button')
await user.click(button)
await waitFor(() => {
const dropdown = document.querySelector('[data-dropdown="model-combobox"]')
expect(dropdown).toBeInTheDocument()
})
})
it('opens dropdown when clicking input', async () => {
const user = userEvent.setup()
render(<ModelCombobox {...defaultProps} />)
const input = screen.getByRole('textbox')
await user.click(input)
expect(input).toHaveFocus()
await waitFor(() => {
const dropdown = document.querySelector('[data-dropdown="model-combobox"]')
expect(dropdown).toBeInTheDocument()
})
})
it('filters models based on input value', async () => {
const user = userEvent.setup()
const localMockOnChange = vi.fn()
render(<ModelCombobox {...defaultProps} onChange={localMockOnChange} />)
const input = screen.getByRole('textbox')
await user.type(input, 'gpt-4')
expect(localMockOnChange).toHaveBeenCalledWith('gpt-4')
})
it('shows filtered models in dropdown when typing', async () => {
const user = userEvent.setup()
render(<ModelCombobox {...defaultProps} />)
const input = screen.getByRole('textbox')
// Type 'gpt' to trigger dropdown opening
await user.type(input, 'gpt')
await waitFor(() => {
// Dropdown should be open
const dropdown = document.querySelector('[data-dropdown="model-combobox"]')
expect(dropdown).toBeInTheDocument()
// Should show GPT models
expect(screen.getByText('gpt-3.5-turbo')).toBeInTheDocument()
expect(screen.getByText('gpt-4')).toBeInTheDocument()
// Should not show Claude
expect(screen.queryByText('claude-3-haiku')).not.toBeInTheDocument()
})
})
it('handles case insensitive filtering', async () => {
const user = userEvent.setup()
render(<ModelCombobox {...defaultProps} />)
const input = screen.getByRole('textbox')
await user.type(input, 'GPT')
expect(mockOnChange).toHaveBeenCalledWith('GPT')
})
it('shows empty state when no models match filter', async () => {
const user = userEvent.setup()
render(<ModelCombobox {...defaultProps} />)
const input = screen.getByRole('textbox')
// Type something that doesn't match any model to trigger dropdown + empty state
await user.type(input, 'nonexistent')
await waitFor(() => {
// Dropdown should be open
const dropdown = document.querySelector('[data-dropdown="model-combobox"]')
expect(dropdown).toBeInTheDocument()
// Should show empty state message
expect(screen.getByText('No models found for "nonexistent"')).toBeInTheDocument()
})
})
it('selects model from dropdown when clicked', async () => {
const user = userEvent.setup()
const localMockOnChange = vi.fn()
render(<ModelCombobox {...defaultProps} onChange={localMockOnChange} />)
const input = screen.getByRole('textbox')
await user.click(input)
await waitFor(() => {
const modelOption = screen.getByText('gpt-4')
expect(modelOption).toBeInTheDocument()
})
const modelOption = screen.getByText('gpt-4')
await user.click(modelOption)
expect(localMockOnChange).toHaveBeenCalledWith('gpt-4')
expect(input).toHaveValue('gpt-4')
})
it('submits input value with Enter key', async () => {
const user = userEvent.setup()
const localMockOnChange = vi.fn()
render(<ModelCombobox {...defaultProps} onChange={localMockOnChange} />)
const input = screen.getByRole('textbox')
await user.type(input, 'gpt')
await user.keyboard('{Enter}')
expect(localMockOnChange).toHaveBeenCalledWith('gpt')
})
it('displays error message in dropdown', async () => {
const user = userEvent.setup()
render(<ModelCombobox {...defaultProps} error="Network connection failed" />)
const input = screen.getByRole('textbox')
// Click input to open dropdown
await user.click(input)
await waitFor(() => {
// Dropdown should be open
const dropdown = document.querySelector('[data-dropdown="model-combobox"]')
expect(dropdown).toBeInTheDocument()
// Error messages should be displayed
expect(screen.getByText('Failed to load models')).toBeInTheDocument()
expect(screen.getByText('Network connection failed')).toBeInTheDocument()
})
})
it('calls onRefresh when refresh button is clicked', async () => {
const user = userEvent.setup()
const localMockOnRefresh = vi.fn()
render(<ModelCombobox {...defaultProps} error="Network error" onRefresh={localMockOnRefresh} />)
const input = screen.getByRole('textbox')
// Click input to open dropdown
await user.click(input)
await waitFor(() => {
// Dropdown should be open with error section
const dropdown = document.querySelector('[data-dropdown="model-combobox"]')
expect(dropdown).toBeInTheDocument()
const refreshButton = document.querySelector('[aria-label="Refresh models"]')
expect(refreshButton).toBeInTheDocument()
})
const refreshButton = document.querySelector('[aria-label="Refresh models"]')
if (refreshButton) {
await user.click(refreshButton)
expect(localMockOnRefresh).toHaveBeenCalledTimes(1)
}
})
it('opens dropdown when pressing ArrowDown', async () => {
const user = userEvent.setup()
render(<ModelCombobox {...defaultProps} />)
const input = screen.getByRole('textbox')
input.focus()
await user.keyboard('{ArrowDown}')
expect(input).toHaveFocus()
await waitFor(() => {
const dropdown = document.querySelector('[data-dropdown="model-combobox"]')
expect(dropdown).toBeInTheDocument()
})
})
it('navigates through models with arrow keys', async () => {
const user = userEvent.setup()
render(<ModelCombobox {...defaultProps} />)
const input = screen.getByRole('textbox')
input.focus()
// ArrowDown should open dropdown
await user.keyboard('{ArrowDown}')
await waitFor(() => {
// Dropdown should be open
const dropdown = document.querySelector('[data-dropdown="model-combobox"]')
expect(dropdown).toBeInTheDocument()
})
// Navigate to second item
await user.keyboard('{ArrowDown}')
await waitFor(() => {
const secondModel = screen.getByText('gpt-4')
const modelElement = secondModel.closest('[data-model]')
expect(modelElement).toHaveClass('bg-main-view-fg/20')
})
})
it('handles Enter key to select highlighted model', async () => {
const user = userEvent.setup()
const localMockOnChange = vi.fn()
render(<ModelCombobox {...defaultProps} onChange={localMockOnChange} />)
const input = screen.getByRole('textbox')
// Type 'gpt' to open dropdown and filter models
await user.type(input, 'gpt')
await waitFor(() => {
// Dropdown should be open with filtered models
const dropdown = document.querySelector('[data-dropdown="model-combobox"]')
expect(dropdown).toBeInTheDocument()
})
// Navigate to highlight first model and select it
await user.keyboard('{ArrowDown}')
await user.keyboard('{Enter}')
expect(localMockOnChange).toHaveBeenCalledWith('gpt-3.5-turbo')
})
})