mirror of
https://github.com/nuxt/ui.git
synced 2026-09-14 19:51:10 +08:00
f2d73e5bee
Co-authored-by: Jakub <jakub.michalek@freelo.io> Co-authored-by: Benjamin Canac <canacb1@gmail.com>
87 lines
3.9 KiB
TypeScript
87 lines
3.9 KiB
TypeScript
import { describe, it, expect, vi, afterAll, test } from 'vitest'
|
|
import { axe } from 'vitest-axe'
|
|
import { mountSuspended } from '@nuxt/test-utils/runtime'
|
|
import { CalendarDate } from '@internationalized/date'
|
|
import Calendar from '../../src/runtime/components/Calendar.vue'
|
|
import type { CalendarProps, CalendarSlots } from '../../src/runtime/components/Calendar.vue'
|
|
import ComponentRender from '../component-render'
|
|
import theme from '#build/ui/calendar'
|
|
|
|
describe('Calendar', () => {
|
|
const sizes = Object.keys(theme.variants.size) as any
|
|
const variants = Object.keys(theme.variants.variant) as any
|
|
const date = new Date('2025-01-01')
|
|
|
|
vi.setSystemTime(date)
|
|
|
|
afterAll(() => {
|
|
vi.useRealTimers()
|
|
})
|
|
|
|
it.each([
|
|
// Props
|
|
['with modelValue', { props: { modelValue: new CalendarDate(2025, 1, 1) } }],
|
|
['with default value', { props: { defaultValue: new CalendarDate(2025, 1, 1) } }],
|
|
['with range', { props: { range: true } }],
|
|
['with multiple', { props: { multiple: true } }],
|
|
['with disabled', { props: { disabled: true } }],
|
|
['with readonly', { props: { readonly: true } }],
|
|
['with isDateDisabled', { props: { isDateDisabled: () => true } }],
|
|
['with isDateUnavailable', { props: { isDateUnavailable: () => true } }],
|
|
['with weekStartsOn', { props: { weekStartsOn: 1 } }],
|
|
['with weekdayFormat', { props: { weekdayFormat: 'short' } }],
|
|
['with numberOfMonths', { props: { numberOfMonths: 2 } }],
|
|
['with nextYear', { props: { nextYear: { size: 'lg', color: 'primary' } } }],
|
|
['with nextMonth', { props: { nextMonth: { size: 'lg', color: 'primary' } } }],
|
|
['with prevYear', { props: { prevYear: { size: 'lg', color: 'primary' } } }],
|
|
['with prevMonth', { props: { prevMonth: { size: 'lg', color: 'primary' } } }],
|
|
['without fixedWeeks', { props: { fixedWeeks: false } }],
|
|
['without monthControls', { props: { monthControls: false } }],
|
|
['without yearControls', { props: { yearControls: false } }],
|
|
...sizes.map((size: string) => [`with size ${size}`, { props: { size } }]),
|
|
...variants.map((variant: string) => [`with variant ${variant}`, { props: { variant, defaultValue: new CalendarDate(2025, 1, 15) } }]),
|
|
['with color neutral', { props: { color: 'neutral' } }],
|
|
['with as', { props: { as: 'section' } }],
|
|
['with class', { props: { class: 'max-w-sm' } }],
|
|
['with ui', { props: { ui: { header: 'gap-4' } } }],
|
|
// Slots
|
|
['with heading slot', { slots: { heading: () => 'Heading' } }],
|
|
['with day slot', { slots: { day: ({ day }: Parameters<CalendarSlots['day']>[0]) => day.day } }],
|
|
['with week-day slot', { slots: { 'week-day': ({ day }: Parameters<CalendarSlots['week-day']>[0]) => day } }]
|
|
])('renders %s correctly', async (nameOrHtml: string, options: { props?: CalendarProps<false, false>, slots?: Partial<CalendarSlots> }) => {
|
|
const html = await ComponentRender(nameOrHtml, options, Calendar)
|
|
expect(html).toMatchSnapshot()
|
|
})
|
|
|
|
describe('emits', () => {
|
|
test('update:modelValue event single', async () => {
|
|
const wrapper = await mountSuspended(Calendar)
|
|
const date = new CalendarDate(2025, 1, 1)
|
|
|
|
await wrapper.setValue(date)
|
|
expect(wrapper.emitted()).toMatchObject({ 'update:modelValue': [[date]] })
|
|
})
|
|
|
|
test('update:modelValue event range', async () => {
|
|
const wrapper = await mountSuspended(Calendar, { props: { range: true } })
|
|
const date = [new CalendarDate(2025, 1, 1), new CalendarDate(2025, 1, 2)]
|
|
|
|
await wrapper.setValue(date)
|
|
expect(wrapper.emitted()).toMatchObject({ 'update:modelValue': [[date]] })
|
|
})
|
|
})
|
|
|
|
it('passes accessibility tests', async () => {
|
|
const wrapper = await mountSuspended(Calendar, {
|
|
props: {
|
|
modelValue: new CalendarDate(2025, 1, 1),
|
|
range: true,
|
|
multiple: true,
|
|
numberOfMonths: 2
|
|
}
|
|
})
|
|
|
|
expect(await axe(wrapper.element)).toHaveNoViolations()
|
|
})
|
|
})
|