11import { JSDOM } from 'jsdom' ;
2- import { beforeEach , describe , expect , it } from 'vitest' ;
2+ import { beforeEach , describe , expect , it , vi } from 'vitest' ;
33import SimpleJekyllSearch from '../src/SimpleJekyllSearch' ;
44import { SearchData , SearchOptions } from '../src/utils/types' ;
55
@@ -144,4 +144,98 @@ describe('SimpleJekyllSearch', () => {
144144 expect ( resultsContainer . innerHTML ) . toContain ( 'Test Post' ) ;
145145 } ) ;
146146 } ) ;
147+
148+ describe ( 'error handling' , ( ) => {
149+ beforeEach ( ( ) => {
150+ mockOptions . json = mockSearchData ;
151+ } ) ;
152+
153+ it ( 'should call onError callback when provided' , async ( ) => {
154+ const onErrorSpy = vi . fn ( ) ;
155+ const optionsWithErrorHandler = { ...mockOptions , onError : onErrorSpy } ;
156+
157+ searchInstance . init ( optionsWithErrorHandler ) ;
158+
159+ const input = mockOptions . searchInput ;
160+ input . value = 'test' ;
161+ input . dispatchEvent ( new dom . window . KeyboardEvent ( 'input' , { key : 't' } ) ) ;
162+
163+ await new Promise ( resolve => setTimeout ( resolve , mockOptions . debounceTime ! + 10 ) ) ;
164+
165+ expect ( onErrorSpy ) . not . toHaveBeenCalled ( ) ;
166+ } ) ;
167+
168+ it ( 'should handle malformed search data gracefully' , async ( ) => {
169+ const onErrorSpy = vi . fn ( ) ;
170+ const consoleErrorSpy = vi . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
171+
172+ const malformedData = [
173+ { title : 'Valid Post' , url : '/valid' } ,
174+ { title : null , url : undefined } ,
175+ { title : 'Another Valid Post' , url : '/another' }
176+ ] ;
177+
178+ const optionsWithMalformedData = {
179+ ...mockOptions ,
180+ json : malformedData as any ,
181+ onError : onErrorSpy
182+ } ;
183+
184+ expect ( ( ) => searchInstance . init ( optionsWithMalformedData ) ) . not . toThrow ( ) ;
185+
186+ const input = mockOptions . searchInput ;
187+ input . value = 'Valid' ;
188+ input . dispatchEvent ( new dom . window . KeyboardEvent ( 'input' , { key : 'V' } ) ) ;
189+
190+ await new Promise ( resolve => setTimeout ( resolve , mockOptions . debounceTime ! + 10 ) ) ;
191+
192+ expect ( onErrorSpy ) . toHaveBeenCalledWith ( expect . any ( Error ) ) ;
193+ consoleErrorSpy . mockRestore ( ) ;
194+ } ) ;
195+
196+ it ( 'should handle missing DOM elements gracefully' , async ( ) => {
197+ const onErrorSpy = vi . fn ( ) ;
198+ const consoleErrorSpy = vi . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
199+
200+ const optionsWithMissingElement = {
201+ ...mockOptions ,
202+ searchInput : null as any ,
203+ onError : onErrorSpy
204+ } ;
205+
206+ expect ( ( ) => searchInstance . init ( optionsWithMissingElement ) ) . toThrow ( ) ;
207+ consoleErrorSpy . mockRestore ( ) ;
208+ } ) ;
209+
210+ it ( 'should use default error handler when onError not provided' , async ( ) => {
211+ const consoleErrorSpy = vi . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
212+
213+ searchInstance . init ( mockOptions ) ;
214+
215+ const input = mockOptions . searchInput ;
216+ input . value = 'test' ;
217+ input . dispatchEvent ( new dom . window . KeyboardEvent ( 'input' , { key : 't' } ) ) ;
218+
219+ await new Promise ( resolve => setTimeout ( resolve , mockOptions . debounceTime ! + 10 ) ) ;
220+
221+ expect ( consoleErrorSpy ) . not . toHaveBeenCalled ( ) ;
222+ consoleErrorSpy . mockRestore ( ) ;
223+ } ) ;
224+
225+ it ( 'should handle invalid search queries gracefully' , async ( ) => {
226+ const onErrorSpy = vi . fn ( ) ;
227+ const optionsWithErrorHandler = { ...mockOptions , onError : onErrorSpy } ;
228+
229+ searchInstance . init ( optionsWithErrorHandler ) ;
230+
231+ const input = mockOptions . searchInput ;
232+ input . value = 'a' . repeat ( 10000 ) ;
233+ input . dispatchEvent ( new dom . window . KeyboardEvent ( 'input' , { key : 'a' } ) ) ;
234+
235+ await new Promise ( resolve => setTimeout ( resolve , mockOptions . debounceTime ! + 10 ) ) ;
236+
237+ expect ( mockOptions . resultsContainer . innerHTML ) . toContain ( 'No results found' ) ;
238+ expect ( onErrorSpy ) . not . toHaveBeenCalled ( ) ;
239+ } ) ;
240+ } ) ;
147241} ) ;
0 commit comments