mirror of
https://github.com/nuxt/ui.git
synced 2026-09-14 19:51:10 +08:00
d3b5f1d3f9
Co-authored-by: Benjamin Canac <canacb1@gmail.com>
67 lines
2.7 KiB
TypeScript
67 lines
2.7 KiB
TypeScript
import { describe, it, expect } from 'vitest'
|
|
import { axe } from 'vitest-axe'
|
|
import { mountSuspended } from '@nuxt/test-utils/runtime'
|
|
import { renderEach } from '../component-render'
|
|
import User from '../../src/runtime/components/User.vue'
|
|
import theme from '#build/ui/user'
|
|
|
|
describe('User', () => {
|
|
const sizes = Object.keys(theme.variants.size) as any
|
|
const orientations = Object.keys(theme.variants.orientation) as any
|
|
|
|
const props = {
|
|
name: 'Benjamin Canac',
|
|
description: 'Software Engineer',
|
|
avatar: { src: 'https://github.com/benjamincanac.png', alt: 'User avatar' }
|
|
}
|
|
|
|
renderEach(User, [
|
|
// Props
|
|
['with name', { props: { name: props.name } }],
|
|
['with description', { props: { name: props.name, description: props.description } }],
|
|
['with to', { props: { ...props, to: 'https://github.com/benjamincanac' } }],
|
|
['with avatar', { props }],
|
|
['with chip', { props: { ...props, chip: { color: 'success' } } }],
|
|
...sizes.map((size: string) => [`with size ${size}`, { props: { ...props, size } }]),
|
|
...orientations.map((orientation: string) => [`with orientation ${orientation}`, { props: { ...props, orientation } }]),
|
|
['with as', { props: { ...props, as: 'section' } }],
|
|
['with class', { props: { ...props, class: 'absolute' } }],
|
|
['with ui', { props: { ...props, ui: { name: 'font-bold' } } }],
|
|
// Slots
|
|
['with avatar slot', { props, slots: { avatar: () => 'Avatar slot' } }],
|
|
['with name slot', { props, slots: { name: () => 'Name slot' } }],
|
|
['with description slot', { props, slots: { description: () => 'Description slot' } }],
|
|
['with default slot', { props, slots: { default: () => 'Default slot' } }]
|
|
])
|
|
|
|
it('passes accessibility tests', async () => {
|
|
const wrapper = await mountSuspended(User, {
|
|
props: {
|
|
...props,
|
|
to: 'https://github.com/benjamincanac',
|
|
chip: { color: 'info' }
|
|
}
|
|
})
|
|
|
|
expect(await axe(wrapper.element)).toHaveNoViolations()
|
|
})
|
|
|
|
it('forwards attrs to root when `to` prop is absent', async () => {
|
|
const wrapper = await mountSuspended(User, {
|
|
props: { name: 'John Doe' },
|
|
attrs: { 'aria-label': 'test-label', 'data-testid': 'test-id' }
|
|
})
|
|
expect(wrapper.attributes('aria-label')).toBe('test-label')
|
|
expect(wrapper.attributes('data-testid')).toBe('test-id')
|
|
})
|
|
|
|
it('forwards attrs to link when `to` prop is set', async () => {
|
|
const wrapper = await mountSuspended(User, {
|
|
props: { name: 'John Doe', to: 'https://github.com/benjamincanac' },
|
|
attrs: { 'aria-label': 'test-label' }
|
|
})
|
|
expect(wrapper.attributes('aria-label')).toBeUndefined()
|
|
expect(wrapper.find('a').attributes('aria-label')).toBe('test-label')
|
|
})
|
|
})
|