diff --git a/frontend/src/components/input/FormInput.test.ts b/frontend/src/components/input/FormInput.test.ts new file mode 100644 index 000000000..4f23ec033 --- /dev/null +++ b/frontend/src/components/input/FormInput.test.ts @@ -0,0 +1,123 @@ +import {describe, it, expect, vi} from 'vitest' +import {mount} from '@vue/test-utils' +import FormInput from './FormInput.vue' + +describe('FormInput', () => { + it('renders a Bulma-classed input', () => { + const wrapper = mount(FormInput) + const input = wrapper.find('input') + expect(input.exists()).toBe(true) + expect(input.classes()).toContain('input') + }) + + it('supports v-model', async () => { + const wrapper = mount(FormInput, { + props: { + modelValue: 'hello', + 'onUpdate:modelValue': (val: string | number) => wrapper.setProps({modelValue: val}), + }, + }) + const input = wrapper.find('input') + expect(input.element.value).toBe('hello') + + await input.setValue('world') + expect(wrapper.props('modelValue')).toBe('world') + }) + + it('preserves numeric type in v-model when modelValue is a number', async () => { + const wrapper = mount(FormInput, { + props: { + modelValue: 42, + 'onUpdate:modelValue': (val: number | string) => wrapper.setProps({modelValue: val as number}), + }, + }) + await wrapper.find('input').setValue('7') + expect(wrapper.props('modelValue')).toBe(7) + }) + + it('coerces to number when the .number modifier is set even if modelValue starts null', async () => { + const wrapper = mount(FormInput, { + props: { + modelValue: null, + modelModifiers: {number: true}, + 'onUpdate:modelValue': (val: number | string) => wrapper.setProps({modelValue: val as number}), + }, + }) + await wrapper.find('input').setValue('42') + expect(wrapper.props('modelValue')).toBe(42) + expect(typeof wrapper.props('modelValue')).toBe('number') + }) + + it('applies is-loading class when loading', () => { + const wrapper = mount(FormInput, {props: {loading: true}}) + expect(wrapper.find('input').classes()).toContain('is-loading') + }) + + it('applies disabled class and attribute when disabled', () => { + const wrapper = mount(FormInput, {props: {disabled: true}}) + const input = wrapper.find('input') + expect(input.classes()).toContain('disabled') + expect(input.attributes('disabled')).toBe('') + }) + + it('uses an explicit id prop when given', () => { + const wrapper = mount(FormInput, {props: {id: 'my-id'}}) + expect(wrapper.find('input').attributes('id')).toBe('my-id') + }) + + it('generates a unique id when no id prop is given', () => { + const wrapper = mount({ + components: {FormInput}, + template: '
+ {{ error }} +
+