Skip to content

Commit 5bb4a95

Browse files
authored
Complete TypeScript migration for src/redirects - Convert all test files (#56633)
1 parent c0e0886 commit 5bb4a95

File tree

9 files changed

+35
-27
lines changed

9 files changed

+35
-27
lines changed
File renamed without changes.
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ describe('redirects', () => {
2323
basePath: path.join(__dirname, '../../../content'),
2424
languageCode: 'en',
2525
})
26+
if (!page) throw new Error('Failed to initialize page')
2627
const pageRedirects = page.buildRedirects()
2728
expect(isPlainObject(pageRedirects)).toBe(true)
2829
})
@@ -33,6 +34,7 @@ describe('redirects', () => {
3334
basePath: path.join(__dirname, '../../../content'),
3435
languageCode: 'en',
3536
})
37+
if (!page) throw new Error('Failed to initialize page')
3638
const pageRedirects = page.buildRedirects()
3739
expect(pageRedirects['/about-issues']).toBe('/issues')
3840
expect(pageRedirects['/creating-an-issue']).toBe('/issues')
@@ -88,7 +90,7 @@ describe('redirects', () => {
8890
})
8991

9092
describe('trailing slashes', () => {
91-
let redirects
93+
let redirects: Record<string, string>
9294
beforeAll(async () => {
9395
const res = await get('/en?json=redirects')
9496
redirects = JSON.parse(res.body)
@@ -102,7 +104,7 @@ describe('redirects', () => {
102104

103105
test('are absent from all destination URLs', async () => {
104106
const values = Object.entries(redirects)
105-
.filter(([, to]) => !to.includes('://'))
107+
.filter(([, to]: [string, string]) => !to.includes('://'))
106108
.map(([from_]) => from_)
107109
expect(values.length).toBeGreaterThan(100)
108110
expect(values.every((value) => !value.endsWith('/'))).toBe(true)
@@ -138,24 +140,24 @@ describe('redirects', () => {
138140
})
139141

140142
describe('external redirects', () => {
141-
let redirects
143+
let redirects: Record<string, string>
142144
beforeAll(async () => {
143145
const res = await get('/en?json=redirects')
144146
redirects = JSON.parse(res.body)
145147
})
146148

147149
test('no external redirect starts with a language prefix', () => {
148150
const values = Object.entries(redirects)
149-
.filter(([, to]) => to.includes('://'))
151+
.filter(([, to]: [string, string]) => to.includes('://'))
150152
.map(([from_]) => from_)
151153
.filter((from_) => from_.startsWith('/en/'))
152154
expect(values.length).toBe(0)
153155
})
154156

155157
test('no external redirect should go to developer.github.com', () => {
156158
const values = Object.values(redirects)
157-
.filter((to) => to.includes('://'))
158-
.filter((to) => new URL(to).hostname === 'developer.github.com')
159+
.filter((to: string) => to.includes('://'))
160+
.filter((to: string) => new URL(to).hostname === 'developer.github.com')
159161
expect(values.length).toBe(0)
160162
})
161163

src/redirects/tests/routing/developer-site-redirects.js renamed to src/redirects/tests/routing/developer-site-redirects.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,18 @@ describe('developer redirects', () => {
106106
graphql: './src/fixtures/fixtures/graphql-redirects.json',
107107
}
108108
if (!(label in FIXTURES)) throw new Error('unrecognized label')
109-
const fixtures = readJsonFile(FIXTURES[label])
109+
const fixtures = readJsonFile(FIXTURES[label as keyof typeof FIXTURES])
110110
// Don't use a `Promise.all()` because it's actually slower
111111
// because of all the eventloop context switching.
112112
for (let [oldPath, newPath] of Object.entries(fixtures)) {
113113
// REST and GraphQL developer Enterprise paths with a version are only supported up to 2.21.
114114
// We make an exception to always redirect versionless paths to the latest version.
115-
newPath = newPath.replace(
115+
newPath = (newPath as string).replace(
116116
'/enterprise-server/',
117117
`/enterprise-server@${enterpriseServerReleases.latest}/`,
118118
)
119119
const res = await get(oldPath)
120-
const sameFirstPrefix = oldPath.split('/')[1] === newPath.split('/')[1]
120+
const sameFirstPrefix = oldPath.split('/')[1] === (newPath as string).split('/')[1]
121121
expect(res.statusCode, `${oldPath} did not redirect to ${newPath}`).toBe(
122122
sameFirstPrefix ? 301 : 302,
123123
)
File renamed without changes.

src/redirects/tests/routing/top-developer-site-path-redirects.js renamed to src/redirects/tests/routing/top-developer-site-path-redirects.ts

File renamed without changes.

src/redirects/tests/routing/versionless-redirects.js renamed to src/redirects/tests/routing/versionless-redirects.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ describe('versioned redirects', () => {
7676
newPath.includes('/enterprise-server@latest'),
7777
)
7878

79-
enterpriseServerPaths.forEach(([oldPath, newPath]) => {
79+
enterpriseServerPaths.forEach(([, newPath]) => {
8080
const transformedPath = `/en${newPath.replace(
8181
'/enterprise-server@latest',
8282
`/enterprise-server@${latest}`,
Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ import {
88
oldestSupported,
99
} from '@/versions/lib/enterprise-server-releases'
1010

11+
// Test helper type for mocking contexts
12+
type TestContext = {
13+
pages: Record<string, any>
14+
redirects: Record<string, string>
15+
}
16+
1117
const previousEnterpriserServerVersion = supported[1]
1218

1319
describe('getRedirect basics', () => {
@@ -19,7 +25,7 @@ describe('getRedirect basics', () => {
1925
// part.
2026
// But some redirects from `developer.json` as old and static.
2127
const uri = '/enterprise/3.0/foo/bar'
22-
const ctx = {
28+
const ctx: TestContext = {
2329
pages: {},
2430
redirects: {
2531
'/enterprise/3.0/foo/bar': '/something/else',
@@ -29,15 +35,15 @@ describe('getRedirect basics', () => {
2935
})
3036

3137
test('should return undefined if nothing could be found', () => {
32-
const ctx = {
38+
const ctx: TestContext = {
3339
pages: {},
3440
redirects: {},
3541
}
3642
expect(getRedirect('/foo/pizza', ctx)).toBeUndefined()
3743
})
3844

3945
test('should just inject language on version "home pages"', () => {
40-
const ctx = {
46+
const ctx: TestContext = {
4147
pages: {},
4248
redirects: {},
4349
}
@@ -69,7 +75,7 @@ describe('getRedirect basics', () => {
6975
})
7076

7177
test('should handle some odd exceptions', () => {
72-
const ctx = {
78+
const ctx: TestContext = {
7379
pages: {},
7480
redirects: {},
7581
}
@@ -86,7 +92,7 @@ describe('getRedirect basics', () => {
8692
})
8793

8894
test('should figure out redirect based on presence of pages in certain cases', () => {
89-
const ctx = {
95+
const ctx: TestContext = {
9096
pages: {
9197
[`/en/enterprise-server@${previousEnterpriserServerVersion}/foo/bar`]: null,
9298
[`/en/enterprise-server@${previousEnterpriserServerVersion}/admin/github-management`]: null,
@@ -131,7 +137,7 @@ describe('getRedirect basics', () => {
131137
})
132138

133139
test('should not do anything on some prefixes', () => {
134-
const ctx = {
140+
const ctx: TestContext = {
135141
pages: {},
136142
redirects: {},
137143
}
@@ -159,7 +165,7 @@ describe('getRedirect basics', () => {
159165
test('should work for some deprecated enterprise-server URLs too', () => {
160166
// Starting with enterprise-server 3.0, we have made redirects become
161167
// a *function* rather than a lookup on a massive object.
162-
const ctx = {
168+
const ctx: TestContext = {
163169
pages: {},
164170
redirects: {},
165171
}
@@ -170,23 +176,23 @@ describe('getRedirect basics', () => {
170176

171177
describe('github-ae@latest', () => {
172178
test('home page should redirect to enterprise-cloud home page', () => {
173-
const ctx = {
179+
const ctx: TestContext = {
174180
pages: {},
175181
redirects: {},
176182
}
177183
expect(getRedirect('/github-ae@latest', ctx)).toBe('/en/enterprise-cloud@latest')
178184
expect(getRedirect('/en/github-ae@latest', ctx)).toBe('/en/enterprise-cloud@latest')
179185
})
180186
test('should redirect to home page for admin/release-notes', () => {
181-
const ctx = {
187+
const ctx: TestContext = {
182188
pages: {},
183189
redirects: {},
184190
}
185191
expect(getRedirect('/github-ae@latest/admin/release-notes', ctx)).toBe('/en')
186192
expect(getRedirect('/en/github-ae@latest/admin/release-notes', ctx)).toBe('/en')
187193
})
188194
test('a page that does exits, without correction, in enterprise-cloud', () => {
189-
const ctx = {
195+
const ctx: TestContext = {
190196
pages: {
191197
'/en/enterprise-cloud@latest/foo': null,
192198
},
@@ -196,7 +202,7 @@ describe('github-ae@latest', () => {
196202
expect(getRedirect('/en/github-ae@latest/foo', ctx)).toBe('/en/enterprise-cloud@latest/foo')
197203
})
198204
test("a page that doesn't exist in enterprise-cloud but in FPT", () => {
199-
const ctx = {
205+
const ctx: TestContext = {
200206
pages: {
201207
'/en/foo': true,
202208
},
@@ -206,7 +212,7 @@ describe('github-ae@latest', () => {
206212
expect(getRedirect('/en/github-ae@latest/foo', ctx)).toBe('/en/foo')
207213
})
208214
test("a page that doesn't exist in enterprise-cloud or in FPT", () => {
209-
const ctx = {
215+
const ctx: TestContext = {
210216
pages: {
211217
'/en/foo': true,
212218
},
@@ -216,7 +222,7 @@ describe('github-ae@latest', () => {
216222
expect(getRedirect('/en/github-ae@latest/bar', ctx)).toBe('/en')
217223
})
218224
test('a URL with legacy redirects, that redirects to enterprise-cloud', () => {
219-
const ctx = {
225+
const ctx: TestContext = {
220226
pages: {
221227
'/en/foo': true,
222228
'/en/enterprise-cloud@latest/foo': true,
@@ -229,7 +235,7 @@ describe('github-ae@latest', () => {
229235
expect(getRedirect('/en/github-ae@latest/food', ctx)).toBe('/en/enterprise-cloud@latest/foo')
230236
})
231237
test("a URL with legacy redirects, that can't redirect to enterprise-cloud", () => {
232-
const ctx = {
238+
const ctx: TestContext = {
233239
pages: {
234240
'/en/foo': true,
235241
// Note the lack of an enterprise-cloud page here

src/versions/lib/enterprise-server-releases.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ export const deprecatedReleasesOnDeveloperSite: string[]
4343
export const firstReleaseNote: string
4444
export const firstRestoredAdminGuides: string
4545

46-
export declare function findReleaseNumberIndex(releaseNum: number): number
47-
export declare function getNextReleaseNumber(releaseNum: number): string
48-
export declare function getPreviousReleaseNumber(releaseNum: number): string
46+
export declare function findReleaseNumberIndex(releaseNum: string): number
47+
export declare function getNextReleaseNumber(releaseNum: string): string
48+
export declare function getPreviousReleaseNumber(releaseNum: string): string
4949

5050
const allExports = {
5151
dates,

0 commit comments

Comments
 (0)