From 1925a060bc47e28efadbb319cdf428deecb1d33a Mon Sep 17 00:00:00 2001 From: Paul Valerie GOMA Date: Tue, 22 Jul 2025 15:26:29 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20test:=20added=20test=20file?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/VInput.spec.ts | 190 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 190 insertions(+) create mode 100644 test/VInput.spec.ts diff --git a/test/VInput.spec.ts b/test/VInput.spec.ts new file mode 100644 index 0000000..5b039d7 --- /dev/null +++ b/test/VInput.spec.ts @@ -0,0 +1,190 @@ +import { describe, it, expect } from 'vitest' +import { mount } from '@vue/test-utils' +import VInput from '../src/components/input/VInput.vue' + +const globalConfig = { + global: { + mocks: { + $primevue: { + config: {} + } + }, + stubs: { + VHint: true, + VDivider: true, + Password: true, + Textarea: true, + VLabel: true, + } + } +} + +const noStubVLabelConfig = { + ...globalConfig.global, + stubs: { + ...globalConfig.global.stubs, + VLabel: false, + } +} + +const noStubTextareaConfig = { + ...globalConfig.global, + stubs: { + ...globalConfig.global.stubs, + Textarea: false, + } +} + +const noStubPasswordConfig = { + ...globalConfig.global, + stubs: { + ...globalConfig.global.stubs, + Textarea: false, + } +} + +describe('VInput.vue', () => { + it('renders InputText by default', () => { + const wrapper = mount(VInput, globalConfig) + expect(wrapper.find('input').exists()).toBe(true) + }) + + it('renders VLabel when labelVisible is true and passes the label prop', () => { + const wrapper = mount(VInput, { + props: { + labelVisible: true, + label: 'Username' + }, + global: noStubVLabelConfig + }) + + const labelComponent = wrapper.findComponent({ name: 'VLabel' }) + expect(labelComponent.exists()).toBe(true) + expect(labelComponent.props('label')).toBe('Username') + expect(wrapper.text()).toContain('Username') + }) + + it('does not render VLabel when labelVisible is false', () => { + const wrapper = mount(VInput, { + props: { + labelVisible: false, + label: 'Username' + }, + global: noStubVLabelConfig + }) + + const labelComponent = wrapper.findComponent({ name: 'VLabel' }) + expect(labelComponent.exists()).toBe(false) + }) + + // Test : update:modelValue from InputText + it('emits update:modelValue when input value changes', async () => { + const wrapper = mount(VInput, { + props: { + modelValue: '' + }, + global: globalConfig.global + }) + + const input = wrapper.find('input') + await input.setValue('new value') + + expect(wrapper.emitted('update:modelValue')).toBeTruthy() + expect(wrapper.emitted('update:modelValue')![0]).toEqual(['new value']) + }) + + // Test : value-change from InputText + it('emits value-change when InputText emits valueChange', async () => { + const wrapper = mount(VInput, { + props: { + modelValue: '' + }, + global: globalConfig.global + }) + + const inputText = wrapper.findComponent({ name: 'InputText' }) + inputText.vm.$emit('valueChange', 'changed') + + await wrapper.vm.$nextTick() + + expect(wrapper.emitted('value-change')).toBeTruthy() + expect(wrapper.emitted('value-change')![0]).toEqual(['changed']) + }) + + // Test : value-change from Textarea + it('emits value-change when Textarea emits valueChange', async () => { + const wrapper = mount(VInput, { + props: { + isTextarea: true, + modelValue: '' + }, + global: noStubTextareaConfig + }) + + const textarea = wrapper.findComponent({ name: 'Textarea' }) + textarea.vm.$emit('valueChange', 'textarea changed') + + await wrapper.vm.$nextTick() + + expect(wrapper.emitted('value-change')).toBeTruthy() + expect(wrapper.emitted('value-change')![0]).toEqual(['textarea changed']) + }) + + // Test : value-change from Password + it('emits value-change when Password emits valueChange', async () => { + const wrapper = mount(VInput, { + props: { + isPassword: true, + modelValue: '' + }, + global: noStubPasswordConfig + }) + + const password = wrapper.findComponent({ name: 'Password' }) + password.vm.$emit('valueChange', 'password changed') + + await wrapper.vm.$nextTick() + + expect(wrapper.emitted('value-change')).toBeTruthy() + expect(wrapper.emitted('value-change')![0]).toEqual(['password changed']) + }) + + // Test : update:modelValue from Password + it('emits update:modelValue when Password value changes', async () => { + const wrapper = mount(VInput, { + props: { + isPassword: true, + modelValue: '' + }, + global: noStubPasswordConfig + }) + + const password = wrapper.findComponent({ name: 'Password' }) + password.vm.$emit('update:modelValue', 'new-password') + + await wrapper.vm.$nextTick() + + expect(wrapper.emitted('update:modelValue')).toBeTruthy() + expect(wrapper.emitted('update:modelValue')![0]).toEqual(['new-password']) + }) + + // Test : change from Password + it('emits change when Password emits change', async () => { + const wrapper = mount(VInput, { + props: { + isPassword: true, + modelValue: '' + }, + global: noStubPasswordConfig + }) + + const password = wrapper.findComponent({ name: 'Password' }) + password.vm.$emit('change', 'password-changed') + + await wrapper.vm.$nextTick() + + expect(wrapper.emitted('change')).toBeTruthy() + expect(wrapper.emitted('change')![0]).toEqual(['password-changed']) + }) + +})