11import { GET } from '../../../../../../pages/api/[version]/icons/index'
2- import { getAllIcons } from '../../../../../../utils/icons/reactIcons'
32
43const mockApiIndex = {
54 versions : [ 'v5' , 'v6' ] ,
@@ -35,8 +34,18 @@ const mockIcons = [
3534 } ,
3635]
3736
37+ function createFetchMock ( ) : typeof fetch {
38+ return jest . fn ( ( input : RequestInfo | URL ) => {
39+ const url = typeof input === 'string' ? input : input . toString ( )
40+ const json = ( ) =>
41+ Promise . resolve (
42+ url . includes ( 'iconsIndex.json' ) ? { icons : mockIcons } : mockApiIndex
43+ )
44+ return Promise . resolve ( { ok : true , json } as Response )
45+ } ) as typeof fetch
46+ }
47+
3848jest . mock ( '../../../../../../utils/icons/reactIcons' , ( ) => ( {
39- getAllIcons : jest . fn ( ( ) => Promise . resolve ( mockIcons ) ) ,
4049 filterIcons : jest . fn ( ( icons : typeof mockIcons , filter : string ) => {
4150 if ( ! filter || ! filter . trim ( ) ) {
4251 return icons
@@ -51,12 +60,7 @@ jest.mock('../../../../../../utils/icons/reactIcons', () => ({
5160} ) )
5261
5362it ( 'returns all icons with metadata' , async ( ) => {
54- global . fetch = jest . fn ( ( ) =>
55- Promise . resolve ( {
56- ok : true ,
57- json : ( ) => Promise . resolve ( mockApiIndex ) ,
58- } as Response ) ,
59- )
63+ global . fetch = createFetchMock ( )
6064
6165 const response = await GET ( {
6266 params : { version : 'v6' } ,
@@ -83,12 +87,7 @@ it('returns all icons with metadata', async () => {
8387} )
8488
8589it ( 'filters icons when filter parameter is provided' , async ( ) => {
86- global . fetch = jest . fn ( ( ) =>
87- Promise . resolve ( {
88- ok : true ,
89- json : ( ) => Promise . resolve ( mockApiIndex ) ,
90- } as Response ) ,
91- )
90+ global . fetch = createFetchMock ( )
9291
9392 const response = await GET ( {
9493 params : { version : 'v6' } ,
@@ -106,12 +105,7 @@ it('filters icons when filter parameter is provided', async () => {
106105} )
107106
108107it ( 'filter is case-insensitive' , async ( ) => {
109- global . fetch = jest . fn ( ( ) =>
110- Promise . resolve ( {
111- ok : true ,
112- json : ( ) => Promise . resolve ( mockApiIndex ) ,
113- } as Response ) ,
114- )
108+ global . fetch = createFetchMock ( )
115109
116110 const response = await GET ( {
117111 params : { version : 'v6' } ,
@@ -126,12 +120,7 @@ it('filter is case-insensitive', async () => {
126120} )
127121
128122it ( 'returns empty icons array when filter yields no matches' , async ( ) => {
129- global . fetch = jest . fn ( ( ) =>
130- Promise . resolve ( {
131- ok : true ,
132- json : ( ) => Promise . resolve ( mockApiIndex ) ,
133- } as Response ) ,
134- )
123+ global . fetch = createFetchMock ( )
135124
136125 const response = await GET ( {
137126 params : { version : 'v6' } ,
@@ -147,12 +136,7 @@ it('returns empty icons array when filter yields no matches', async () => {
147136} )
148137
149138it ( 'returns 404 error for nonexistent version' , async ( ) => {
150- global . fetch = jest . fn ( ( ) =>
151- Promise . resolve ( {
152- ok : true ,
153- json : ( ) => Promise . resolve ( mockApiIndex ) ,
154- } as Response ) ,
155- )
139+ global . fetch = createFetchMock ( )
156140
157141 const response = await GET ( {
158142 params : { version : 'v99' } ,
@@ -169,12 +153,7 @@ it('returns 404 error for nonexistent version', async () => {
169153} )
170154
171155it ( 'returns 400 error when version parameter is missing' , async ( ) => {
172- global . fetch = jest . fn ( ( ) =>
173- Promise . resolve ( {
174- ok : true ,
175- json : ( ) => Promise . resolve ( mockApiIndex ) ,
176- } as Response ) ,
177- )
156+ global . fetch = createFetchMock ( )
178157
179158 const response = await GET ( {
180159 params : { } ,
@@ -189,15 +168,17 @@ it('returns 400 error when version parameter is missing', async () => {
189168 jest . restoreAllMocks ( )
190169} )
191170
192- it ( 'returns 500 error when getAllIcons throws' , async ( ) => {
193- ; ( getAllIcons as jest . Mock ) . mockRejectedValueOnce ( new Error ( 'Load failed' ) )
194-
195- global . fetch = jest . fn ( ( ) =>
196- Promise . resolve ( {
171+ it ( 'returns 500 error when fetchIconsIndex throws' , async ( ) => {
172+ global . fetch = jest . fn ( ( input : RequestInfo | URL ) => {
173+ const url = typeof input === 'string' ? input : input . toString ( )
174+ if ( url . includes ( 'iconsIndex.json' ) ) {
175+ return Promise . resolve ( { ok : false , status : 500 , statusText : 'Internal Server Error' } as Response )
176+ }
177+ return Promise . resolve ( {
197178 ok : true ,
198179 json : ( ) => Promise . resolve ( mockApiIndex ) ,
199- } as Response ) ,
200- )
180+ } as Response )
181+ } ) as typeof fetch
201182
202183 const response = await GET ( {
203184 params : { version : 'v6' } ,
0 commit comments