|
| 1 | +// @vitest-environment happy-dom |
| 2 | +import {mount} from '@vue/test-utils'; |
| 3 | +import {describe, expect, it} from 'vitest'; |
| 4 | + |
| 5 | +import DateInput from '../src/components/DateInput.vue'; |
| 6 | + |
| 7 | +describe('DateInput', () => { |
| 8 | + it('renders a date input reflecting the model and valid state', () => { |
| 9 | + const wrapper = mount(DateInput, {props: {id: 'dob', modelValue: '2026-07-17'}}); |
| 10 | + const input = wrapper.find('input'); |
| 11 | + |
| 12 | + expect(input.attributes('type')).toBe('date'); |
| 13 | + expect((input.element as HTMLInputElement).value).toBe('2026-07-17'); |
| 14 | + expect(input.classes()).toContain('ui-control'); |
| 15 | + expect(input.classes()).not.toContain('is-invalid'); |
| 16 | + expect(input.attributes('aria-invalid')).toBeUndefined(); |
| 17 | + expect(input.attributes('aria-required')).toBeUndefined(); |
| 18 | + }); |
| 19 | + |
| 20 | + it('renders empty for a null model', () => { |
| 21 | + const wrapper = mount(DateInput, {props: {id: 'dob', modelValue: null}}); |
| 22 | + expect((wrapper.find('input').element as HTMLInputElement).value).toBe(''); |
| 23 | + }); |
| 24 | + |
| 25 | + it('honours min/max/required/invalid/describedby', () => { |
| 26 | + const wrapper = mount(DateInput, { |
| 27 | + props: { |
| 28 | + id: 'dob', |
| 29 | + modelValue: '2026-07-17', |
| 30 | + min: '2026-01-01', |
| 31 | + max: '2026-12-31', |
| 32 | + required: true, |
| 33 | + invalid: true, |
| 34 | + describedby: 'dob-error', |
| 35 | + }, |
| 36 | + }); |
| 37 | + const input = wrapper.find('input'); |
| 38 | + |
| 39 | + expect(input.attributes('min')).toBe('2026-01-01'); |
| 40 | + expect(input.attributes('max')).toBe('2026-12-31'); |
| 41 | + expect(input.classes()).toContain('is-invalid'); |
| 42 | + expect(input.attributes('aria-invalid')).toBe('true'); |
| 43 | + expect(input.attributes('aria-required')).toBe('true'); |
| 44 | + expect(input.attributes('aria-describedby')).toBe('dob-error'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('emits the date string on input', async () => { |
| 48 | + const wrapper = mount(DateInput, {props: {id: 'dob', modelValue: null}}); |
| 49 | + await wrapper.find('input').setValue('2026-03-01'); |
| 50 | + expect(wrapper.emitted('update:modelValue')?.[0]).toEqual(['2026-03-01']); |
| 51 | + }); |
| 52 | + |
| 53 | + it("emits '' when the date is cleared (raw native value; middleware maps to null)", async () => { |
| 54 | + const wrapper = mount(DateInput, {props: {id: 'dob', modelValue: '2026-07-17'}}); |
| 55 | + await wrapper.find('input').setValue(''); |
| 56 | + expect(wrapper.emitted('update:modelValue')?.[0]).toEqual(['']); |
| 57 | + }); |
| 58 | + |
| 59 | + it('renders the disabled attribute when disabled', () => { |
| 60 | + const wrapper = mount(DateInput, {props: {id: 'dob', disabled: true, modelValue: null}}); |
| 61 | + expect(wrapper.find('input').attributes('disabled')).toBeDefined(); |
| 62 | + }); |
| 63 | +}); |
0 commit comments