-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Expand file tree
/
Copy pathNotebookScopeSelector.test.tsx
More file actions
59 lines (44 loc) · 2.22 KB
/
Copy pathNotebookScopeSelector.test.tsx
File metadata and controls
59 lines (44 loc) · 2.22 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
import { render, screen, fireEvent } from '@testing-library/react'
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { NotebookScopeSelector } from './NotebookScopeSelector'
import { useNotebooks } from '@/lib/hooks/use-notebooks'
// useTranslation is mocked globally in setup.ts (t returns the key string)
vi.mock('@/lib/hooks/use-notebooks', () => ({
useNotebooks: vi.fn(),
}))
const mockUseNotebooks = vi.mocked(useNotebooks)
const notebooks = [
{ id: 'notebook:a', name: 'Alpha', description: '' },
{ id: 'notebook:b', name: 'Beta', description: 'second' },
]
describe('NotebookScopeSelector', () => {
beforeEach(() => {
vi.clearAllMocks()
mockUseNotebooks.mockReturnValue({ data: notebooks, isLoading: false } as ReturnType<typeof useNotebooks>)
})
it('reads as "all notebooks" when nothing is selected and hides the clear action', () => {
render(<NotebookScopeSelector selectedIds={[]} onChange={vi.fn()} />)
expect(screen.getByText('searchPage.scopeAllNotebooks')).toBeInTheDocument()
expect(screen.queryByText('searchPage.scopeClear')).not.toBeInTheDocument()
})
it('adds a notebook to the scope when its checkbox is toggled', () => {
const onChange = vi.fn()
render(<NotebookScopeSelector selectedIds={['notebook:a']} onChange={onChange} />)
fireEvent.click(screen.getByRole('button', { name: /searchPage.scopeNotebooks/ }))
fireEvent.click(screen.getByRole('checkbox', { name: /Beta/ }))
expect(onChange).toHaveBeenCalledWith(['notebook:a', 'notebook:b'])
})
it('removes an already selected notebook when toggled again', () => {
const onChange = vi.fn()
render(<NotebookScopeSelector selectedIds={['notebook:a', 'notebook:b']} onChange={onChange} />)
fireEvent.click(screen.getByRole('button', { name: /searchPage.scopeNotebooks/ }))
fireEvent.click(screen.getByRole('checkbox', { name: /Alpha/ }))
expect(onChange).toHaveBeenCalledWith(['notebook:b'])
})
it('clears the whole scope from the clear action', () => {
const onChange = vi.fn()
render(<NotebookScopeSelector selectedIds={['notebook:a']} onChange={onChange} />)
fireEvent.click(screen.getByText('searchPage.scopeClear'))
expect(onChange).toHaveBeenCalledWith([])
})
})