| Numéro du ticket | Titre du ticket | |------------------|-----------------| | | | ## Description de la PR ## Modification du .env ## Check list - [ ] Pas de régression - [ ] TU/TI/TF rédigée - [ ] TU/TI/TF OK - [ ] CHANGELOG modifié Reviewed-on: #45 Co-authored-by: tristan <tristan@yuno.malio.fr> Co-committed-by: tristan <tristan@yuno.malio.fr>
309 lines
9.4 KiB
TypeScript
309 lines
9.4 KiB
TypeScript
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 InputPhone from './InputPhone.vue'
|
|
|
|
type InputPhoneProps = {
|
|
id?: string
|
|
label?: string
|
|
name?: string
|
|
autocomplete?: string
|
|
modelValue?: string | null
|
|
inputClass?: string
|
|
labelClass?: string
|
|
groupClass?: string
|
|
required?: boolean
|
|
disabled?: boolean
|
|
readonly?: boolean
|
|
hint?: string
|
|
error?: string
|
|
success?: string
|
|
iconName?: string
|
|
iconPosition?: 'left' | 'right'
|
|
iconSize?: string | number
|
|
iconColor?: string
|
|
mask?: string
|
|
addable?: boolean
|
|
addIconName?: string
|
|
addButtonLabel?: string
|
|
}
|
|
|
|
const InputPhoneForTest = InputPhone as DefineComponent<InputPhoneProps>
|
|
|
|
const mountComponent = (props: InputPhoneProps = {}) =>
|
|
mount(InputPhoneForTest, {
|
|
props,
|
|
global: {
|
|
stubs: {
|
|
IconifyIcon: {
|
|
template: '<span data-test="icon" v-bind="$attrs" />',
|
|
},
|
|
},
|
|
},
|
|
})
|
|
|
|
describe('MalioInputPhone', () => {
|
|
it('renders the initial input value', () => {
|
|
const wrapper = mountComponent({modelValue: '+33 6 12 34 56 78'})
|
|
|
|
expect(wrapper.get('input').element.value).toBe('+33 6 12 34 56 78')
|
|
})
|
|
|
|
it('renders the label text', () => {
|
|
const wrapper = mountComponent({label: 'Téléphone'})
|
|
|
|
expect(wrapper.get('label').text()).toBe('Téléphone')
|
|
})
|
|
|
|
it('has type tel', () => {
|
|
const wrapper = mountComponent()
|
|
|
|
expect(wrapper.get('input').attributes('type')).toBe('tel')
|
|
})
|
|
|
|
it('has inputmode tel', () => {
|
|
const wrapper = mountComponent()
|
|
|
|
expect(wrapper.get('input').attributes('inputmode')).toBe('tel')
|
|
})
|
|
|
|
it('renders the default phone icon', () => {
|
|
const wrapper = mountComponent()
|
|
|
|
const iconComponent = wrapper.findComponent(IconifyIcon)
|
|
expect(iconComponent.props('icon')).toBe('mdi:phone-outline')
|
|
})
|
|
|
|
it('allows overriding the icon', () => {
|
|
const wrapper = mountComponent({iconName: 'mdi:cellphone'})
|
|
|
|
const iconComponent = wrapper.findComponent(IconifyIcon)
|
|
expect(iconComponent.props('icon')).toBe('mdi:cellphone')
|
|
})
|
|
|
|
it('does not render icon when iconName is empty', () => {
|
|
const wrapper = mountComponent({iconName: ''})
|
|
|
|
expect(wrapper.find('[data-test="icon"]').exists()).toBe(false)
|
|
})
|
|
|
|
it('places icon on the left by default', () => {
|
|
const wrapper = mountComponent()
|
|
|
|
expect(wrapper.get('[data-test="icon"]').classes()).toContain('left-[10px]')
|
|
})
|
|
|
|
it('places icon on the right when iconPosition is right', () => {
|
|
const wrapper = mountComponent({iconPosition: 'right'})
|
|
|
|
expect(wrapper.get('[data-test="icon"]').classes()).toContain('right-[10px]')
|
|
})
|
|
|
|
it('emits update:modelValue on input change', async () => {
|
|
const wrapper = mountComponent({modelValue: ''})
|
|
|
|
await wrapper.get('input').setValue('+33612345678')
|
|
|
|
expect(wrapper.emitted('update:modelValue')?.[0]).toEqual(['+33612345678'])
|
|
})
|
|
|
|
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: 'Numéro invalide'})
|
|
|
|
expect(wrapper.get('p.text-m-danger').text()).toBe('Numéro invalide')
|
|
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: 'Numéro valide'})
|
|
|
|
expect(wrapper.get('p.text-m-success').text()).toBe('Numéro 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('shows default icon color when empty and unfocused', () => {
|
|
const wrapper = mountComponent()
|
|
|
|
expect(wrapper.get('[data-test="icon"]').classes()).toContain('text-m-muted')
|
|
})
|
|
|
|
it('shows primary icon color on focus', async () => {
|
|
const wrapper = mountComponent()
|
|
|
|
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 = mountComponent({modelValue: '+33612345678'})
|
|
|
|
expect(wrapper.get('[data-test="icon"]').classes()).toContain('text-black')
|
|
})
|
|
|
|
it('keeps default icon color when disabled, even if filled', () => {
|
|
const wrapper = mountComponent({modelValue: '+33612345678', disabled: true})
|
|
|
|
expect(wrapper.get('[data-test="icon"]').classes()).toContain('text-m-muted')
|
|
})
|
|
|
|
it('error overrides focus color on icon', async () => {
|
|
const wrapper = mountComponent({error: 'Numéro invalide'})
|
|
|
|
await wrapper.get('input').trigger('focus')
|
|
|
|
expect(wrapper.get('[data-test="icon"]').classes()).toContain('text-m-danger')
|
|
})
|
|
|
|
it('shows hint message', () => {
|
|
const wrapper = mountComponent({hint: 'Format international recommandé'})
|
|
|
|
expect(wrapper.get('p.text-m-muted').text()).toBe('Format international recommandé')
|
|
})
|
|
|
|
it('links label to input via for/id', () => {
|
|
const wrapper = mountComponent({id: 'phone-field', label: 'Téléphone'})
|
|
|
|
expect(wrapper.get('input').attributes('id')).toBe('phone-field')
|
|
expect(wrapper.get('label').attributes('for')).toBe('phone-field')
|
|
})
|
|
|
|
it('generates an id when missing and reuses it on label', () => {
|
|
const wrapper = mountComponent({label: 'Téléphone'})
|
|
|
|
const inputId = wrapper.get('input').attributes('id')
|
|
|
|
expect(inputId?.startsWith('malio-input-phone-')).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')
|
|
})
|
|
|
|
it('uses autocomplete off by default', () => {
|
|
const wrapper = mountComponent()
|
|
|
|
expect(wrapper.get('input').attributes('autocomplete')).toBe('off')
|
|
})
|
|
|
|
it('allows overriding autocomplete', () => {
|
|
const wrapper = mountComponent({autocomplete: 'tel'})
|
|
|
|
expect(wrapper.get('input').attributes('autocomplete')).toBe('tel')
|
|
})
|
|
|
|
it('does not render add button by default', () => {
|
|
const wrapper = mountComponent()
|
|
|
|
expect(wrapper.find('[data-test="add-button"]').exists()).toBe(false)
|
|
})
|
|
|
|
it('renders add button when addable is true', () => {
|
|
const wrapper = mountComponent({addable: true})
|
|
|
|
expect(wrapper.find('[data-test="add-button"]').exists()).toBe(true)
|
|
})
|
|
|
|
it('emits add event when add button is clicked', async () => {
|
|
const wrapper = mountComponent({addable: true})
|
|
|
|
await wrapper.get('[data-test="add-button"]').trigger('click')
|
|
|
|
expect(wrapper.emitted('add')).toHaveLength(1)
|
|
})
|
|
|
|
it('does not emit add when disabled', async () => {
|
|
const wrapper = mountComponent({addable: true, disabled: true})
|
|
|
|
await wrapper.get('[data-test="add-button"]').trigger('click')
|
|
|
|
expect(wrapper.emitted('add')).toBeUndefined()
|
|
})
|
|
|
|
it('does not emit add when readonly', async () => {
|
|
const wrapper = mountComponent({addable: true, readonly: true})
|
|
|
|
await wrapper.get('[data-test="add-button"]').trigger('click')
|
|
|
|
expect(wrapper.emitted('add')).toBeUndefined()
|
|
})
|
|
|
|
it('disables add button when disabled', () => {
|
|
const wrapper = mountComponent({addable: true, disabled: true})
|
|
|
|
expect(wrapper.get('[data-test="add-button"]').attributes('disabled')).toBeDefined()
|
|
})
|
|
|
|
it('disables add button when readonly', () => {
|
|
const wrapper = mountComponent({addable: true, readonly: true})
|
|
|
|
expect(wrapper.get('[data-test="add-button"]').attributes('disabled')).toBeDefined()
|
|
})
|
|
|
|
it('renders the default add icon (mdi:plus)', () => {
|
|
const wrapper = mountComponent({addable: true})
|
|
|
|
const icons = wrapper.findAllComponents(IconifyIcon)
|
|
const addIcon = icons[icons.length - 1]
|
|
expect(addIcon.props('icon')).toBe('mdi:plus')
|
|
})
|
|
|
|
it('allows overriding the add icon', () => {
|
|
const wrapper = mountComponent({addable: true, addIconName: 'mdi:phone-plus'})
|
|
|
|
const icons = wrapper.findAllComponents(IconifyIcon)
|
|
const addIcon = icons[icons.length - 1]
|
|
expect(addIcon.props('icon')).toBe('mdi:phone-plus')
|
|
})
|
|
|
|
it('exposes aria-label on add button', () => {
|
|
const wrapper = mountComponent({addable: true, addButtonLabel: 'Ajouter un autre numéro'})
|
|
|
|
expect(wrapper.get('[data-test="add-button"]').attributes('aria-label')).toBe('Ajouter un autre numéro')
|
|
})
|
|
|
|
it('adds right padding to input when addable', () => {
|
|
const wrapper = mountComponent({addable: true})
|
|
|
|
expect(wrapper.get('input').classes()).toContain('!pr-10')
|
|
})
|
|
|
|
it('applies mask via maska directive', async () => {
|
|
const wrapper = mountComponent({mask: '+## # ## ## ## ##'})
|
|
|
|
await wrapper.get('input').setValue('33612345678')
|
|
|
|
expect(wrapper.emitted('update:modelValue')).toBeDefined()
|
|
})
|
|
})
|