887ebdebd7
## Résumé (MUI-41) Harmonise l'état « obligatoire » des composants de formulaire et normalise le champ email. ### `required` + astérisque - Nouveau composant partagé `MalioRequiredMark` : astérisque rouge (`text-m-danger`, **16px**), `aria-hidden`. - Prop `required` désormais cohérente sur toute la famille formulaire ; quand vraie, l'astérisque s'affiche **dans le label**. - Prop ajoutée à `Select`, `SelectCheckbox`, `InputUpload`, `InputRichText` (les autres l'avaient déjà). - Accessibilité : `required` natif là où l'élément le supporte, sinon `aria-required` (Select/SelectCheckbox sur le `<button>`, RichText sur le wrapper éditeur, Upload sur le champ visible). - `MalioSiteSelector` **exclu** volontairement (segmented control, pas de label de champ). ### Sanitisation email (`MalioInputEmail`) - Suppression de **tous les espaces** à la saisie (pas de masque). - Nouvelle prop opt-in `lowercase` (défaut `false`) : normalise en minuscules à la frappe (cohérent RG-1.21 Starseed). - Garde défensive curseur : l'API de sélection est interdite sur `type="email"` → repositionnement best-effort sans jamais lever. - La validation de format reste à la couche `error`. ### Docs & playground - `COMPONENTS.md` (doc `required` cohérente + note famille + `lowercase`) et `CHANGELOG.md` mis à jour. - Exemples playground `required` et email `lowercase` ajoutés. ## Test plan - [x] Suite complète : 42 fichiers / 771 tests verts - [x] Lint : 0 erreur - [x] Tests `aria-required` sur Select/SelectCheckbox/RichText - [ ] Vérif visuelle playground : astérisque 16px dans le label, email qui retire les espaces / minuscule Spec & plan : `docs/superpowers/specs/` et `docs/superpowers/plans/`. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Reviewed-on: #60 Co-authored-by: tristan <tristan@yuno.malio.fr> Co-committed-by: tristan <tristan@yuno.malio.fr>
199 lines
6.2 KiB
TypeScript
199 lines
6.2 KiB
TypeScript
import {describe, expect, it} from 'vitest'
|
||
import {mount} from '@vue/test-utils'
|
||
import type {DefineComponent} from 'vue'
|
||
import InputNumber from './InputNumber.vue'
|
||
|
||
type InputNumberProps = {
|
||
modelValue?: string | null
|
||
label?: string
|
||
required?: boolean
|
||
readonly?: boolean
|
||
min?: number | string
|
||
max?: number | string
|
||
error?: string
|
||
hint?: string
|
||
reserveMessageSpace?: boolean
|
||
}
|
||
|
||
const InputNumberForTest = InputNumber as DefineComponent<InputNumberProps>
|
||
|
||
const mountInputNumber = (props: InputNumberProps = {}) =>
|
||
mount(InputNumberForTest, {
|
||
props,
|
||
global: {
|
||
stubs: {
|
||
IconifyIcon: {
|
||
template: '<span data-test="icon" v-bind="$attrs" />',
|
||
},
|
||
},
|
||
},
|
||
})
|
||
|
||
describe('MalioInputNumber', () => {
|
||
it('renders the input with a fixed 22px height', () => {
|
||
const wrapper = mountInputNumber()
|
||
const input = wrapper.get('input')
|
||
|
||
expect(input.classes()).toContain('h-[22px]')
|
||
})
|
||
|
||
it('renders the increment and decrement buttons with a fixed 20px height', () => {
|
||
const wrapper = mountInputNumber()
|
||
const buttons = wrapper.findAll('button')
|
||
|
||
expect(buttons).toHaveLength(2)
|
||
})
|
||
|
||
it('still emits update:modelValue on input', async () => {
|
||
const wrapper = mountInputNumber({modelValue: ''})
|
||
const input = wrapper.get('input')
|
||
|
||
await input.setValue('99')
|
||
|
||
expect(wrapper.emitted('update:modelValue')?.[0]).toEqual(['99'])
|
||
})
|
||
|
||
it('filters letters from the input value', async () => {
|
||
const wrapper = mountInputNumber({modelValue: ''})
|
||
const input = wrapper.get('input')
|
||
|
||
await input.setValue('a1b2c3')
|
||
|
||
expect(wrapper.emitted('update:modelValue')?.[0]).toEqual(['123'])
|
||
expect(input.element.value).toBe('123')
|
||
})
|
||
|
||
it('formats large numbers with spaces in the input display', async () => {
|
||
const wrapper = mountInputNumber({modelValue: ''})
|
||
const input = wrapper.get('input')
|
||
|
||
await input.setValue('1000000')
|
||
|
||
expect(wrapper.emitted('update:modelValue')?.[0]).toEqual(['1000000'])
|
||
expect(input.element.value).toBe('1 000 000')
|
||
})
|
||
|
||
it('accepts decimal values with commas', async () => {
|
||
const wrapper = mountInputNumber({modelValue: ''})
|
||
const input = wrapper.get('input')
|
||
|
||
await input.setValue('12,5')
|
||
|
||
expect(wrapper.emitted('update:modelValue')?.[0]).toEqual(['12.5'])
|
||
expect(input.element.value).toBe('12.5')
|
||
})
|
||
|
||
it('keeps a trailing decimal separator while typing', async () => {
|
||
const wrapper = mountInputNumber({modelValue: ''})
|
||
const input = wrapper.get('input')
|
||
|
||
await input.setValue('12,')
|
||
|
||
expect(wrapper.emitted('update:modelValue')?.[0]).toEqual(['12.'])
|
||
expect(input.element.value).toBe('12.')
|
||
})
|
||
|
||
it('accepts a decimal starting with a comma', async () => {
|
||
const wrapper = mountInputNumber({modelValue: ''})
|
||
const input = wrapper.get('input')
|
||
|
||
await input.setValue(',5')
|
||
|
||
expect(wrapper.emitted('update:modelValue')?.[0]).toEqual(['0.5'])
|
||
expect(input.element.value).toBe('0.5')
|
||
})
|
||
|
||
it('increments the current value when clicking plus', async () => {
|
||
const wrapper = mountInputNumber({modelValue: '2'})
|
||
|
||
await wrapper.findAll('button')[1].trigger('click')
|
||
|
||
expect(wrapper.emitted('update:modelValue')?.[0]).toEqual(['3'])
|
||
})
|
||
|
||
it('increments decimal values with a step of 1', async () => {
|
||
const wrapper = mountInputNumber({modelValue: '1.5'})
|
||
|
||
await wrapper.findAll('button')[1].trigger('click')
|
||
|
||
expect(wrapper.emitted('update:modelValue')?.[0]).toEqual(['2.5'])
|
||
})
|
||
|
||
it('decrements the current value when clicking minus', async () => {
|
||
const wrapper = mountInputNumber({modelValue: '2'})
|
||
|
||
await wrapper.findAll('button')[0].trigger('click')
|
||
|
||
expect(wrapper.emitted('update:modelValue')?.[0]).toEqual(['1'])
|
||
})
|
||
|
||
it('does not change the value from buttons when readonly', async () => {
|
||
const wrapper = mountInputNumber({modelValue: '2', readonly: true})
|
||
|
||
await wrapper.findAll('button')[1].trigger('click')
|
||
|
||
expect(wrapper.emitted('update:modelValue')).toBeUndefined()
|
||
})
|
||
|
||
it('disables minus and prevents decrement at min', async () => {
|
||
const wrapper = mountInputNumber({modelValue: '2', min: 2})
|
||
const minusButton = wrapper.findAll('button')[0]
|
||
|
||
expect(minusButton.attributes('disabled')).toBeDefined()
|
||
|
||
await minusButton.trigger('click')
|
||
|
||
expect(wrapper.emitted('update:modelValue')).toBeUndefined()
|
||
})
|
||
|
||
it('disables plus and prevents increment at max', async () => {
|
||
const wrapper = mountInputNumber({modelValue: '2', max: 2})
|
||
const plusButton = wrapper.findAll('button')[1]
|
||
|
||
expect(plusButton.attributes('disabled')).toBeDefined()
|
||
|
||
await plusButton.trigger('click')
|
||
|
||
expect(wrapper.emitted('update:modelValue')).toBeUndefined()
|
||
})
|
||
|
||
it('clamps manual input to max', async () => {
|
||
const wrapper = mountInputNumber({modelValue: '', max: 5})
|
||
const input = wrapper.get('input')
|
||
|
||
await input.setValue('12')
|
||
|
||
expect(wrapper.emitted('update:modelValue')?.[0]).toEqual(['5'])
|
||
expect(input.element.value).toBe('5')
|
||
})
|
||
|
||
it('affiche l\'astérisque quand required est vrai', () => {
|
||
const wrapper = mountInputNumber({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 = mountInputNumber({label: 'Champ'})
|
||
expect(wrapper.find('[data-test="required-mark"]').exists()).toBe(false)
|
||
})
|
||
|
||
it('réserve l’espace message par défaut même sans message', () => {
|
||
const wrapper = mountInputNumber({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 = mountInputNumber({label: 'Champ', reserveMessageSpace: false})
|
||
expect(wrapper.find('[id$="-describedby"]').exists()).toBe(false)
|
||
})
|
||
|
||
it('reserveMessageSpace=false avec message : ligne rendue sans min-h', () => {
|
||
const wrapper = mountInputNumber({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]')
|
||
})
|
||
})
|