|
| 1 | +// @vitest-environment happy-dom |
| 2 | +import {mount} from '@vue/test-utils'; |
| 3 | +import {describe, expect, it} from 'vitest'; |
| 4 | + |
| 5 | +import NumberInput from '../src/components/NumberInput.vue'; |
| 6 | + |
| 7 | +describe('NumberInput', () => { |
| 8 | + it('renders a number input reflecting the model and valid state', () => { |
| 9 | + const wrapper = mount(NumberInput, {props: {id: 'qty', modelValue: 5}}); |
| 10 | + const input = wrapper.find('input'); |
| 11 | + |
| 12 | + expect(input.attributes('type')).toBe('number'); |
| 13 | + expect((input.element as HTMLInputElement).value).toBe('5'); |
| 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(NumberInput, {props: {id: 'qty', modelValue: null}}); |
| 22 | + expect((wrapper.find('input').element as HTMLInputElement).value).toBe(''); |
| 23 | + }); |
| 24 | + |
| 25 | + it('honours placeholder/min/max/step/required/invalid/describedby', () => { |
| 26 | + const wrapper = mount(NumberInput, { |
| 27 | + props: { |
| 28 | + id: 'qty', |
| 29 | + modelValue: 3, |
| 30 | + placeholder: 'How many', |
| 31 | + min: 0, |
| 32 | + max: 10, |
| 33 | + step: 2, |
| 34 | + required: true, |
| 35 | + invalid: true, |
| 36 | + describedby: 'qty-error', |
| 37 | + }, |
| 38 | + }); |
| 39 | + const input = wrapper.find('input'); |
| 40 | + |
| 41 | + expect(input.attributes('placeholder')).toBe('How many'); |
| 42 | + expect(input.attributes('min')).toBe('0'); |
| 43 | + expect(input.attributes('max')).toBe('10'); |
| 44 | + expect(input.attributes('step')).toBe('2'); |
| 45 | + expect(input.classes()).toContain('is-invalid'); |
| 46 | + expect(input.attributes('aria-invalid')).toBe('true'); |
| 47 | + expect(input.attributes('aria-required')).toBe('true'); |
| 48 | + expect(input.attributes('aria-describedby')).toBe('qty-error'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('emits the parsed number on input', async () => { |
| 52 | + const wrapper = mount(NumberInput, {props: {id: 'qty', modelValue: null}}); |
| 53 | + await wrapper.find('input').setValue('42'); |
| 54 | + expect(wrapper.emitted('update:modelValue')?.[0]).toEqual([42]); |
| 55 | + }); |
| 56 | + |
| 57 | + it('emits null when the input is cleared (NaN guard, owned once)', async () => { |
| 58 | + const wrapper = mount(NumberInput, {props: {id: 'qty', modelValue: 7}}); |
| 59 | + await wrapper.find('input').setValue(''); |
| 60 | + expect(wrapper.emitted('update:modelValue')?.[0]).toEqual([null]); |
| 61 | + }); |
| 62 | + |
| 63 | + it('renders the disabled attribute when disabled', () => { |
| 64 | + const wrapper = mount(NumberInput, {props: {id: 'qty', disabled: true, modelValue: null}}); |
| 65 | + expect(wrapper.find('input').attributes('disabled')).toBeDefined(); |
| 66 | + }); |
| 67 | +}); |
0 commit comments