refactor(notes): extract title validation into domain service

This commit is contained in:
devcodetools committed 2026-09-13 16:58:12 +02:00
1 parent 557e48116a
commit 5aeeb21f7f
3 files changed
+37 -3

No files matched your search

+4 -3
View File
@@ -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 {
+26
View File
@@ -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')
})
})
+7
View File
@@ -0,0 +1,7 @@
export function isTitleValid(title: string): boolean {
return title.trim().length > 0
}
export function normalizeTitle(title: string): string {
return title.trim()
}