cda0f994ca
Ajoute une prop booléenne reserveMessageSpace (défaut true) aux 10 composants de la famille input. Par défaut, comportement inchangé (ligne message toujours rendue avec min-h-[1rem]). À false, la ligne ne prend aucun espace en l'absence de message, et s'affiche sans min-h quand un message est présent. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
264 lines
9.2 KiB
TypeScript
264 lines
9.2 KiB
TypeScript
import {describe, expect, it, vi} from 'vitest'
|
||
import {mount} from '@vue/test-utils'
|
||
import type {DefineComponent} from 'vue'
|
||
import { Icon as IconifyIcon } from '@iconify/vue'
|
||
import InputUpload from './InputUpload.vue'
|
||
|
||
type InputUploadProps = {
|
||
id?: string
|
||
label?: string
|
||
modelValue?: string | null
|
||
inputClass?: string
|
||
labelClass?: string
|
||
groupClass?: string
|
||
disabled?: boolean
|
||
readonly?: boolean
|
||
hint?: string
|
||
error?: string
|
||
success?: string
|
||
displayIcon?: boolean
|
||
accept?: string
|
||
required?: boolean
|
||
reserveMessageSpace?: boolean
|
||
}
|
||
|
||
const InputUploadForTest = InputUpload as DefineComponent<InputUploadProps>
|
||
|
||
const mountComponent = (props: InputUploadProps = {}) =>
|
||
mount(InputUploadForTest, {
|
||
props,
|
||
global: {
|
||
stubs: {
|
||
IconifyIcon: {
|
||
template: '<span data-test="icon" v-bind="$attrs" />',
|
||
},
|
||
},
|
||
},
|
||
})
|
||
|
||
describe('MalioInputUpload', () => {
|
||
it('renders the initial display value', () => {
|
||
const wrapper = mountComponent({modelValue: 'document.pdf'})
|
||
|
||
expect(wrapper.get('input[type="text"]').element.value).toBe('document.pdf')
|
||
})
|
||
|
||
it('renders the label text', () => {
|
||
const wrapper = mountComponent({label: 'Téléverser un fichier'})
|
||
|
||
expect(wrapper.get('label').text()).toBe('Téléverser un fichier')
|
||
})
|
||
|
||
it('has a hidden file input', () => {
|
||
const wrapper = mountComponent()
|
||
|
||
expect(wrapper.find('input[type="file"]').exists()).toBe(true)
|
||
expect(wrapper.find('input[type="file"]').classes()).toContain('hidden')
|
||
})
|
||
|
||
it('text input is readonly', () => {
|
||
const wrapper = mountComponent()
|
||
|
||
expect(wrapper.get('input[type="text"]').attributes('readonly')).toBeDefined()
|
||
})
|
||
|
||
it('renders icon by default', () => {
|
||
const wrapper = mountComponent()
|
||
|
||
expect(wrapper.find('[data-test="icon"]').exists()).toBe(true)
|
||
})
|
||
|
||
it('does not render icon when displayIcon is false', () => {
|
||
const wrapper = mountComponent({displayIcon: false})
|
||
|
||
expect(wrapper.find('[data-test="icon"]').exists()).toBe(false)
|
||
})
|
||
|
||
it('shows the correct upload icon', () => {
|
||
const wrapper = mountComponent()
|
||
|
||
const iconComponent = wrapper.findComponent(IconifyIcon)
|
||
expect(iconComponent.props('icon')).toBe('mdi:cloud-arrow-up-outline')
|
||
})
|
||
|
||
it('emits update:modelValue when a file is selected', async () => {
|
||
const wrapper = mountComponent({modelValue: ''})
|
||
const fileInput = wrapper.find('input[type="file"]')
|
||
const file = new File(['content'], 'test.pdf', {type: 'application/pdf'})
|
||
|
||
Object.defineProperty(fileInput.element, 'files', {
|
||
value: [file],
|
||
})
|
||
await fileInput.trigger('change')
|
||
|
||
expect(wrapper.emitted('update:modelValue')?.[0]).toEqual(['test.pdf'])
|
||
})
|
||
|
||
it('emits file-selected with the File object when a file is selected', async () => {
|
||
const wrapper = mountComponent({modelValue: ''})
|
||
const fileInput = wrapper.find('input[type="file"]')
|
||
const file = new File(['content'], 'test.pdf', {type: 'application/pdf'})
|
||
|
||
Object.defineProperty(fileInput.element, 'files', {
|
||
value: [file],
|
||
})
|
||
await fileInput.trigger('change')
|
||
|
||
expect(wrapper.emitted('file-selected')?.[0]).toEqual([file])
|
||
})
|
||
|
||
it('sets disabled on both inputs when disabled is true', () => {
|
||
const wrapper = mountComponent({disabled: true})
|
||
|
||
expect(wrapper.get('input[type="text"]').attributes('disabled')).toBeDefined()
|
||
expect(wrapper.get('input[type="file"]').attributes('disabled')).toBeDefined()
|
||
expect(wrapper.get('input[type="text"]').classes()).toContain('cursor-not-allowed')
|
||
})
|
||
|
||
it('shows error message and styles', () => {
|
||
const wrapper = mountComponent({error: 'Fichier requis'})
|
||
|
||
expect(wrapper.get('p.text-m-danger').text()).toBe('Fichier requis')
|
||
expect(wrapper.get('input[type="text"]').classes()).toContain('border-m-danger')
|
||
expect(wrapper.get('input[type="text"]').attributes('aria-invalid')).toBe('true')
|
||
})
|
||
|
||
it('shows error style on icon', () => {
|
||
const wrapper = mountComponent({error: 'Error'})
|
||
|
||
expect(wrapper.get('[data-test="icon"]').classes()).toContain('text-m-danger')
|
||
})
|
||
|
||
it('shows success message and styles', () => {
|
||
const wrapper = mountComponent({success: 'Fichier valide'})
|
||
|
||
expect(wrapper.get('p.text-m-success').text()).toBe('Fichier valide')
|
||
expect(wrapper.get('input[type="text"]').classes()).toContain('border-m-success')
|
||
})
|
||
|
||
it('shows success style on icon', () => {
|
||
const wrapper = mountComponent({success: 'Success'})
|
||
|
||
expect(wrapper.get('[data-test="icon"]').classes()).toContain('text-m-success')
|
||
})
|
||
|
||
it('shows hint message', () => {
|
||
const wrapper = mountComponent({hint: 'PDF uniquement'})
|
||
|
||
expect(wrapper.get('p.text-m-muted').text()).toBe('PDF uniquement')
|
||
})
|
||
|
||
it('links label to input via for/id', () => {
|
||
const wrapper = mountComponent({id: 'upload', label: 'Fichier'})
|
||
|
||
expect(wrapper.get('input[type="text"]').attributes('id')).toBe('upload')
|
||
expect(wrapper.get('label').attributes('for')).toBe('upload')
|
||
})
|
||
|
||
it('generates an id when missing and reuses it on label', () => {
|
||
const wrapper = mountComponent({label: 'Fichier'})
|
||
|
||
const inputId = wrapper.get('input[type="text"]').attributes('id')
|
||
|
||
expect(inputId?.startsWith('malio-input-upload-')).toBe(true)
|
||
expect(wrapper.get('label').attributes('for')).toBe(inputId)
|
||
})
|
||
|
||
it('aria-invalid is false when no error', () => {
|
||
const wrapper = mountComponent()
|
||
|
||
expect(wrapper.get('input[type="text"]').attributes('aria-invalid')).toBe('false')
|
||
})
|
||
|
||
it('expose aria-required sur le champ visible quand required est vrai', () => {
|
||
const wrapper = mountComponent({label: 'Champ', required: true})
|
||
expect(wrapper.get('input[type="text"]').attributes('aria-required')).toBe('true')
|
||
})
|
||
|
||
it('passes accept attribute to file input', () => {
|
||
const wrapper = mountComponent({accept: '.pdf,.doc'})
|
||
|
||
expect(wrapper.get('input[type="file"]').attributes('accept')).toBe('.pdf,.doc')
|
||
})
|
||
|
||
it('shows primary icon color on focus', async () => {
|
||
const wrapper = mountComponent()
|
||
|
||
await wrapper.get('input[type="text"]').trigger('focus')
|
||
|
||
expect(wrapper.get('[data-test="icon"]').classes()).toContain('text-m-primary')
|
||
})
|
||
|
||
it('shows black icon color when filled and unfocused', () => {
|
||
const wrapper = mountComponent({modelValue: 'document.pdf'})
|
||
|
||
expect(wrapper.get('[data-test="icon"]').classes()).toContain('text-black')
|
||
})
|
||
|
||
it('affiche l\'astérisque quand required est vrai', () => {
|
||
const wrapper = mountComponent({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 = mountComponent({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 = mountComponent({label: 'Champ', readonly: true})
|
||
const field = wrapper.get('input[type="text"]')
|
||
expect(field.classes()).toContain('border-black')
|
||
expect(field.classes()).not.toContain('border-m-muted')
|
||
expect(field.classes()).not.toContain('grow-height')
|
||
expect(field.classes()).not.toContain('focus:border-m-primary')
|
||
})
|
||
|
||
it('readonly vide : label gris, pas de bleu', () => {
|
||
const wrapper = mountComponent({label: 'Champ', readonly: true})
|
||
const label = wrapper.get('label')
|
||
expect(label.classes()).not.toContain('peer-focus:text-m-primary')
|
||
expect(label.classes()).toContain('text-m-muted')
|
||
})
|
||
|
||
it('readonly vide : icône en text-m-muted', () => {
|
||
const wrapper = mountComponent({label: 'Champ', readonly: true})
|
||
expect(wrapper.get('[data-test="icon"]').classes()).toContain('text-m-muted')
|
||
})
|
||
|
||
it('readonly rempli : label noir + icône noire', () => {
|
||
const wrapper = mountComponent({label: 'Champ', readonly: true, modelValue: 'fichier.pdf'})
|
||
expect(wrapper.get('label').classes()).toContain('text-black')
|
||
expect(wrapper.get('[data-test="icon"]').classes()).toContain('text-black')
|
||
})
|
||
|
||
it('readonly empêche l\'ouverture du sélecteur de fichier', async () => {
|
||
const wrapper = mountComponent({label: 'Champ', readonly: true})
|
||
const fileInput = wrapper.get('input[type="file"]').element as HTMLInputElement
|
||
const clickSpy = vi.spyOn(fileInput, 'click')
|
||
await wrapper.get('input[type="text"]').trigger('click')
|
||
expect(clickSpy).not.toHaveBeenCalled()
|
||
})
|
||
|
||
it('réserve l’espace message par défaut même sans message', () => {
|
||
const wrapper = mountComponent({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 = mountComponent({label: 'Champ', reserveMessageSpace: false})
|
||
expect(wrapper.find('[id$="-describedby"]').exists()).toBe(false)
|
||
})
|
||
|
||
it('reserveMessageSpace=false avec message : ligne rendue sans min-h', () => {
|
||
const wrapper = mountComponent({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]')
|
||
})
|
||
})
|