@@ -9,13 +9,30 @@ const mockApiIndex = {
99
1010const mockSvg = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><circle cx="256" cy="256" r="200"/></svg>'
1111
12- jest . mock ( '../../../../../../utils/icons/reactIcons' , ( ) => ( {
13- getIconSvg : jest . fn ( ( setId : string , iconName : string ) => {
14- if ( setId === 'fa' && iconName === 'FaCircle' ) {
15- return Promise . resolve ( mockSvg )
12+ const mockIconSvgs : Record < string , Record < string , string > > = {
13+ fa : { FaCircle : mockSvg } ,
14+ }
15+
16+ function createFetchMock ( ) : typeof fetch {
17+ return jest . fn ( ( input : RequestInfo | URL ) => {
18+ const url = typeof input === 'string' ? input : input . toString ( )
19+ const match = url . match ( / \/ i c o n s S v g s \/ ( [ ^ / ] + ) \. j s o n / )
20+ if ( match ) {
21+ const setId = match [ 1 ]
22+ const svgs = mockIconSvgs [ setId ] ?? { }
23+ return Promise . resolve ( {
24+ ok : true ,
25+ json : ( ) => Promise . resolve ( svgs ) ,
26+ } as Response )
1627 }
17- return Promise . resolve ( null )
18- } ) ,
28+ return Promise . resolve ( {
29+ ok : true ,
30+ json : ( ) => Promise . resolve ( mockApiIndex ) ,
31+ } as Response )
32+ } ) as typeof fetch
33+ }
34+
35+ jest . mock ( '../../../../../../utils/icons/reactIcons' , ( ) => ( {
1936 parseIconId : jest . fn ( ( iconId : string ) => {
2037 const underscoreIndex = iconId . indexOf ( '_' )
2138 if ( underscoreIndex <= 0 ) {
@@ -31,12 +48,7 @@ jest.mock('../../../../../../utils/icons/reactIcons', () => ({
3148} ) )
3249
3350it ( 'returns SVG markup for valid icon' , async ( ) => {
34- global . fetch = jest . fn ( ( ) =>
35- Promise . resolve ( {
36- ok : true ,
37- json : ( ) => Promise . resolve ( mockApiIndex ) ,
38- } as Response ) ,
39- )
51+ global . fetch = createFetchMock ( )
4052
4153 const response = await GET ( {
4254 params : { version : 'v6' , iconName : 'fa_FaCircle' } ,
@@ -55,12 +67,7 @@ it('returns SVG markup for valid icon', async () => {
5567} )
5668
5769it ( 'returns 404 when icon is not found in set' , async ( ) => {
58- global . fetch = jest . fn ( ( ) =>
59- Promise . resolve ( {
60- ok : true ,
61- json : ( ) => Promise . resolve ( mockApiIndex ) ,
62- } as Response ) ,
63- )
70+ global . fetch = createFetchMock ( )
6471
6572 const response = await GET ( {
6673 params : { version : 'v6' , iconName : 'fa_FaNonExistent' } ,
@@ -78,12 +85,7 @@ it('returns 404 when icon is not found in set', async () => {
7885} )
7986
8087it ( 'returns 400 for invalid icon name format (no underscore)' , async ( ) => {
81- global . fetch = jest . fn ( ( ) =>
82- Promise . resolve ( {
83- ok : true ,
84- json : ( ) => Promise . resolve ( mockApiIndex ) ,
85- } as Response ) ,
86- )
88+ global . fetch = createFetchMock ( )
8789
8890 const response = await GET ( {
8991 params : { version : 'v6' , iconName : 'invalid' } ,
@@ -101,12 +103,7 @@ it('returns 400 for invalid icon name format (no underscore)', async () => {
101103} )
102104
103105it ( 'returns 400 for invalid icon name format (leading underscore)' , async ( ) => {
104- global . fetch = jest . fn ( ( ) =>
105- Promise . resolve ( {
106- ok : true ,
107- json : ( ) => Promise . resolve ( mockApiIndex ) ,
108- } as Response ) ,
109- )
106+ global . fetch = createFetchMock ( )
110107
111108 const response = await GET ( {
112109 params : { version : 'v6' , iconName : '_FaCircle' } ,
@@ -122,12 +119,7 @@ it('returns 400 for invalid icon name format (leading underscore)', async () =>
122119} )
123120
124121it ( 'returns 400 when icon name parameter is missing' , async ( ) => {
125- global . fetch = jest . fn ( ( ) =>
126- Promise . resolve ( {
127- ok : true ,
128- json : ( ) => Promise . resolve ( mockApiIndex ) ,
129- } as Response ) ,
130- )
122+ global . fetch = createFetchMock ( )
131123
132124 const response = await GET ( {
133125 params : { version : 'v6' } ,
@@ -143,12 +135,7 @@ it('returns 400 when icon name parameter is missing', async () => {
143135} )
144136
145137it ( 'returns 404 for nonexistent version' , async ( ) => {
146- global . fetch = jest . fn ( ( ) =>
147- Promise . resolve ( {
148- ok : true ,
149- json : ( ) => Promise . resolve ( mockApiIndex ) ,
150- } as Response ) ,
151- )
138+ global . fetch = createFetchMock ( )
152139
153140 const response = await GET ( {
154141 params : { version : 'v99' , iconName : 'fa_FaCircle' } ,
@@ -165,12 +152,7 @@ it('returns 404 for nonexistent version', async () => {
165152} )
166153
167154it ( 'returns 400 when version parameter is missing' , async ( ) => {
168- global . fetch = jest . fn ( ( ) =>
169- Promise . resolve ( {
170- ok : true ,
171- json : ( ) => Promise . resolve ( mockApiIndex ) ,
172- } as Response ) ,
173- )
155+ global . fetch = createFetchMock ( )
174156
175157 const response = await GET ( {
176158 params : { iconName : 'fa_FaCircle' } ,
@@ -186,13 +168,20 @@ it('returns 400 when version parameter is missing', async () => {
186168} )
187169
188170it ( 'returns 500 when fetchApiIndex fails' , async ( ) => {
189- global . fetch = jest . fn ( ( ) =>
190- Promise . resolve ( {
191- ok : false ,
192- status : 500 ,
193- statusText : 'Internal Server Error' ,
194- } as Response ) ,
195- )
171+ global . fetch = jest . fn ( ( input : RequestInfo | URL ) => {
172+ const url = typeof input === 'string' ? input : input . toString ( )
173+ if ( url . includes ( 'apiIndex.json' ) ) {
174+ return Promise . resolve ( {
175+ ok : false ,
176+ status : 500 ,
177+ statusText : 'Internal Server Error' ,
178+ } as Response )
179+ }
180+ return Promise . resolve ( {
181+ ok : true ,
182+ json : ( ) => Promise . resolve ( { } ) ,
183+ } as Response )
184+ } ) as typeof fetch
196185
197186 const response = await GET ( {
198187 params : { version : 'v6' , iconName : 'fa_FaCircle' } ,
0 commit comments