Skip to content

Commit 76e5270

Browse files
fix unit tests
1 parent 06a90f4 commit 76e5270

File tree

2 files changed

+110
-14
lines changed

2 files changed

+110
-14
lines changed
Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
import { 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

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

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

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

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

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

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

84140
it('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
})
Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,66 @@
11
import { GET } from '../../../../pages/api/versions'
22

3-
/**
4-
* Mock apiIndex.json with multiple versions
5-
*/
6-
jest.mock('outputDir/apiIndex.json', () => ({
3+
const mockApiIndex = {
74
versions: ['v5', 'v6'],
85
sections: {},
96
pages: {},
107
tabs: {},
11-
}))
8+
}
129

1310
it('returns unique versions as sorted array', async () => {
14-
const response = await GET({} as any)
11+
global.fetch = jest.fn(() =>
12+
Promise.resolve({
13+
ok: true,
14+
json: () => Promise.resolve(mockApiIndex),
15+
} as Response)
16+
)
17+
18+
const response = await GET({
19+
url: new URL('http://localhost:4321/api/versions'),
20+
} as any)
1521
const body = await response.json()
1622

1723
expect(response.status).toBe(200)
1824
expect(response.headers.get('Content-Type')).toBe('application/json; charset=utf-8')
1925
expect(body).toEqual(['v5', 'v6'])
26+
27+
jest.restoreAllMocks()
2028
})
2129

2230
it('sorts versions alphabetically', async () => {
23-
const response = await GET({} as any)
31+
global.fetch = jest.fn(() =>
32+
Promise.resolve({
33+
ok: true,
34+
json: () => Promise.resolve(mockApiIndex),
35+
} as Response)
36+
)
37+
38+
const response = await GET({
39+
url: new URL('http://localhost:4321/api/versions'),
40+
} as any)
2441
const body = await response.json()
2542

2643
expect(body).toEqual(['v5', 'v6'])
44+
45+
jest.restoreAllMocks()
2746
})
2847

2948
it('returns only the versions from the index', async () => {
30-
const response = await GET({} as any)
49+
global.fetch = jest.fn(() =>
50+
Promise.resolve({
51+
ok: true,
52+
json: () => Promise.resolve(mockApiIndex),
53+
} as Response)
54+
)
55+
56+
const response = await GET({
57+
url: new URL('http://localhost:4321/api/versions'),
58+
} as any)
3159
const body = await response.json()
3260

3361
// Should return exactly the versions from the mocked apiIndex.json
3462
expect(body).toEqual(['v5', 'v6'])
3563
expect(body).toHaveLength(2)
64+
65+
jest.restoreAllMocks()
3666
})

0 commit comments

Comments
 (0)