|
| 1 | +// Composables |
| 2 | +import { makeAutocompleteProps, useAutocomplete } from '../autocomplete' |
| 3 | + |
| 4 | +// Utilities |
| 5 | +import { mount } from '@vue/test-utils' |
| 6 | +import { defineComponent, h, nextTick } from 'vue' |
| 7 | + |
| 8 | +// Types |
| 9 | +import type { InputAutocompleteProps } from '../autocomplete' |
| 10 | + |
| 11 | +describe('input-autocomplete', () => { |
| 12 | + function mountFunction (props: Partial<InputAutocompleteProps> = {}) { |
| 13 | + return mount(defineComponent({ |
| 14 | + props: { |
| 15 | + name: String, |
| 16 | + ...makeAutocompleteProps(), |
| 17 | + }, |
| 18 | + setup (props) { |
| 19 | + const { fieldAutocomplete, fieldName, isSuppressing, update } = useAutocomplete(props) |
| 20 | + |
| 21 | + return () => h('input', { |
| 22 | + autocomplete: fieldAutocomplete.value, |
| 23 | + name: fieldName.value, |
| 24 | + onFocus: () => isSuppressing.value && update(), |
| 25 | + }) |
| 26 | + }, |
| 27 | + }), { props }) |
| 28 | + } |
| 29 | + |
| 30 | + it.each([ |
| 31 | + [{ autocomplete: undefined, name: undefined }], |
| 32 | + [{ autocomplete: 'on', name: undefined }], |
| 33 | + [{ autocomplete: undefined, name: 'username' }], |
| 34 | + [{ autocomplete: 'current-password', name: 'password' }], |
| 35 | + [{ autocomplete: 'shipping street-address', name: 'shipping-address' }], |
| 36 | + [{ autocomplete: 'off', name: 'email' }], |
| 37 | + ])('should pass through regular props', async (props: InputAutocompleteProps) => { |
| 38 | + const wrapper = mountFunction(props) |
| 39 | + |
| 40 | + expect(wrapper.vm.$el.autocomplete).toEqual(props.autocomplete ?? '') |
| 41 | + expect(wrapper.vm.$el.name).toEqual(props.name ?? '') |
| 42 | + |
| 43 | + wrapper.trigger('focus') |
| 44 | + await nextTick() |
| 45 | + expect(wrapper.vm.$el.name).toEqual(props.name ?? '') |
| 46 | + }) |
| 47 | + |
| 48 | + it('[suppress] should update field name on focus', async () => { |
| 49 | + const wrapper = mountFunction({ autocomplete: 'suppress', name: 'username' }) |
| 50 | + |
| 51 | + expect(wrapper.vm.$el.autocomplete).toBe('off') |
| 52 | + expect(wrapper.vm.$el.name).toBe('username-v-0-0') |
| 53 | + |
| 54 | + wrapper.trigger('focus') |
| 55 | + await nextTick() |
| 56 | + expect(wrapper.vm.$el.name).toMatch(/username-v-0-\d{13}/) |
| 57 | + }) |
| 58 | +}) |
0 commit comments