|
| 1 | +import { VDateInput } from '../VDateInput' |
| 2 | + |
| 3 | +// Utilities |
| 4 | +import { mount } from '@vue/test-utils' |
| 5 | +import { createVuetify } from '@/framework' |
| 6 | + |
| 7 | +// global.ResizeObserver = require('resize-observer-polyfill') |
| 8 | + |
| 9 | +describe('VDateInput', () => { |
| 10 | + const vuetify = createVuetify() |
| 11 | + |
| 12 | + afterEach(() => { |
| 13 | + vi.clearAllMocks() |
| 14 | + }) |
| 15 | + |
| 16 | + function mountFunction (component: any, options = {}) { |
| 17 | + return mount(component, { |
| 18 | + global: { |
| 19 | + plugins: [vuetify], |
| 20 | + }, |
| 21 | + ...options, |
| 22 | + }) |
| 23 | + } |
| 24 | + |
| 25 | + describe('update-on prop', () => { |
| 26 | + const TEST_DATE = '2025-01-01' |
| 27 | + |
| 28 | + it('should update modelValue only on enter key press', async () => { |
| 29 | + const wrapper = mountFunction( |
| 30 | + <VDateInput |
| 31 | + updateOn={['enter']} |
| 32 | + modelValue={ null } |
| 33 | + /> |
| 34 | + ) |
| 35 | + const input = wrapper.find('input') |
| 36 | + |
| 37 | + await input.trigger('click') |
| 38 | + await input.trigger('focus') |
| 39 | + await input.setValue(TEST_DATE) |
| 40 | + |
| 41 | + await input.trigger('keydown', { key: 'Enter' }) |
| 42 | + expect(wrapper.emitted('update:modelValue')).toBeTruthy() |
| 43 | + |
| 44 | + await input.trigger('blur') |
| 45 | + expect(wrapper.emitted('update:modelValue')).toHaveLength(1) |
| 46 | + }) |
| 47 | + |
| 48 | + it('should update modelValue only on blur event', async () => { |
| 49 | + const wrapper = mountFunction( |
| 50 | + <VDateInput |
| 51 | + updateOn={['blur']} |
| 52 | + modelValue={ null } |
| 53 | + /> |
| 54 | + ) |
| 55 | + const input = wrapper.find('input') |
| 56 | + |
| 57 | + await input.trigger('click') |
| 58 | + await input.trigger('focus') |
| 59 | + await input.setValue(TEST_DATE) |
| 60 | + |
| 61 | + await input.trigger('keydown', { key: 'Enter' }) |
| 62 | + expect(wrapper.emitted('update:modelValue')).toBeFalsy() |
| 63 | + |
| 64 | + await input.trigger('blur') |
| 65 | + expect(wrapper.emitted('update:modelValue')).toBeTruthy() |
| 66 | + }) |
| 67 | + |
| 68 | + it('should update modelValue on both enter key press and blur event', async () => { |
| 69 | + const wrapper = mountFunction( |
| 70 | + <VDateInput |
| 71 | + updateOn={['enter', 'blur']} |
| 72 | + modelValue={ null } |
| 73 | + /> |
| 74 | + ) |
| 75 | + const input = wrapper.find('input') |
| 76 | + |
| 77 | + await input.trigger('click') |
| 78 | + await input.trigger('focus') |
| 79 | + await input.setValue(TEST_DATE) |
| 80 | + |
| 81 | + await input.trigger('keydown', { key: 'Enter' }) |
| 82 | + expect(wrapper.emitted('update:modelValue')).toBeTruthy() |
| 83 | + |
| 84 | + await input.trigger('blur') |
| 85 | + expect(wrapper.emitted('update:modelValue')).toHaveLength(2) |
| 86 | + }) |
| 87 | + |
| 88 | + it('should make the input readonly and prevent value updates', async () => { |
| 89 | + const wrapper = mountFunction( |
| 90 | + <VDateInput |
| 91 | + updateOn={[]} |
| 92 | + modelValue={ null } |
| 93 | + /> |
| 94 | + ) |
| 95 | + const input = wrapper.find('input') |
| 96 | + |
| 97 | + expect(input.attributes('readonly')).toBeDefined() |
| 98 | + expect(input.element.readOnly).toBe(true) |
| 99 | + |
| 100 | + await input.trigger('click') |
| 101 | + await input.trigger('focus') |
| 102 | + |
| 103 | + await input.setValue(TEST_DATE) |
| 104 | + await input.trigger('keydown', { key: 'Enter' }) |
| 105 | + await input.trigger('blur') |
| 106 | + |
| 107 | + expect(wrapper.emitted('update:modelValue')).toBeFalsy() |
| 108 | + }) |
| 109 | + }) |
| 110 | +}) |
0 commit comments