From 2dddbc729d9d30fc6477a087169580b31bb5f570 Mon Sep 17 00:00:00 2001 From: Paul Valerie GOMA Date: Wed, 23 Jul 2025 17:54:25 +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/VSelect.spec.ts | 101 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 test/VSelect.spec.ts diff --git a/test/VSelect.spec.ts b/test/VSelect.spec.ts new file mode 100644 index 0000000..3e0c8ca --- /dev/null +++ b/test/VSelect.spec.ts @@ -0,0 +1,101 @@ +import { describe, it, expect, vi } from 'vitest'; +import { mount } from '@vue/test-utils'; +import VSelect from '../src/components/select/VSelect.vue'; +import Select from 'primevue/select'; + +// Mock global matchMedia +Object.defineProperty(window, 'matchMedia', { + writable: true, + value: vi.fn().mockImplementation((query) => ({ + matches: false, + media: query, + onchange: null, + addListener: vi.fn(), // deprecated + removeListener: vi.fn(), // deprecated + addEventListener: vi.fn(), + removeEventListener: vi.fn(), + dispatchEvent: vi.fn(), + })), +}); + + +const globalConfig = { + global: { + components: { + Checkbox: Select + }, + mocks: { + $primevue: { + config: {} + } + }, + stubs: { + VLabel: false, + VHint: true, + } + } +} + +describe('VSelect.vue', () => { + const options = [ + { value: 'apple', text: 'Apple' }, + { value: 'banana', text: 'Banana' }, + { value: 'cherry', text: 'Cherry' } + ]; + + it('renders with basic props', () => { + const wrapper = mount(VSelect, { + props: { + label: 'Fruits', + options, + modelValue: null + }, + global: globalConfig.global + }); + + const label = wrapper.findComponent({ name: 'VLabel' }); + expect(label.exists()).toBe(true); + expect(label.props('label')).toBe('Fruits'); + + }); + + it('emits update:modelValue and change when value is selected', async () => { + const wrapper = mount(VSelect, { + props: { + options, + modelValue: null + }, + global: globalConfig.global + }); + + // Simule la sélection d'une option + wrapper.vm.$emit('update:modelValue', 'banana'); + wrapper.vm.$emit('change', { value: 'banana' }); + + const emittedUpdate = wrapper.emitted('update:modelValue'); + const emittedChange = wrapper.emitted('change'); + + expect(emittedUpdate).toBeTruthy(); + expect(emittedUpdate![0]).toEqual(['banana']); + + expect(emittedChange).toBeTruthy(); + expect(emittedChange![0]).toEqual([{ value: 'banana' }]); + }); + + it('reacts to external modelValue changes', async () => { + const wrapper = mount(VSelect, { + props: { + options, + modelValue: 'apple' + }, + global: globalConfig.global + }); + + await wrapper.setProps({ modelValue: 'cherry' }); + expect(wrapper.emitted('update:modelValue')).toBeFalsy(); + + const select = wrapper.findComponent({ name: 'Select' }); + expect(select.props('modelValue')).toBe('cherry'); + + }); +});