Files
malio-layer-ui/app/components/malio/input/InputAmount.test.ts
T
tristan 336cb9e315 feat(ui) : saisie clavier MalioDate + bouton « + » InputEmail + séparateurs InputAmount (#MUI-42) (#68)
Cette PR regroupe **trois évolutions** de la librairie (retours ERP).

---

## 1. MalioDate — saisie manuelle au clavier

Ajoute la **saisie manuelle au clavier** `JJ/MM/AAAA` sur `MalioDate` (opt-in via la prop `editable`), en plus de la sélection au calendrier.

- `CalendarField` (interne) gagne un mode `editable` : input non `readonly`, masque maska `##/##/####`, buffer local synchronisé sur la valeur, event `commit` au blur / à Entrée.
- `MalioDate` parse le texte (`parseDisplayToIso`), valide les bornes (`isDateInRange`) et gère un état d'erreur interne fusionné avec la prop `error` du consommateur.
- Le focus ouvre le popover ; la saisie invalide/hors bornes conserve le texte et affiche un message (`invalidMessage`, défaut `Date invalide`) ; la sélection au calendrier ou un changement externe de `modelValue` efface l'erreur.
- **Aucune régression** : `editable` défaut `false` ; le reste de la famille Date (DateRange/DateTime/DateWeek) est inchangé.

Nouvelles props `MalioDate` : `editable` (boolean, défaut false), `invalidMessage` (string, défaut Date invalide).

---

## 2. MalioInputEmail — bouton « + » d'ajout

Ajoute à `MalioInputEmail` le même bouton « + » que `MalioInputPhone` : un bouton optionnel qui émet un event `add` (ex. pour ajouter dynamiquement un autre champ email).

- Props `addable` (défaut `false`), `addIconName` (défaut `mdi:plus`), `addButtonLabel` (défaut `Ajouter une adresse email`) ; nouvel event `add()`.
- L'icône email étant à droite par défaut, une computed `effectiveIconPosition` la **déplace automatiquement à gauche** quand `addable` est actif, libérant la droite pour le bouton.
- Le bouton respecte `disabled`/`readonly` (pas d'émission).
- **Aucune régression** : `addable` défaut `false` ; la logique de sanitisation email (espaces, `lowercase`, caret) est intacte.

---

## 3. MalioInputAmount — séparateurs de milliers

Affiche les montants groupés à la française (`1 234 567,89` : espace pour les milliers, virgule décimale), **en temps réel** pendant la saisie, tout en gardant une valeur émise propre.

- La valeur émise (`modelValue`) reste une **chaîne numérique propre** : point décimal, sans espaces (`'1234567.89'`). Contrat consommateur inchangé.
- Fonctions pures extraites dans `composables/amountFormat.ts` (`normalizeAmount`, `formatGroupedAmount`, helpers curseur) — testées en isolation.
- À la frappe : parse → émission du modèle propre → reformatage groupé → repositionnement du curseur (comptage des caractères significatifs hors espaces).
- `maxLength` borne désormais la **longueur du modèle** (le `maxlength` natif, qui compterait les espaces, est retiré).
- **Activé par défaut** sur tous les `MalioInputAmount` ; format FR figé.

---

Spec et plan des trois features : `docs/superpowers/specs/` et `docs/superpowers/plans/`.

## Plan de test
- [x] `npm run test -- Date.test.ts` → 40 tests OK
- [x] `npm run test -- InputEmail.test.ts` → 52 tests OK
- [x] `npm run test -- amountFormat.test.ts InputAmount.test.ts` → 50 tests OK
- [x] `npm run lint` → 0 erreur
- [ ] Vérif manuelle playground `composant/date` : saisie valide → ISO ; `32/13/2026` → texte conservé + rouge ; sélection calendrier efface l'erreur
- [ ] Vérif manuelle playground `composant/input/inputEmail` : carte « Ajout dynamique » → le « + » ajoute un champ ; icône à gauche + bouton à droite
- [ ] Vérif manuelle playground `composant/input/inputAmount` : carte « Grand montant » → `1234567` s'affiche `1 234 567` en live, `modelValue` émis `1234567` ; curseur cohérent

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Reviewed-on: #68
Co-authored-by: tristan <tristan@yuno.malio.fr>
Co-committed-by: tristan <tristan@yuno.malio.fr>
2026-06-09 15:39:38 +00:00

282 lines
9.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import {describe, expect, it} from 'vitest'
import {mount} from '@vue/test-utils'
import type {DefineComponent} from 'vue'
import InputAmount from './InputAmount.vue'
type InputAmountProps = {
id?: string
label?: string
name?: string
autocomplete?: string
modelValue?: string | null
inputClass?: string
labelClass?: string
groupClass?: string
required?: boolean
maxLength?: number | string
minLength?: number | string
disabled?: boolean
readonly?: boolean
hint?: string
error?: string
success?: string
iconName?: string
iconPosition?: 'left' | 'right'
iconSize?: string | number
iconColor?: string
reserveMessageSpace?: boolean
}
const InputAmountForTest = InputAmount as DefineComponent<InputAmountProps>
const mountInputAmount = (props: InputAmountProps = {}) =>
mount(InputAmountForTest, {
props,
global: {
stubs: {
IconifyIcon: {
template: '<span data-test="icon" v-bind="$attrs" />',
},
},
},
})
describe('MalioInputAmount', () => {
it('renders as a text input with decimal input mode', () => {
const wrapper = mountInputAmount()
expect(wrapper.get('input').attributes('type')).toBe('text')
expect(wrapper.get('input').attributes('inputmode')).toBe('decimal')
})
it('renders the default icon with muted styling', () => {
const wrapper = mountInputAmount()
expect(wrapper.get('[data-test="icon"]').exists()).toBe(true)
expect(wrapper.get('[data-test="icon"]').classes()).toContain('text-m-muted')
expect(wrapper.get('[data-test="icon"]').classes()).toContain('right-[10px]')
})
it('generates an amount-specific id', () => {
const wrapper = mountInputAmount({label: 'Montant'})
const inputId = wrapper.get('input').attributes('id')
expect(inputId?.startsWith('malio-input-amount-')).toBe(true)
expect(wrapper.get('label').attributes('for')).toBe(inputId)
})
it('applies the provided input classes', () => {
const wrapper = mountInputAmount({inputClass: 'text-right'})
expect(wrapper.get('input').classes()).toContain('text-right')
})
it('links hint text through aria-describedby', () => {
const wrapper = mountInputAmount({hint: 'Saisissez un montant'})
const inputId = wrapper.get('input').attributes('id')
expect(wrapper.get('input').attributes('aria-describedby')).toBe(`${inputId}-describedby`)
expect(wrapper.get('p').attributes('id')).toBe(`${inputId}-describedby`)
})
it('sets aria-invalid and describedby when showing an error', () => {
const wrapper = mountInputAmount({error: 'Montant invalide'})
const inputId = wrapper.get('input').attributes('id')
expect(wrapper.get('input').attributes('aria-invalid')).toBe('true')
expect(wrapper.get('input').attributes('aria-describedby')).toBe(`${inputId}-describedby`)
expect(wrapper.get('p.text-m-danger').text()).toBe('Montant invalide')
})
it('keeps dots as the decimal separator on input', async () => {
const wrapper = mountInputAmount({modelValue: ''})
await wrapper.get('input').setValue('12.5')
expect(wrapper.emitted('update:modelValue')?.[0]).toEqual(['12.5'])
expect(wrapper.get('input').element.value).toBe('12,5')
})
it('accepts commas but normalizes them to dots', async () => {
const wrapper = mountInputAmount({modelValue: ''})
await wrapper.get('input').setValue('0012,345abc')
expect(wrapper.emitted('update:modelValue')?.[0]).toEqual(['12.34'])
expect(wrapper.get('input').element.value).toBe('12,34')
})
it('normalizes a leading decimal separator', async () => {
const wrapper = mountInputAmount({modelValue: ''})
await wrapper.get('input').setValue(',5')
expect(wrapper.emitted('update:modelValue')?.[0]).toEqual(['0.5'])
expect(wrapper.get('input').element.value).toBe('0,5')
})
it('keeps the normalized decimal value on blur', async () => {
const wrapper = mountInputAmount()
const input = wrapper.get('input')
await input.setValue('12.5')
await input.trigger('blur')
expect(wrapper.emitted('update:modelValue')).toEqual([['12.5']])
expect(input.element.value).toBe('12,5')
})
it('keeps integer values unchanged on blur', async () => {
const wrapper = mountInputAmount()
const input = wrapper.get('input')
await input.setValue('12')
await input.trigger('blur')
expect(wrapper.emitted('update:modelValue')).toEqual([['12']])
expect(input.element.value).toBe('12')
})
it('keeps an empty value empty on blur', async () => {
const wrapper = mountInputAmount()
const input = wrapper.get('input')
await input.setValue('')
await input.trigger('blur')
expect(wrapper.emitted('update:modelValue')).toEqual([['']])
expect(input.element.value).toBe('')
})
it('supports icon positioning on the left', () => {
const wrapper = mountInputAmount({
label: 'Montant',
iconPosition: 'left',
})
expect(wrapper.get('[data-test="icon"]').classes()).toContain('left-[10px]')
expect(wrapper.get('input').classes()).toContain('!pl-11')
expect(wrapper.get('label').classes()).toContain('left-11')
})
it('shows primary icon color on focus', async () => {
const wrapper = mountInputAmount()
await wrapper.get('input').trigger('focus')
expect(wrapper.get('[data-test="icon"]').classes()).toContain('text-m-primary')
})
it('shows black icon color when filled and unfocused', () => {
const wrapper = mountInputAmount({modelValue: '12,50'})
expect(wrapper.get('[data-test="icon"]').classes()).toContain('text-black')
})
it('affiche l\'astérisque quand required est vrai', () => {
const wrapper = mountInputAmount({label: 'Champ', required: true})
expect(wrapper.find('[data-test="required-mark"]').exists()).toBe(true)
})
it('n\'affiche pas l\'astérisque par défaut', () => {
const wrapper = mountInputAmount({label: 'Champ'})
expect(wrapper.find('[data-test="required-mark"]').exists()).toBe(false)
})
it('readonly : bordure noire même vide, pas de grow/bleu', () => {
const wrapper = mountInputAmount({label: 'Champ', readonly: true})
const field = wrapper.get('input')
expect(field.classes()).toContain('border-black')
expect(field.classes()).not.toContain('border-m-muted')
expect(field.classes()).not.toContain('focus:border-m-primary')
expect(field.classes()).not.toContain('grow-height')
})
it('readonly vide : label gris, pas de bleu', () => {
const wrapper = mountInputAmount({label: 'Champ', readonly: true})
expect(wrapper.get('label').classes()).not.toContain('peer-focus:text-m-primary')
expect(wrapper.get('label').classes()).toContain('text-m-muted')
})
it('readonly vide : icône en text-m-muted', () => {
const wrapper = mountInputAmount({label: 'Champ', readonly: true})
expect(wrapper.get('[data-test="icon"]').classes()).toContain('text-m-muted')
})
it('readonly rempli : label noir et icône noire', () => {
const wrapper = mountInputAmount({label: 'Champ', readonly: true, modelValue: '12.50'})
expect(wrapper.get('label').classes()).toContain('text-black')
expect(wrapper.get('[data-test="icon"]').classes()).toContain('text-black')
})
it('réserve lespace message par défaut même sans message', () => {
const wrapper = mountInputAmount({label: 'Champ'})
const msg = wrapper.find('[id$="-describedby"]')
expect(msg.exists()).toBe(true)
expect(msg.classes()).toContain('min-h-[1rem]')
})
it('reserveMessageSpace=false sans message : pas de ligne réservée', () => {
const wrapper = mountInputAmount({label: 'Champ', reserveMessageSpace: false})
expect(wrapper.find('[id$="-describedby"]').exists()).toBe(false)
})
it('reserveMessageSpace=false avec message : ligne rendue sans min-h', () => {
const wrapper = mountInputAmount({label: 'Champ', reserveMessageSpace: false, error: 'Erreur'})
const msg = wrapper.find('[id$="-describedby"]')
expect(msg.exists()).toBe(true)
expect(msg.classes()).not.toContain('min-h-[1rem]')
})
it('groupe les milliers à l\'affichage tout en émettant la valeur propre', async () => {
const wrapper = mountInputAmount({modelValue: ''})
await wrapper.get('input').setValue('1234567')
expect(wrapper.emitted('update:modelValue')?.at(-1)).toEqual(['1234567'])
expect(wrapper.get('input').element.value).toBe('1 234 567')
})
it('groupe un grand montant avec décimales', async () => {
const wrapper = mountInputAmount({modelValue: ''})
await wrapper.get('input').setValue('1234567,89')
expect(wrapper.emitted('update:modelValue')?.at(-1)).toEqual(['1234567.89'])
expect(wrapper.get('input').element.value).toBe('1 234 567,89')
})
it('formate la valeur initiale (modelValue) en groupé', () => {
const wrapper = mountInputAmount({modelValue: '1234567.89'})
expect(wrapper.get('input').element.value).toBe('1 234 567,89')
})
it('maxLength borne la longueur du modèle : un dépassement est ignoré', async () => {
const wrapper = mountInputAmount({modelValue: '', maxLength: 4})
await wrapper.get('input').setValue('12345')
expect(wrapper.emitted('update:modelValue')).toBeUndefined()
expect(wrapper.get('input').element.value).toBe('')
})
it('maxLength autorise une valeur à la limite', async () => {
const wrapper = mountInputAmount({modelValue: '', maxLength: 4})
await wrapper.get('input').setValue('1234')
expect(wrapper.emitted('update:modelValue')?.at(-1)).toEqual(['1234'])
expect(wrapper.get('input').element.value).toBe('1 234')
})
it('n\'a plus d\'attribut maxlength natif sur l\'input', () => {
const wrapper = mountInputAmount({maxLength: 4})
expect(wrapper.get('input').attributes('maxlength')).toBeUndefined()
})
})