feat : reorganisation de la structure projet
This commit is contained in:
151
app/components/malio/button/ButtonIcon.test.ts
Normal file
151
app/components/malio/button/ButtonIcon.test.ts
Normal file
@@ -0,0 +1,151 @@
|
||||
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 ButtonIcon from './ButtonIcon.vue'
|
||||
|
||||
type ButtonIconProps = {
|
||||
id?: string
|
||||
icon: string
|
||||
ariaLabel: string
|
||||
disabled?: boolean
|
||||
buttonClass?: string
|
||||
iconSize?: string | number
|
||||
variant?: 'filled' | 'ghost'
|
||||
}
|
||||
|
||||
const ButtonIconForTest = ButtonIcon as DefineComponent<ButtonIconProps>
|
||||
|
||||
const mountComponent = (props: ButtonIconProps = {icon: 'mdi:arrow-left', ariaLabel: 'Retour'}) =>
|
||||
mount(ButtonIconForTest, {
|
||||
props,
|
||||
global: {
|
||||
stubs: {
|
||||
IconifyIcon: {
|
||||
template: '<span data-test="icon" v-bind="$attrs" />',
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
describe('MalioButtonIcon', () => {
|
||||
it('renders a button with the icon', () => {
|
||||
const wrapper = mountComponent()
|
||||
|
||||
expect(wrapper.find('button').exists()).toBe(true)
|
||||
expect(wrapper.find('[data-test="icon"]').exists()).toBe(true)
|
||||
})
|
||||
|
||||
it('uses provided id on button', () => {
|
||||
const wrapper = mountComponent({id: 'custom-id', icon: 'mdi:arrow-left', ariaLabel: 'Retour'})
|
||||
|
||||
expect(wrapper.get('button').attributes('id')).toBe('custom-id')
|
||||
})
|
||||
|
||||
it('generates an id when missing', () => {
|
||||
const wrapper = mountComponent()
|
||||
|
||||
const buttonId = wrapper.get('button').attributes('id')
|
||||
expect(buttonId?.startsWith('malio-button-icon-')).toBe(true)
|
||||
})
|
||||
|
||||
it('sets aria-label on button', () => {
|
||||
const wrapper = mountComponent({icon: 'mdi:arrow-left', ariaLabel: 'Retour'})
|
||||
|
||||
expect(wrapper.get('button').attributes('aria-label')).toBe('Retour')
|
||||
})
|
||||
|
||||
it('sets type="button" on the button', () => {
|
||||
const wrapper = mountComponent()
|
||||
|
||||
expect(wrapper.get('button').attributes('type')).toBe('button')
|
||||
})
|
||||
|
||||
it('passes icon name to icon component', () => {
|
||||
const wrapper = mount(ButtonIconForTest, {
|
||||
props: {icon: 'mdi:pencil-outline', ariaLabel: 'Modifier'},
|
||||
})
|
||||
|
||||
const iconComponent = wrapper.findComponent(IconifyIcon)
|
||||
expect(iconComponent.props('icon')).toBe('mdi:pencil-outline')
|
||||
})
|
||||
|
||||
it('passes icon size to icon component', () => {
|
||||
const wrapper = mount(ButtonIconForTest, {
|
||||
props: {icon: 'mdi:arrow-left', ariaLabel: 'Retour', iconSize: 32},
|
||||
})
|
||||
|
||||
const iconComponent = wrapper.findComponent(IconifyIcon)
|
||||
expect(iconComponent.props('width')).toBe(32)
|
||||
expect(iconComponent.props('height')).toBe(32)
|
||||
})
|
||||
|
||||
it('emits click event when clicked', async () => {
|
||||
const wrapper = mountComponent()
|
||||
|
||||
await wrapper.get('button').trigger('click')
|
||||
|
||||
expect(wrapper.emitted('click')).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('does not emit click when disabled', async () => {
|
||||
const wrapper = mountComponent({icon: 'mdi:arrow-left', ariaLabel: 'Retour', disabled: true})
|
||||
|
||||
await wrapper.get('button').trigger('click')
|
||||
|
||||
expect(wrapper.emitted('click')).toBeUndefined()
|
||||
})
|
||||
|
||||
it('sets disabled attribute when disabled', () => {
|
||||
const wrapper = mountComponent({icon: 'mdi:arrow-left', ariaLabel: 'Retour', disabled: true})
|
||||
|
||||
expect(wrapper.get('button').attributes('disabled')).toBeDefined()
|
||||
})
|
||||
|
||||
it('applies disabled styles when disabled', () => {
|
||||
const wrapper = mountComponent({icon: 'mdi:arrow-left', ariaLabel: 'Retour', disabled: true})
|
||||
|
||||
expect(wrapper.get('button').classes()).toContain('cursor-not-allowed')
|
||||
expect(wrapper.get('button').classes()).toContain('bg-m-btn-disabled')
|
||||
})
|
||||
|
||||
it('applies cursor-pointer when not disabled', () => {
|
||||
const wrapper = mountComponent()
|
||||
|
||||
expect(wrapper.get('button').classes()).toContain('cursor-pointer')
|
||||
})
|
||||
|
||||
it('applies white text color for icon visibility', () => {
|
||||
const wrapper = mountComponent()
|
||||
|
||||
expect(wrapper.get('button').classes()).toContain('text-white')
|
||||
})
|
||||
|
||||
it('applies default background color', () => {
|
||||
const wrapper = mountComponent()
|
||||
|
||||
expect(wrapper.get('button').classes()).toContain('bg-m-btn-default')
|
||||
})
|
||||
|
||||
it('applies buttonClass', () => {
|
||||
const wrapper = mountComponent({icon: 'mdi:arrow-left', ariaLabel: 'Retour', buttonClass: 'rounded-full'})
|
||||
|
||||
expect(wrapper.get('button').classes()).toContain('rounded-full')
|
||||
})
|
||||
|
||||
it('applies ghost variant with no background and colored icon', () => {
|
||||
const wrapper = mountComponent({icon: 'mdi:arrow-left', ariaLabel: 'Retour', variant: 'ghost'})
|
||||
|
||||
expect(wrapper.get('button').classes()).toContain('text-m-btn-default')
|
||||
expect(wrapper.get('button').classes()).not.toContain('bg-m-btn-default')
|
||||
expect(wrapper.get('button').classes()).not.toContain('text-white')
|
||||
})
|
||||
|
||||
it('applies ghost disabled styles with no background', () => {
|
||||
const wrapper = mountComponent({icon: 'mdi:arrow-left', ariaLabel: 'Retour', variant: 'ghost', disabled: true})
|
||||
|
||||
expect(wrapper.get('button').classes()).toContain('text-m-btn-disabled')
|
||||
expect(wrapper.get('button').classes()).toContain('cursor-not-allowed')
|
||||
expect(wrapper.get('button').classes()).not.toContain('bg-m-btn-disabled')
|
||||
})
|
||||
})
|
||||
76
app/components/malio/button/ButtonIcon.vue
Normal file
76
app/components/malio/button/ButtonIcon.vue
Normal file
@@ -0,0 +1,76 @@
|
||||
<template>
|
||||
<button
|
||||
:id="buttonId"
|
||||
:class="mergedButtonClass"
|
||||
:disabled="disabled"
|
||||
:aria-label="ariaLabel"
|
||||
type="button"
|
||||
v-bind="attrs"
|
||||
@click="onClick"
|
||||
>
|
||||
<IconifyIcon
|
||||
:icon="icon"
|
||||
:width="iconSize"
|
||||
:height="iconSize"
|
||||
data-test="icon"
|
||||
/>
|
||||
</button>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import {computed, useAttrs, useId} from 'vue'
|
||||
import {Icon as IconifyIcon} from '@iconify/vue'
|
||||
import {twMerge} from 'tailwind-merge'
|
||||
|
||||
defineOptions({name: 'MalioButtonIcon', inheritAttrs: false})
|
||||
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
id?: string
|
||||
icon: string
|
||||
ariaLabel: string
|
||||
disabled?: boolean
|
||||
buttonClass?: string
|
||||
iconSize?: string | number
|
||||
variant?: 'filled' | 'ghost'
|
||||
}>(),
|
||||
{
|
||||
id: '',
|
||||
disabled: false,
|
||||
buttonClass: '',
|
||||
iconSize: 24,
|
||||
variant: 'filled',
|
||||
},
|
||||
)
|
||||
|
||||
const attrs = useAttrs()
|
||||
const generatedId = useId()
|
||||
|
||||
const buttonId = computed(() => props.id || `malio-button-icon-${generatedId}`)
|
||||
|
||||
const isFilled = computed(() => props.variant === 'filled')
|
||||
|
||||
const mergedButtonClass = computed(() =>
|
||||
twMerge(
|
||||
'inline-flex items-center justify-center rounded-md p-1 transition-colors duration-150 focus:outline-none focus-visible:ring-2 focus-visible:ring-m-primary/50',
|
||||
isFilled.value
|
||||
? props.disabled
|
||||
? 'bg-m-btn-disabled text-white cursor-not-allowed'
|
||||
: 'bg-m-btn-default hover:bg-m-btn-hover active:bg-m-btn-active text-white cursor-pointer'
|
||||
: props.disabled
|
||||
? 'text-m-btn-disabled cursor-not-allowed'
|
||||
: 'text-m-btn-default hover:text-m-btn-hover active:text-m-btn-active cursor-pointer',
|
||||
props.buttonClass,
|
||||
),
|
||||
)
|
||||
|
||||
const emit = defineEmits<{
|
||||
(event: 'click', e: MouseEvent): void
|
||||
}>()
|
||||
|
||||
function onClick(e: MouseEvent) {
|
||||
if (!props.disabled) {
|
||||
emit('click', e)
|
||||
}
|
||||
}
|
||||
</script>
|
||||
Reference in New Issue
Block a user