@@ -99,6 +99,36 @@ describe('useFilterableApi', () => {
9999 } ) ;
100100 } ) ;
101101
102+ it ( 'filters unfiltered API responses for non-empty search input' , async ( ) => {
103+ const mockData : TestData [ ] = [
104+ { id : '1' , name : 'John Doe' , email : 'john@example.com' } ,
105+ { id : '2' , name : 'Jane Smith' , email : 'jane@example.com' } ,
106+ { id : '3' , name : 'johnny Appleseed' , email : 'johnny@example.com' }
107+ ] ;
108+
109+ mockFetchApi . mockResolvedValueOnce ( {
110+ users : mockData ,
111+ meta : { pagination : { next : null } }
112+ } ) ;
113+
114+ const { result} = renderHook ( ( ) => useFilterableApi < TestData , 'users' , 'name' > ( {
115+ path : '/users' ,
116+ filterKey : 'name' ,
117+ responseKey : 'users'
118+ } ) ) ;
119+
120+ const data = await result . current . loadData ( 'john' ) ;
121+
122+ expect ( data ) . toEqual ( [
123+ { id : '1' , name : 'John Doe' , email : 'john@example.com' } ,
124+ { id : '3' , name : 'johnny Appleseed' , email : 'johnny@example.com' }
125+ ] ) ;
126+ expect ( mockApiUrl ) . toHaveBeenCalledWith ( '/users' , {
127+ filter : 'name:~\'john\'' ,
128+ limit : '20'
129+ } ) ;
130+ } ) ;
131+
102132 it ( 'escapes single quotes in filter values' , async ( ) => {
103133 const mockData : TestData [ ] = [ ] ;
104134
@@ -314,7 +344,7 @@ describe('useFilterableApi', () => {
314344 const firstResult = await result . current . loadData ( '' ) ;
315345 expect ( firstResult ) . toEqual ( mockData ) ;
316346
317- const secondResult = await result . current . loadData ( 'test ' ) ;
347+ const secondResult = await result . current . loadData ( 'john ' ) ;
318348 expect ( secondResult ) . toEqual ( mockData ) ;
319349 } ) ;
320350
@@ -425,7 +455,9 @@ describe('useFilterableApi', () => {
425455
426456 it ( 'handles rapid filter changes correctly' , async ( ) => {
427457 const mockData : TestData [ ] = [
428- { id : '1' , name : 'Test Result' }
458+ { id : '1' , name : 'test1' } ,
459+ { id : '2' , name : 'test2' } ,
460+ { id : '3' , name : 'test3' }
429461 ] ;
430462
431463 mockFetchApi . mockResolvedValue ( {
@@ -444,12 +476,10 @@ describe('useFilterableApi', () => {
444476 const promise2 = result . current . loadData ( 'test2' ) ;
445477 const promise3 = result . current . loadData ( 'test3' ) ;
446478
447- const results = await Promise . all ( [ promise1 , promise2 , promise3 ] ) ;
448-
449479 // All calls should return data
450- results . forEach ( ( resultItem ) => {
451- expect ( resultItem ) . toEqual ( mockData ) ;
452- } ) ;
480+ await expect ( promise1 ) . resolves . toEqual ( [ { id : '1' , name : 'test1' } ] ) ;
481+ await expect ( promise2 ) . resolves . toEqual ( [ { id : '2' , name : 'test2' } ] ) ;
482+ await expect ( promise3 ) . resolves . toEqual ( [ { id : '3' , name : 'test3' } ] ) ;
453483 } ) ;
454484 } ) ;
455485
0 commit comments