diff --git a/src/components/NoteList.test.tsx b/src/components/NoteList.test.tsx new file mode 100644 index 0000000..00894b4 --- /dev/null +++ b/src/components/NoteList.test.tsx @@ -0,0 +1,41 @@ +import { fireEvent, render, screen } from '@testing-library/react' +import { describe, expect, it, vi } from 'vitest' +import NoteList from './NoteList' +import type { Note } from '../types' + +const notes: Note[] = [ + { id: 3, title: 'Courses', content: 'Lait' }, + { id: 1, title: 'Pense-bête', content: '' }, + { id: 2, title: '', content: 'vide sans titre' }, +] + +describe('NoteList', () => { + it('renders the title of each note', () => { + render() + + expect(screen.getByText('Courses')).toBeInTheDocument() + expect(screen.getByText('Pense-bête')).toBeInTheDocument() + }) + + it('shows a placeholder for notes without a title', () => { + render() + + expect(screen.getByText('Sans titre')).toBeInTheDocument() + }) + + it('calls onSelect with the note id when clicked', () => { + const onSelect = vi.fn() + + render() + + fireEvent.click(screen.getByText('Courses')) + + expect(onSelect).toHaveBeenCalledWith(3) + }) + + it('renders an empty state message when there are no notes', () => { + render() + + expect(screen.getByText(/aucune note/i)).toBeInTheDocument() + }) +}) diff --git a/src/components/NoteList.tsx b/src/components/NoteList.tsx new file mode 100644 index 0000000..63663e7 --- /dev/null +++ b/src/components/NoteList.tsx @@ -0,0 +1,32 @@ +import type { Note } from '../types' + +interface NoteListProps { + notes: Note[] + onSelect: (id: number) => void +} + +const EMPTY_MESSAGE = 'Aucune note pour le moment. Touchez + pour en créer une.' + +function NoteList({ notes, onSelect }: NoteListProps) { + if (notes.length === 0) { + return

{EMPTY_MESSAGE}

+ } + + return ( +
    + {notes.map((note) => ( +
  • + +
  • + ))} +
+ ) +} + +export default NoteList