Skip to content

Commit 9228833

Browse files
address PR feedback
1 parent 9d6dfa2 commit 9228833

File tree

8 files changed

+267
-84
lines changed

8 files changed

+267
-84
lines changed

src/__tests__/pages/api/__tests__/[version]/[section]/[page]/[tab].test.ts

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
134152
beforeEach(() => {
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

196223
it('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

212242
it('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

227260
it('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

243279
it('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

259298
it('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

src/pages/api/[version]/[section]/[page]/[tab].ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable no-console */
21
import type { APIRoute } from 'astro'
32
import { fetchApiIndex } from '../../../../../utils/apiIndex/fetch'
43
import { createJsonResponse, createIndexKey } from '../../../../../utils/apiHelpers'

src/pages/api/[version]/[section]/[page]/[tab]/examples.ts

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,9 @@
1-
import type { APIRoute, GetStaticPaths } from 'astro'
1+
import type { APIRoute } from 'astro'
22
import { createJsonResponse, createIndexKey } from '../../../../../../utils/apiHelpers'
3-
import { getApiIndex } from '../../../../../../utils/apiIndex/get'
43
import { fetchApiIndex } from '../../../../../../utils/apiIndex/fetch'
54

65
export const prerender = false
76

8-
export const getStaticPaths: GetStaticPaths = async () => {
9-
// Use the pre-generated index file
10-
const index = await getApiIndex()
11-
12-
const paths: { params: { version: string; section: string; page: string; tab: string } }[] = []
13-
14-
// Build paths from index structure
15-
for (const version of index.versions) {
16-
for (const section of index.sections[version] || []) {
17-
const sectionKey = createIndexKey(version, section)
18-
for (const page of index.pages[sectionKey] || []) {
19-
const pageKey = createIndexKey(version, section, page)
20-
for (const tab of index.tabs[pageKey] || []) {
21-
// Only create paths for tabs that have examples
22-
const tabKey = createIndexKey(version, section, page, tab)
23-
if (index.examples[tabKey] && index.examples[tabKey].length > 0) {
24-
paths.push({ params: { version, section, page, tab } })
25-
}
26-
}
27-
}
28-
}
29-
}
30-
31-
return paths
32-
}
33-
347
export const GET: APIRoute = async ({ params, url }) => {
358
const { version, section, page, tab } = params
369

0 commit comments

Comments
 (0)