|
| 1 | +import { mount } from '@vue/test-utils' |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 3 | +import { nextTick } from 'vue' |
| 4 | +import { createI18n } from 'vue-i18n' |
| 5 | + |
| 6 | +import SearchBox from './SearchBox.vue' |
| 7 | + |
| 8 | +const i18n = createI18n({ |
| 9 | + legacy: false, |
| 10 | + locale: 'en', |
| 11 | + messages: { |
| 12 | + en: { |
| 13 | + templateWidgets: { |
| 14 | + sort: { |
| 15 | + searchPlaceholder: 'Search...' |
| 16 | + } |
| 17 | + } |
| 18 | + } |
| 19 | + } |
| 20 | +}) |
| 21 | + |
| 22 | +describe('SearchBox', () => { |
| 23 | + beforeEach(() => { |
| 24 | + vi.clearAllMocks() |
| 25 | + vi.useFakeTimers() |
| 26 | + }) |
| 27 | + |
| 28 | + afterEach(() => { |
| 29 | + vi.restoreAllMocks() |
| 30 | + }) |
| 31 | + |
| 32 | + const createWrapper = (props = {}) => { |
| 33 | + return mount(SearchBox, { |
| 34 | + props: { |
| 35 | + modelValue: '', |
| 36 | + ...props |
| 37 | + }, |
| 38 | + global: { |
| 39 | + plugins: [i18n] |
| 40 | + } |
| 41 | + }) |
| 42 | + } |
| 43 | + |
| 44 | + describe('debounced search functionality', () => { |
| 45 | + it('should debounce search input by 300ms', async () => { |
| 46 | + const wrapper = createWrapper() |
| 47 | + const input = wrapper.find('input') |
| 48 | + |
| 49 | + // Type search query |
| 50 | + await input.setValue('test') |
| 51 | + |
| 52 | + // Model should not update immediately |
| 53 | + expect(wrapper.emitted('update:modelValue')).toBeFalsy() |
| 54 | + |
| 55 | + // Advance timers by 299ms (just before debounce delay) |
| 56 | + vi.advanceTimersByTime(299) |
| 57 | + await nextTick() |
| 58 | + expect(wrapper.emitted('update:modelValue')).toBeFalsy() |
| 59 | + |
| 60 | + // Advance timers by 1ms more (reaching 300ms) |
| 61 | + vi.advanceTimersByTime(1) |
| 62 | + await nextTick() |
| 63 | + |
| 64 | + // Model should now be updated |
| 65 | + expect(wrapper.emitted('update:modelValue')).toBeTruthy() |
| 66 | + expect(wrapper.emitted('update:modelValue')?.[0]).toEqual(['test']) |
| 67 | + }) |
| 68 | + |
| 69 | + it('should reset debounce timer on each keystroke', async () => { |
| 70 | + const wrapper = createWrapper() |
| 71 | + const input = wrapper.find('input') |
| 72 | + |
| 73 | + // Type first character |
| 74 | + await input.setValue('t') |
| 75 | + vi.advanceTimersByTime(200) |
| 76 | + await nextTick() |
| 77 | + |
| 78 | + // Type second character (should reset timer) |
| 79 | + await input.setValue('te') |
| 80 | + vi.advanceTimersByTime(200) |
| 81 | + await nextTick() |
| 82 | + |
| 83 | + // Type third character (should reset timer again) |
| 84 | + await input.setValue('tes') |
| 85 | + vi.advanceTimersByTime(200) |
| 86 | + await nextTick() |
| 87 | + |
| 88 | + // Should not have emitted yet (only 200ms passed since last keystroke) |
| 89 | + expect(wrapper.emitted('update:modelValue')).toBeFalsy() |
| 90 | + |
| 91 | + // Advance final 100ms to reach 300ms |
| 92 | + vi.advanceTimersByTime(100) |
| 93 | + await nextTick() |
| 94 | + |
| 95 | + // Should now emit with final value |
| 96 | + expect(wrapper.emitted('update:modelValue')).toBeTruthy() |
| 97 | + expect(wrapper.emitted('update:modelValue')?.[0]).toEqual(['tes']) |
| 98 | + }) |
| 99 | + |
| 100 | + it('should only emit final value after rapid typing', async () => { |
| 101 | + const wrapper = createWrapper() |
| 102 | + const input = wrapper.find('input') |
| 103 | + |
| 104 | + // Simulate rapid typing |
| 105 | + const searchTerms = ['s', 'se', 'sea', 'sear', 'searc', 'search'] |
| 106 | + for (const term of searchTerms) { |
| 107 | + await input.setValue(term) |
| 108 | + vi.advanceTimersByTime(50) // Less than debounce delay |
| 109 | + } |
| 110 | + |
| 111 | + // Should not have emitted yet |
| 112 | + expect(wrapper.emitted('update:modelValue')).toBeFalsy() |
| 113 | + |
| 114 | + // Complete the debounce delay |
| 115 | + vi.advanceTimersByTime(300) |
| 116 | + await nextTick() |
| 117 | + |
| 118 | + // Should emit only once with final value |
| 119 | + expect(wrapper.emitted('update:modelValue')).toHaveLength(1) |
| 120 | + expect(wrapper.emitted('update:modelValue')?.[0]).toEqual(['search']) |
| 121 | + }) |
| 122 | + |
| 123 | + describe('bidirectional model sync', () => { |
| 124 | + it('should sync external model changes to internal state', async () => { |
| 125 | + const wrapper = createWrapper({ modelValue: 'initial' }) |
| 126 | + const input = wrapper.find('input') |
| 127 | + |
| 128 | + expect(input.element.value).toBe('initial') |
| 129 | + |
| 130 | + // Update model externally |
| 131 | + await wrapper.setProps({ modelValue: 'external update' }) |
| 132 | + await nextTick() |
| 133 | + |
| 134 | + // Internal state should sync |
| 135 | + expect(input.element.value).toBe('external update') |
| 136 | + }) |
| 137 | + }) |
| 138 | + |
| 139 | + describe('placeholder', () => { |
| 140 | + it('should use custom placeholder when provided', () => { |
| 141 | + const wrapper = createWrapper({ placeholder: 'Custom search...' }) |
| 142 | + const input = wrapper.find('input') |
| 143 | + |
| 144 | + expect(input.attributes('placeholder')).toBe('Custom search...') |
| 145 | + expect(input.attributes('aria-label')).toBe('Custom search...') |
| 146 | + }) |
| 147 | + |
| 148 | + it('should use default placeholder when not provided', () => { |
| 149 | + const wrapper = createWrapper() |
| 150 | + const input = wrapper.find('input') |
| 151 | + |
| 152 | + expect(input.attributes('placeholder')).toBe('Search...') |
| 153 | + expect(input.attributes('aria-label')).toBe('Search...') |
| 154 | + }) |
| 155 | + }) |
| 156 | + |
| 157 | + describe('autofocus', () => { |
| 158 | + it('should focus input when autofocus is true', async () => { |
| 159 | + const wrapper = createWrapper({ autofocus: true }) |
| 160 | + await nextTick() |
| 161 | + |
| 162 | + const input = wrapper.find('input') |
| 163 | + const inputElement = input.element as HTMLInputElement |
| 164 | + |
| 165 | + // Note: In JSDOM, focus() doesn't actually set document.activeElement |
| 166 | + // We can only verify that the focus method exists and doesn't throw |
| 167 | + expect(inputElement.focus).toBeDefined() |
| 168 | + }) |
| 169 | + |
| 170 | + it('should not autofocus when autofocus is false', () => { |
| 171 | + const wrapper = createWrapper({ autofocus: false }) |
| 172 | + const input = wrapper.find('input') |
| 173 | + |
| 174 | + expect(document.activeElement).not.toBe(input.element) |
| 175 | + }) |
| 176 | + }) |
| 177 | + |
| 178 | + describe('click to focus', () => { |
| 179 | + it('should focus input when wrapper is clicked', async () => { |
| 180 | + const wrapper = createWrapper() |
| 181 | + const wrapperDiv = wrapper.find('[class*="flex"]') |
| 182 | + |
| 183 | + await wrapperDiv.trigger('click') |
| 184 | + await nextTick() |
| 185 | + |
| 186 | + // Input should receive focus |
| 187 | + const input = wrapper.find('input').element as HTMLInputElement |
| 188 | + expect(input.focus).toBeDefined() |
| 189 | + }) |
| 190 | + }) |
| 191 | + }) |
| 192 | +}) |
0 commit comments