import { mount } from '@vue/test-utils' import { describe, expect, it } from 'vitest' import { defineComponent, ref } from 'vue' import CodeInput from '@/components/CodeInput.vue' // Parent avec v-model : reproduit l'usage réel (la valeur ne remonte qu'au rendu du parent). function mountWithModel() { const completed: string[] = [] const Parent = defineComponent({ components: { CodeInput }, setup: () => ({ code: ref(''), completed }), template: '', }) const wrapper = mount(Parent, { attachTo: document.body }) return { wrapper, completed } } function paste(input: Element, text: string) { const event = new Event('paste', { bubbles: true, cancelable: true }) as ClipboardEvent Object.defineProperty(event, 'clipboardData', { value: { getData: () => text } }) input.dispatchEvent(event) } describe('CodeInput', () => { it('affiche six cases étiquetées', () => { const { wrapper } = mountWithModel() const digits = wrapper.findAll('input') expect(digits).toHaveLength(6) expect(digits[0]!.attributes('autocomplete')).toBe('one-time-code') expect(digits[5]!.attributes('aria-label')).toBe('Code – chiffre 6') wrapper.unmount() }) it('répartit un code collé et émet complete', async () => { const { wrapper, completed } = mountWithModel() paste(wrapper.find('input').element, '123 456') await wrapper.vm.$nextTick() expect(wrapper.findAll('input').map((i) => (i.element as HTMLInputElement).value)).toEqual([ '1', '2', '3', '4', '5', '6', ]) expect(completed).toEqual(['123456']) wrapper.unmount() }) it('émet complete à la saisie du dernier chiffre', async () => { const { wrapper, completed } = mountWithModel() const inputs = wrapper.findAll('input') for (const [index, digit] of [...'987654'].entries()) { await inputs[index]!.setValue(digit) } expect(completed).toEqual(['987654']) wrapper.unmount() }) it('ignore les caractères non numériques', async () => { const { wrapper, completed } = mountWithModel() await wrapper.find('input').setValue('a') expect((wrapper.find('input').element as HTMLInputElement).value).toBe('') expect(completed).toEqual([]) wrapper.unmount() }) })