import {describe, expect, it} from 'vitest' import {mount} from '@vue/test-utils' import type {DefineComponent} from 'vue' import { Icon as IconifyIcon } from '@iconify/vue' import InputPassword from './InputPassword.vue' type InputPasswordProps = { 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 displayIcon?: boolean } const InputPasswordForTest = InputPassword as DefineComponent const mountComponent = (props: InputPasswordProps = {}) => mount(InputPasswordForTest, { props, global: { stubs: { IconifyIcon: { template: '', }, }, }, }) describe('MalioInputPassword', () => { it('renders the initial input value', () => { const wrapper = mountComponent({modelValue: 'secret123'}) expect(wrapper.get('input').element.value).toBe('secret123') }) it('renders the label text', () => { const wrapper = mountComponent({label: 'Mot de passe'}) expect(wrapper.get('label').text()).toBe('Mot de passe') }) it('has type password by default', () => { const wrapper = mountComponent() expect(wrapper.get('input').attributes('type')).toBe('password') }) it('toggles to type text when icon is clicked', async () => { const wrapper = mountComponent() await wrapper.get('[data-test="icon"]').trigger('click') expect(wrapper.get('input').attributes('type')).toBe('text') }) it('toggles back to password on second click', async () => { const wrapper = mountComponent() await wrapper.get('[data-test="icon"]').trigger('click') await wrapper.get('[data-test="icon"]').trigger('click') expect(wrapper.get('input').attributes('type')).toBe('password') }) it('does not render icon when displayIcon is false', () => { const wrapper = mountComponent({displayIcon: false}) expect(wrapper.find('[data-test="icon"]').exists()).toBe(false) }) it('renders icon by default', () => { const wrapper = mountComponent() expect(wrapper.find('[data-test="icon"]').exists()).toBe(true) }) it('shows eye-off-outline icon when password is hidden', () => { const wrapper = mountComponent() const iconComponent = wrapper.findComponent(IconifyIcon) expect(iconComponent.props('icon')).toBe('mdi:eye-off-outline') }) it('shows eye-outline icon when password is visible', async () => { const wrapper = mountComponent() await wrapper.get('[data-test="icon"]').trigger('click') const iconComponent = wrapper.findComponent(IconifyIcon) expect(iconComponent.props('icon')).toBe('mdi:eye-outline') }) it('emits update:modelValue on input change', async () => { const wrapper = mountComponent({modelValue: ''}) await wrapper.get('input').setValue('new password') expect(wrapper.emitted('update:modelValue')?.[0]).toEqual(['new password']) }) it('sets disabled styles when true', () => { const wrapper = mountComponent({disabled: true}) expect(wrapper.get('input').attributes('disabled')).toBeDefined() expect(wrapper.get('input').classes()).toContain('cursor-not-allowed') }) it('sets readonly when true', () => { const wrapper = mountComponent({readonly: true}) expect(wrapper.get('input').attributes('readonly')).toBeDefined() }) it('shows error message and styles', () => { const wrapper = mountComponent({error: 'Mot de passe requis'}) expect(wrapper.get('p.text-m-danger').text()).toBe('Mot de passe requis') expect(wrapper.get('input').classes()).toContain('border-m-danger') expect(wrapper.get('input').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: 'Mot de passe valide'}) expect(wrapper.get('p.text-m-success').text()).toBe('Mot de passe valide') expect(wrapper.get('input').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('links label to input via for/id', () => { const wrapper = mountComponent({id: 'pwd', label: 'Password'}) expect(wrapper.get('input').attributes('id')).toBe('pwd') expect(wrapper.get('label').attributes('for')).toBe('pwd') }) it('generates an id when missing and reuses it on label', () => { const wrapper = mountComponent({label: 'Password'}) const inputId = wrapper.get('input').attributes('id') expect(inputId?.startsWith('malio-input-password-')).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').attributes('aria-invalid')).toBe('false') }) })