import { createWrapper } from 'app/test/vitest/helper.js'; import { describe, it, expect } from 'vitest'; import VnInputDate from 'components/common/VnInputDate.vue'; let vm; let wrapper; function generateWrapper(outlined = false, required = false) { wrapper = createWrapper(VnInputDate, { props: { modelValue: '2000-12-31T23:00:00.000Z', 'onUpdate:modelValue': (e) => wrapper.setProps({ modelValue: e }), }, attrs: { isOutlined: outlined, required: required, }, }); wrapper = wrapper.wrapper; vm = wrapper.vm; } describe('VnInputDate', () => { describe('formattedDate', () => { it('validateAndCleanInput should remove non-numeric characters', async () => { generateWrapper(); vm.validateAndCleanInput('10a/1s2/2dd0a23'); await vm.$nextTick(); expect(vm.inputValue).toBe('10/12/2023'); }); it('manageDate should reverse the date', async () => { generateWrapper(); vm.manageDate('10/12/2023'); await vm.$nextTick(); expect(vm.inputValue).toBe('2023/12/10'); }); it('formatDate should format the date correctly when a valid date is entered with full year', async () => { const input = wrapper.find('input'); await input.setValue('25.12/2002'); await vm.$nextTick(); await vm.formatDate(); expect(vm.model).toBe('2002-12-24T23:00:00.000Z'); }); it('should format the date correctly when a valid date is entered with short year', async () => { const input = wrapper.find('input'); await input.setValue('31.12-23'); await vm.$nextTick(); await vm.formatDate(); expect(vm.model).toBe('2023-12-30T23:00:00.000Z'); }); it('should format the date correctly when a valid date is entered without year', async () => { const input = wrapper.find('input'); await input.setValue('12.03'); await vm.$nextTick(); await vm.formatDate(); expect(vm.model).toBe('2001-03-11T23:00:00.000Z'); }); }); describe('styleAttrs', () => { it('should return empty styleAttrs when isOutlined is false', async () => { generateWrapper(); await vm.$nextTick(); expect(vm.styleAttrs).toEqual({}); }); it('should set styleAttrs when isOutlined is true', async () => { generateWrapper(true, false); await vm.$nextTick(); expect(vm.styleAttrs.outlined).toBe(true); }); }); describe('required', () => { it('should not applies required class when isRequired is false', async () => { generateWrapper(); await vm.$nextTick(); expect(wrapper.find('.vn-input-date').classes()).not.toContain('required'); }); it('should applies required class when isRequired is true', async () => { generateWrapper(false, true); await vm.$nextTick(); expect(wrapper.find('.vn-input-date').classes()).toContain('required'); }); }); });