176 lines
5.6 KiB
TypeScript
176 lines
5.6 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 InputUpload from './InputUpload.vue'
|
|
|
|
type InputUploadProps = {
|
|
id?: string
|
|
label?: string
|
|
modelValue?: string | null
|
|
inputClass?: string
|
|
labelClass?: string
|
|
groupClass?: string
|
|
disabled?: boolean
|
|
hint?: string
|
|
error?: string
|
|
success?: string
|
|
displayIcon?: boolean
|
|
accept?: string
|
|
}
|
|
|
|
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('passes accept attribute to file input', () => {
|
|
const wrapper = mountComponent({accept: '.pdf,.doc'})
|
|
|
|
expect(wrapper.get('input[type="file"]').attributes('accept')).toBe('.pdf,.doc')
|
|
})
|
|
})
|