|
| 1 | +import { describe, it, expect } from 'vitest' |
| 2 | +import TTaginput from '../controls/taginput.vue' |
| 3 | +import { |
| 4 | + mountComponent, |
| 5 | + triggerKeyboard |
| 6 | +} from '../lib/testutil/component-helpers' |
| 7 | + |
| 8 | +const fruitOptions = [ |
| 9 | + { value: 'apple', label: 'Apple' }, |
| 10 | + { value: 'banana', label: 'Banana' }, |
| 11 | + { value: 'cherry', label: 'Cherry' } |
| 12 | +] |
| 13 | + |
| 14 | +describe('TTaginput allowNew', () => { |
| 15 | + it('adds a free-form tag on Enter', async () => { |
| 16 | + const wrapper = mountComponent(TTaginput, { |
| 17 | + props: { modelValue: [], options: fruitOptions, allowNew: true } |
| 18 | + }) |
| 19 | + const input = wrapper.find('input') |
| 20 | + await input.setValue('custom') |
| 21 | + await triggerKeyboard(wrapper, 'Enter', 'input') |
| 22 | + const emitted = wrapper.emitted('update:modelValue') as string[][] |
| 23 | + expect(emitted[emitted.length - 1]).toEqual([['custom']]) |
| 24 | + }) |
| 25 | + |
| 26 | + it('adds a free-form tag on separator key', async () => { |
| 27 | + const wrapper = mountComponent(TTaginput, { |
| 28 | + props: { modelValue: [], options: fruitOptions, allowNew: true } |
| 29 | + }) |
| 30 | + const input = wrapper.find('input') |
| 31 | + await input.setValue('newtag') |
| 32 | + await triggerKeyboard(wrapper, ',', 'input') |
| 33 | + const emitted = wrapper.emitted('update:modelValue') as string[][] |
| 34 | + expect(emitted[emitted.length - 1]).toEqual([['newtag']]) |
| 35 | + }) |
| 36 | + |
| 37 | + it('does not add free-form tag when allowNew is false', async () => { |
| 38 | + const wrapper = mountComponent(TTaginput, { |
| 39 | + props: { modelValue: [], options: fruitOptions, allowNew: false } |
| 40 | + }) |
| 41 | + const input = wrapper.find('input') |
| 42 | + await input.setValue('custom') |
| 43 | + await triggerKeyboard(wrapper, 'Enter', 'input') |
| 44 | + expect(wrapper.emitted('update:modelValue')).toBeFalsy() |
| 45 | + }) |
| 46 | + |
| 47 | + it('does not add duplicate tags', async () => { |
| 48 | + const wrapper = mountComponent(TTaginput, { |
| 49 | + props: { modelValue: ['existing'], options: fruitOptions, allowNew: true } |
| 50 | + }) |
| 51 | + const input = wrapper.find('input') |
| 52 | + await input.setValue('existing') |
| 53 | + await triggerKeyboard(wrapper, 'Enter', 'input') |
| 54 | + // Should clear input but not emit a new modelValue with duplicate |
| 55 | + expect(wrapper.emitted('update:modelValue')).toBeFalsy() |
| 56 | + }) |
| 57 | + |
| 58 | + it('respects maxTags limit', async () => { |
| 59 | + const wrapper = mountComponent(TTaginput, { |
| 60 | + props: { modelValue: ['a', 'b'], options: fruitOptions, allowNew: true, maxTags: 2 } |
| 61 | + }) |
| 62 | + const input = wrapper.find('input') |
| 63 | + await input.setValue('third') |
| 64 | + await triggerKeyboard(wrapper, 'Enter', 'input') |
| 65 | + expect(wrapper.emitted('update:modelValue')).toBeFalsy() |
| 66 | + }) |
| 67 | + |
| 68 | + it('trims whitespace from new tags', async () => { |
| 69 | + const wrapper = mountComponent(TTaginput, { |
| 70 | + props: { modelValue: [], options: fruitOptions, allowNew: true } |
| 71 | + }) |
| 72 | + const input = wrapper.find('input') |
| 73 | + await input.setValue(' spaced ') |
| 74 | + await triggerKeyboard(wrapper, 'Enter', 'input') |
| 75 | + const emitted = wrapper.emitted('update:modelValue') as string[][] |
| 76 | + expect(emitted[emitted.length - 1]).toEqual([['spaced']]) |
| 77 | + }) |
| 78 | + |
| 79 | + it('does not add empty tags', async () => { |
| 80 | + const wrapper = mountComponent(TTaginput, { |
| 81 | + props: { modelValue: [], options: fruitOptions, allowNew: true } |
| 82 | + }) |
| 83 | + const input = wrapper.find('input') |
| 84 | + await input.setValue(' ') |
| 85 | + await triggerKeyboard(wrapper, 'Enter', 'input') |
| 86 | + expect(wrapper.emitted('update:modelValue')).toBeFalsy() |
| 87 | + }) |
| 88 | + |
| 89 | + it('selects dropdown option over free-form when highlighted', async () => { |
| 90 | + const wrapper = mountComponent(TTaginput, { |
| 91 | + props: { modelValue: [], options: fruitOptions, allowNew: true } |
| 92 | + }) |
| 93 | + const input = wrapper.find('input') |
| 94 | + await input.setValue('a') // filters to Apple |
| 95 | + // Arrow down to highlight first option, then Enter |
| 96 | + await triggerKeyboard(wrapper, 'ArrowDown', 'input') |
| 97 | + await triggerKeyboard(wrapper, 'Enter', 'input') |
| 98 | + const emitted = wrapper.emitted('update:modelValue') as string[][] |
| 99 | + expect(emitted[emitted.length - 1]).toEqual([['apple']]) |
| 100 | + }) |
| 101 | +}) |
0 commit comments