Files
DeepTutor/web/tests/notes-learning-interaction.spec.tsx
Bingxi Zhao (Frank) 857a82e74a feat(notes): add block notebooks with inline reviews and linked favorites
Add rich text and learning blocks, media uploads, page trees, and revisioned notebook storage. Present AI suggestions at their target blocks with durable accept and dismiss actions.

Unify saved outputs as Favorites with typed expandable blocks, stable source links, atomic batch insertion, nesting, dragging, and undo/redo.

Validation: 99 frontend tests and 99 backend tests pass in an isolated snapshot, along with TypeScript, scoped ESLint, dependency architecture, and locale parity checks.
2026-10-17 23:15:23 +08:00

69 lines
2.5 KiB
TypeScript

import { act, fireEvent, render, screen } from '@testing-library/react'
import { beforeEach, expect, it, vi } from 'vitest'
import NoteLearningContent from '@/features/notes/components/NoteLearningContent'
import { upsertNotebookEntry } from '@/lib/notebook-api'
vi.mock('react-i18next', () => ({ useTranslation: () => ({ t: (key: string) => key }) }))
vi.mock('@/lib/notebook-api', () => ({ upsertNotebookEntry: vi.fn().mockResolvedValue({}) }))
vi.mock('@/app/(workspace)/learning/books/components/blocks/QuizBlock', () => ({
default: ({ onAttempt }: { onAttempt: (id: string, args: unknown) => void }) => (
<button onClick={() => onAttempt('q', { questionId: 'q:1', userAnswer: 'A', isCorrect: true })}>
Answer quiz
</button>
),
}))
let intersection: IntersectionObserverCallback
beforeEach(() => {
vi.stubGlobal(
'IntersectionObserver',
class {
constructor(callback: IntersectionObserverCallback) {
intersection = callback
}
observe() {}
disconnect() {}
unobserve() {}
}
)
})
const base = {
id: 'q',
kind: 'quiz',
prompt: 'Teach this',
markdown: '',
sourceUrl: '',
data: JSON.stringify({
questions: [{ question: 'Light?', question_id: 'q:1', correct_answer: 'A' }],
}),
}
const visible = (isIntersecting: boolean) =>
act(() =>
intersection([{ isIntersecting } as IntersectionObserverEntry], {} as IntersectionObserver)
)
it('keeps rendered learning content mounted after scrolling away', async () => {
render(<NoteLearningContent {...base} onChange={vi.fn()} />)
visible(true)
await screen.findByRole('button', { name: 'Answer quiz' })
visible(false)
expect(screen.getByRole('button', { name: 'Answer quiz' })).toBeInTheDocument()
})
it('allows quiz preview without writing attempts or the question notebook', async () => {
const onChange = vi.fn()
render(<NoteLearningContent {...base} readOnly onChange={onChange} />)
visible(true)
fireEvent.click(await screen.findByRole('button', { name: 'Answer quiz' }))
expect(onChange).not.toHaveBeenCalled()
expect(upsertNotebookEntry).not.toHaveBeenCalled()
})
it('persists real quiz answers in the learning block', async () => {
const onChange = vi.fn()
render(<NoteLearningContent {...base} onChange={onChange} />)
visible(true)
fireEvent.click(await screen.findByRole('button', { name: 'Answer quiz' }))
expect(JSON.parse(onChange.mock.calls[0][0].data).attempts[0]).toMatchObject({
question_id: 'q:1',
user_answer: 'A',
is_correct: true,
})
expect(upsertNotebookEntry).toHaveBeenCalledOnce()
})