diff --git a/src/components/NoteEditor.tsx b/src/components/NoteEditor.tsx index e4e7db7..cf7bb7d 100644 --- a/src/components/NoteEditor.tsx +++ b/src/components/NoteEditor.tsx @@ -3,6 +3,7 @@ import type { NoteEditorProps, NoteFormProps, } from '../interfaces/note-editor' +import { isTitleValid, normalizeTitle } from '../lib/noteValidation' import DeleteModal from './DeleteModal' const TITLE_REQUIRED_MESSAGE = 'Le titre est obligatoire.' @@ -82,15 +83,15 @@ function NoteEditor(props: NoteEditorProps) { const handleTitleChange = (value: string) => { setTitle(value) - if (titleMissing && value.trim() !== '') setTitleMissing(false) + if (titleMissing && isTitleValid(value)) setTitleMissing(false) } const handleSubmit = () => { - if (title.trim() === '') { + if (!isTitleValid(title)) { setTitleMissing(true) return } - const safeTitle = title.trim() + const safeTitle = normalizeTitle(title) if (props.mode === 'create') { props.onCreate(safeTitle, content) } else { diff --git a/src/lib/noteValidation.test.ts b/src/lib/noteValidation.test.ts new file mode 100644 index 0000000..49f0a38 --- /dev/null +++ b/src/lib/noteValidation.test.ts @@ -0,0 +1,26 @@ +import { describe, expect, it } from 'vitest' +import { isTitleValid, normalizeTitle } from './noteValidation' + +describe('isTitleValid', () => { + it('accepts a non-empty title', () => { + expect(isTitleValid('Courses')).toBe(true) + }) + + it('rejects an empty title', () => { + expect(isTitleValid('')).toBe(false) + }) + + it('rejects a whitespace-only title', () => { + expect(isTitleValid(' ')).toBe(false) + }) +}) + +describe('normalizeTitle', () => { + it('trims surrounding whitespace', () => { + expect(normalizeTitle(' Courses ')).toBe('Courses') + }) + + it('keeps an already trimmed title unchanged', () => { + expect(normalizeTitle('Courses')).toBe('Courses') + }) +}) \ No newline at end of file diff --git a/src/lib/noteValidation.ts b/src/lib/noteValidation.ts new file mode 100644 index 0000000..ff77495 --- /dev/null +++ b/src/lib/noteValidation.ts @@ -0,0 +1,7 @@ +export function isTitleValid(title: string): boolean { + return title.trim().length > 0 +} + +export function normalizeTitle(title: string): string { + return title.trim() +} \ No newline at end of file