@@ -131,76 +131,106 @@ jest.mock('../../../../../../../utils/apiIndex/get', () => ({
131131 } ) ,
132132} ) )
133133
134+ /**
135+ * Mock fetchApiIndex to return the same data as getApiIndex
136+ */
137+ jest . mock ( '../../../../../../../utils/apiIndex/fetch' , ( ) => ( {
138+ fetchApiIndex : jest . fn ( ) . mockResolvedValue ( {
139+ versions : [ 'v6' ] ,
140+ sections : {
141+ v6 : [ 'components' ] ,
142+ } ,
143+ pages : {
144+ 'v6::components' : [ 'alert' ] ,
145+ } ,
146+ tabs : {
147+ 'v6::components::alert' : [ 'react' , 'html' , 'react-demos' ] ,
148+ } ,
149+ } ) ,
150+ } ) )
151+
134152beforeEach ( ( ) => {
135153 jest . clearAllMocks ( )
136154} )
137155
138- it ( 'returns markdown/MDX content as plain text' , async ( ) => {
156+ it ( 'redirects to /text endpoint' , async ( ) => {
157+ const mockRedirect = jest . fn ( ( path : string ) => new Response ( null , { status : 302 , headers : { Location : path } } ) )
139158 const response = await GET ( {
140159 params : {
141160 version : 'v6' ,
142161 section : 'components' ,
143162 page : 'alert' ,
144163 tab : 'react' ,
145164 } ,
165+ url : new URL ( 'http://localhost/api/v6/components/alert/react' ) ,
166+ redirect : mockRedirect ,
146167 } as any )
147- const body = await response . text ( )
148168
149- expect ( response . status ) . toBe ( 200 )
150- expect ( response . headers . get ( 'Content-Type' ) ) . toBe ( 'text/plain; charset=utf-8' )
151- expect ( typeof body ) . toBe ( 'string' )
152- expect ( body ) . toContain ( 'Alert Component' )
169+ expect ( mockRedirect ) . toHaveBeenCalledWith ( '/api/v6/components/alert/react/text' )
170+ expect ( response . status ) . toBe ( 302 )
153171} )
154172
155- it ( 'returns different content for different tabs' , async ( ) => {
173+ it ( 'redirects to /text endpoint for different tabs' , async ( ) => {
174+ const mockRedirect = jest . fn ( ( path : string ) => new Response ( null , { status : 302 , headers : { Location : path } } ) )
175+
156176 const reactResponse = await GET ( {
157177 params : {
158178 version : 'v6' ,
159179 section : 'components' ,
160180 page : 'alert' ,
161181 tab : 'react' ,
162182 } ,
183+ url : new URL ( 'http://localhost/api/v6/components/alert/react' ) ,
184+ redirect : mockRedirect ,
163185 } as any )
164- const reactBody = await reactResponse . text ( )
165186
187+ expect ( reactResponse . status ) . toBe ( 302 )
188+ expect ( mockRedirect ) . toHaveBeenCalledWith ( '/api/v6/components/alert/react/text' )
189+
190+ mockRedirect . mockClear ( )
166191 const htmlResponse = await GET ( {
167192 params : {
168193 version : 'v6' ,
169194 section : 'components' ,
170195 page : 'alert' ,
171196 tab : 'html' ,
172197 } ,
198+ url : new URL ( 'http://localhost/api/v6/components/alert/html' ) ,
199+ redirect : mockRedirect ,
173200 } as any )
174- const htmlBody = await htmlResponse . text ( )
175201
176- expect ( reactBody ) . toContain ( 'React Alert' )
177- expect ( htmlBody ) . toContain ( 'HTML' )
178- expect ( reactBody ) . not . toEqual ( htmlBody )
202+ expect ( htmlResponse . status ) . toBe ( 302 )
203+ expect ( mockRedirect ) . toHaveBeenCalledWith ( '/api/v6/components/alert/html/text' )
179204} )
180205
181- it ( 'returns demo content for demos tabs' , async ( ) => {
206+ it ( 'redirects demos tabs to /text endpoint' , async ( ) => {
207+ const mockRedirect = jest . fn ( ( path : string ) => new Response ( null , { status : 302 , headers : { Location : path } } ) )
182208 const response = await GET ( {
183209 params : {
184210 version : 'v6' ,
185211 section : 'components' ,
186212 page : 'alert' ,
187213 tab : 'react-demos' ,
188214 } ,
215+ url : new URL ( 'http://localhost/api/v6/components/alert/react-demos' ) ,
216+ redirect : mockRedirect ,
189217 } as any )
190- const body = await response . text ( )
191218
192- expect ( response . status ) . toBe ( 200 )
193- expect ( body ) . toContain ( ' demos')
219+ expect ( response . status ) . toBe ( 302 )
220+ expect ( mockRedirect ) . toHaveBeenCalledWith ( '/api/v6/components/alert/react- demos/text ')
194221} )
195222
196223it ( 'returns 404 error for nonexistent version' , async ( ) => {
224+ const mockRedirect = jest . fn ( )
197225 const response = await GET ( {
198226 params : {
199227 version : 'v99' ,
200228 section : 'components' ,
201229 page : 'alert' ,
202230 tab : 'react' ,
203231 } ,
232+ url : new URL ( 'http://localhost/api/v99/components/alert/react' ) ,
233+ redirect : mockRedirect ,
204234 } as any )
205235 const body = await response . json ( )
206236
@@ -210,13 +240,16 @@ it('returns 404 error for nonexistent version', async () => {
210240} )
211241
212242it ( 'returns 404 error for nonexistent section' , async ( ) => {
243+ const mockRedirect = jest . fn ( )
213244 const response = await GET ( {
214245 params : {
215246 version : 'v6' ,
216247 section : 'invalid' ,
217248 page : 'alert' ,
218249 tab : 'react' ,
219250 } ,
251+ url : new URL ( 'http://localhost/api/v6/invalid/alert/react' ) ,
252+ redirect : mockRedirect ,
220253 } as any )
221254 const body = await response . json ( )
222255
@@ -225,13 +258,16 @@ it('returns 404 error for nonexistent section', async () => {
225258} )
226259
227260it ( 'returns 404 error for nonexistent page' , async ( ) => {
261+ const mockRedirect = jest . fn ( )
228262 const response = await GET ( {
229263 params : {
230264 version : 'v6' ,
231265 section : 'components' ,
232266 page : 'nonexistent' ,
233267 tab : 'react' ,
234268 } ,
269+ url : new URL ( 'http://localhost/api/v6/components/nonexistent/react' ) ,
270+ redirect : mockRedirect ,
235271 } as any )
236272 const body = await response . json ( )
237273
@@ -241,13 +277,16 @@ it('returns 404 error for nonexistent page', async () => {
241277} )
242278
243279it ( 'returns 404 error for nonexistent tab' , async ( ) => {
280+ const mockRedirect = jest . fn ( )
244281 const response = await GET ( {
245282 params : {
246283 version : 'v6' ,
247284 section : 'components' ,
248285 page : 'alert' ,
249286 tab : 'nonexistent' ,
250287 } ,
288+ url : new URL ( 'http://localhost/api/v6/components/alert/nonexistent' ) ,
289+ redirect : mockRedirect ,
251290 } as any )
252291 const body = await response . json ( )
253292
@@ -257,12 +296,15 @@ it('returns 404 error for nonexistent tab', async () => {
257296} )
258297
259298it ( 'returns 400 error when required parameters are missing' , async ( ) => {
299+ const mockRedirect = jest . fn ( )
260300 const response = await GET ( {
261301 params : {
262302 version : 'v6' ,
263303 section : 'components' ,
264304 page : 'alert' ,
265305 } ,
306+ url : new URL ( 'http://localhost/api/v6/components/alert' ) ,
307+ redirect : mockRedirect ,
266308 } as any )
267309 const body = await response . json ( )
268310
0 commit comments