11import { GET } from '../../../../pages/api/[version]'
22
3- /**
4- * Mock apiIndex.json with multiple versions (v5, v6)
5- * to test section retrieval for different versions
6- */
7- jest . mock ( 'outputDir/apiIndex.json' , ( ) => ( {
3+ const mockApiIndex = {
84 versions : [ 'v5' , 'v6' ] ,
95 sections : {
106 v5 : [ 'getting-started' ] ,
117 v6 : [ 'components' , 'layouts' , 'utilities' ] ,
128 } ,
139 pages : { } ,
1410 tabs : { } ,
15- } ) )
11+ }
1612
1713it ( 'returns all sections for a valid version' , async ( ) => {
14+ global . fetch = jest . fn ( ( ) =>
15+ Promise . resolve ( {
16+ ok : true ,
17+ json : ( ) => Promise . resolve ( mockApiIndex ) ,
18+ } as Response )
19+ )
20+
1821 const response = await GET ( {
1922 params : { version : 'v6' } ,
23+ url : new URL ( 'http://localhost:4321/api/v6' ) ,
2024 } as any )
2125 const body = await response . json ( )
2226
@@ -26,67 +30,129 @@ it('returns all sections for a valid version', async () => {
2630 expect ( body ) . toContain ( 'components' )
2731 expect ( body ) . toContain ( 'layouts' )
2832 expect ( body ) . toContain ( 'utilities' )
33+
34+ jest . restoreAllMocks ( )
2935} )
3036
3137it ( 'returns only sections for the requested version' , async ( ) => {
38+ global . fetch = jest . fn ( ( ) =>
39+ Promise . resolve ( {
40+ ok : true ,
41+ json : ( ) => Promise . resolve ( mockApiIndex ) ,
42+ } as Response )
43+ )
44+
3245 const response = await GET ( {
3346 params : { version : 'v5' } ,
47+ url : new URL ( 'http://localhost:4321/api/v5' ) ,
3448 } as any )
3549 const body = await response . json ( )
3650
3751 expect ( response . status ) . toBe ( 200 )
3852 expect ( body ) . toContain ( 'getting-started' )
53+
54+ jest . restoreAllMocks ( )
3955} )
4056
4157it ( 'sorts sections alphabetically' , async ( ) => {
58+ global . fetch = jest . fn ( ( ) =>
59+ Promise . resolve ( {
60+ ok : true ,
61+ json : ( ) => Promise . resolve ( mockApiIndex ) ,
62+ } as Response )
63+ )
64+
4265 const response = await GET ( {
4366 params : { version : 'v6' } ,
67+ url : new URL ( 'http://localhost:4321/api/v6' ) ,
4468 } as any )
4569 const body = await response . json ( )
4670
4771 const sorted = [ ...body ] . sort ( )
4872 expect ( body ) . toEqual ( sorted )
73+
74+ jest . restoreAllMocks ( )
4975} )
5076
5177it ( 'deduplicates sections from multiple collections' , async ( ) => {
78+ global . fetch = jest . fn ( ( ) =>
79+ Promise . resolve ( {
80+ ok : true ,
81+ json : ( ) => Promise . resolve ( mockApiIndex ) ,
82+ } as Response )
83+ )
84+
5285 const response = await GET ( {
5386 params : { version : 'v6' } ,
87+ url : new URL ( 'http://localhost:4321/api/v6' ) ,
5488 } as any )
5589 const body = await response . json ( )
5690
5791 const unique = [ ...new Set ( body ) ]
5892 expect ( body ) . toEqual ( unique )
93+
94+ jest . restoreAllMocks ( )
5995} )
6096
6197it ( 'returns 404 error for nonexistent version' , async ( ) => {
98+ global . fetch = jest . fn ( ( ) =>
99+ Promise . resolve ( {
100+ ok : true ,
101+ json : ( ) => Promise . resolve ( mockApiIndex ) ,
102+ } as Response )
103+ )
104+
62105 const response = await GET ( {
63106 params : { version : 'v99' } ,
107+ url : new URL ( 'http://localhost:4321/api/v99' ) ,
64108 } as any )
65109 const body = await response . json ( )
66110
67111 expect ( response . status ) . toBe ( 404 )
68112 expect ( body ) . toHaveProperty ( 'error' )
69113 expect ( body . error ) . toContain ( 'v99' )
70114 expect ( body . error ) . toContain ( 'not found' )
115+
116+ jest . restoreAllMocks ( )
71117} )
72118
73119it ( 'returns 400 error when version parameter is missing' , async ( ) => {
120+ global . fetch = jest . fn ( ( ) =>
121+ Promise . resolve ( {
122+ ok : true ,
123+ json : ( ) => Promise . resolve ( mockApiIndex ) ,
124+ } as Response )
125+ )
126+
74127 const response = await GET ( {
75128 params : { } ,
129+ url : new URL ( 'http://localhost:4321/api/' ) ,
76130 } as any )
77131 const body = await response . json ( )
78132
79133 expect ( response . status ) . toBe ( 400 )
80134 expect ( body ) . toHaveProperty ( 'error' )
81135 expect ( body . error ) . toContain ( 'Version parameter is required' )
136+
137+ jest . restoreAllMocks ( )
82138} )
83139
84140it ( 'excludes content entries that have no section field' , async ( ) => {
141+ global . fetch = jest . fn ( ( ) =>
142+ Promise . resolve ( {
143+ ok : true ,
144+ json : ( ) => Promise . resolve ( mockApiIndex ) ,
145+ } as Response )
146+ )
147+
85148 const response = await GET ( {
86149 params : { version : 'v6' } ,
150+ url : new URL ( 'http://localhost:4321/api/v6' ) ,
87151 } as any )
88152 const body = await response . json ( )
89153
90154 // Should only include sections from entries that have data.section
91155 expect ( body . length ) . toBeGreaterThan ( 0 )
156+
157+ jest . restoreAllMocks ( )
92158} )
0 commit comments