Skip to content

Commit 54de4df

Browse files
lint
1 parent e5c9b90 commit 54de4df

File tree

4 files changed

+122
-62
lines changed

4 files changed

+122
-62
lines changed

src/__tests__/helpers/apiTestHelpers.ts

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ export const mockContentCollections = {
88
name: 'react-component-docs',
99
base: '/mock/path/react',
1010
pattern: '**/*.md',
11-
version: 'v6'
11+
version: 'v6',
1212
},
1313
{
1414
name: 'core-docs',
1515
base: '/mock/path/core',
1616
pattern: '**/*.md',
17-
version: 'v6'
18-
}
19-
]
20-
};
17+
version: 'v6',
18+
},
19+
],
20+
}
2121

2222
export const mockEntriesWithBody = {
2323
'react-component-docs': [
@@ -29,9 +29,9 @@ export const mockEntriesWithBody = {
2929
id: 'Alert',
3030
title: 'Alert',
3131
section: 'components',
32-
tab: 'react'
32+
tab: 'react',
3333
},
34-
collection: 'react-component-docs'
34+
collection: 'react-component-docs',
3535
},
3636
{
3737
id: 'components/alert/html',
@@ -41,9 +41,9 @@ export const mockEntriesWithBody = {
4141
id: 'Alert',
4242
title: 'Alert',
4343
section: 'components',
44-
tab: 'html'
44+
tab: 'html',
4545
},
46-
collection: 'react-component-docs'
46+
collection: 'react-component-docs',
4747
},
4848
{
4949
id: 'components/alert/react-demos',
@@ -53,19 +53,18 @@ export const mockEntriesWithBody = {
5353
id: 'Alert',
5454
title: 'Alert Demos',
5555
section: 'components',
56-
tab: 'react-demos'
56+
tab: 'react-demos',
5757
},
58-
collection: 'react-component-docs'
59-
}
58+
collection: 'react-component-docs',
59+
},
6060
],
61-
'core-docs': []
62-
};
61+
'core-docs': [],
62+
}
6363

64-
export const createGetCollectionMock = (collections: Record<string, any[]>) => {
65-
return jest.fn((collectionName: string) => {
66-
return Promise.resolve(collections[collectionName] || []);
67-
});
68-
};
64+
export const createGetCollectionMock = (collections: Record<string, any[]>) =>
65+
jest.fn((collectionName: string) =>
66+
Promise.resolve(collections[collectionName] || []),
67+
)
6968

7069
export const mockUtils = {
7170
isReactTab: jest.fn((tab: string) => tab.includes('react')),
@@ -86,17 +85,25 @@ export const mockUtils = {
8685
return result
8786
}),
8887
kebabCase: jest.fn((id: string) => {
89-
if (!id) return ''
88+
if (!id) {
89+
return ''
90+
}
9091
return id
9192
.replace(/PatternFly/g, 'Patternfly')
9293
.replace(/([a-z])([A-Z])/g, '$1-$2')
9394
.replace(/[\s_]+/g, '-')
9495
.toLowerCase()
9596
}),
9697
getDefaultTab: jest.fn((filePath?: string) => {
97-
if (!filePath) return 'react'
98-
if (filePath.includes('react')) return 'react'
99-
if (filePath.includes('html')) return 'html'
98+
if (!filePath) {
99+
return 'react'
100+
}
101+
if (filePath.includes('react')) {
102+
return 'react'
103+
}
104+
if (filePath.includes('html')) {
105+
return 'html'
106+
}
100107
return 'react'
101-
})
102-
};
108+
}),
109+
}

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

Lines changed: 59 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,19 @@ import { GET } from '../../../../../../../pages/api/[version]/[section]/[page]/[
66
*/
77
jest.mock('../../../../../../../content', () => ({
88
content: [
9-
{ name: 'react-component-docs', base: '/mock/path/react', pattern: '**/*.md', version: 'v6' },
10-
{ name: 'core-docs', base: '/mock/path/core', pattern: '**/*.md', version: 'v6' }
11-
]
9+
{
10+
name: 'react-component-docs',
11+
base: '/mock/path/react',
12+
pattern: '**/*.md',
13+
version: 'v6',
14+
},
15+
{
16+
name: 'core-docs',
17+
base: '/mock/path/core',
18+
pattern: '**/*.md',
19+
version: 'v6',
20+
},
21+
],
1222
}))
1323

1424
/**
@@ -23,55 +33,84 @@ jest.mock('astro:content', () => ({
2333
id: 'components/alert/react',
2434
slug: 'components/alert/react',
2535
body: '# Alert Component\n\nReact Alert documentation content',
26-
data: { id: 'Alert', title: 'Alert', section: 'components', tab: 'react' },
27-
collection: 'react-component-docs'
36+
data: {
37+
id: 'Alert',
38+
title: 'Alert',
39+
section: 'components',
40+
tab: 'react',
41+
},
42+
collection: 'react-component-docs',
2843
},
2944
{
3045
id: 'components/alert/html',
3146
slug: 'components/alert/html',
3247
body: '# Alert HTML\n\nHTML Alert documentation content',
33-
data: { id: 'Alert', title: 'Alert', section: 'components', tab: 'html' },
34-
collection: 'react-component-docs'
48+
data: {
49+
id: 'Alert',
50+
title: 'Alert',
51+
section: 'components',
52+
tab: 'html',
53+
},
54+
collection: 'react-component-docs',
3555
},
3656
{
3757
id: 'components/alert/react-demos',
3858
slug: 'components/alert/react-demos',
3959
body: '# Alert Demos\n\nReact demos content',
40-
data: { id: 'Alert', title: 'Alert Demos', section: 'components', tab: 'react-demos' },
41-
collection: 'react-component-docs'
42-
}
60+
data: {
61+
id: 'Alert',
62+
title: 'Alert Demos',
63+
section: 'components',
64+
tab: 'react-demos',
65+
},
66+
collection: 'react-component-docs',
67+
},
4368
],
44-
'core-docs': []
69+
'core-docs': [],
4570
}
4671
return Promise.resolve(mockData[collectionName] || [])
47-
})
72+
}),
4873
}))
4974

5075
/**
5176
* Mock utilities for tab identification and transformation
5277
*/
5378
jest.mock('../../../../../../../utils', () => ({
5479
kebabCase: jest.fn((id: string) => {
55-
if (!id) return ''
80+
if (!id) {
81+
return ''
82+
}
5683
return id
5784
.replace(/PatternFly/g, 'Patternfly')
5885
.replace(/([a-z])([A-Z])/g, '$1-$2')
5986
.replace(/[\s_]+/g, '-')
6087
.toLowerCase()
6188
}),
6289
getDefaultTab: jest.fn((filePath?: string) => {
63-
if (!filePath) return 'react'
64-
if (filePath.includes('react')) return 'react'
65-
if (filePath.includes('html')) return 'html'
90+
if (!filePath) {
91+
return 'react'
92+
}
93+
if (filePath.includes('react')) {
94+
return 'react'
95+
}
96+
if (filePath.includes('html')) {
97+
return 'html'
98+
}
6699
return 'react'
67100
}),
68101
addDemosOrDeprecated: jest.fn((tabName: string, filePath?: string) => {
69-
if (!filePath || !tabName) return ''
102+
if (!filePath || !tabName) {
103+
return ''
104+
}
70105
let result = tabName
71-
if (filePath.includes('demos') && !tabName.includes('-demos')) result += '-demos'
72-
if (filePath.includes('deprecated') && !tabName.includes('-deprecated')) result += '-deprecated'
106+
if (filePath.includes('demos') && !tabName.includes('-demos')) {
107+
result += '-demos'
108+
}
109+
if (filePath.includes('deprecated') && !tabName.includes('-deprecated')) {
110+
result += '-deprecated'
111+
}
73112
return result
74-
})
113+
}),
75114
}))
76115

77116
/**

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

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1+
/* eslint-disable no-console */
12
import type { APIRoute, GetStaticPaths } from 'astro'
23
import type { CollectionEntry, CollectionKey } from 'astro:content'
34
import { getCollection } from 'astro:content'
45
import { content } from '../../../../../content'
5-
import { kebabCase, getDefaultTab, addDemosOrDeprecated } from '../../../../../utils'
6+
import {
7+
kebabCase,
8+
getDefaultTab,
9+
addDemosOrDeprecated,
10+
} from '../../../../../utils'
611
import { generateAndWriteApiIndex } from '../../../../../utils/apiIndex/generate'
712
import { getApiIndex } from '../../../../../utils/apiIndex/get'
8-
import { createJsonResponse, createTextResponse, createIndexKey } from '../../../../../utils/apiHelpers'
13+
import {
14+
createJsonResponse,
15+
createTextResponse,
16+
createIndexKey,
17+
} from '../../../../../utils/apiHelpers'
918

1019
export const prerender = true
1120

@@ -18,7 +27,9 @@ export const getStaticPaths: GetStaticPaths = async () => {
1827
// This runs once during build when getCollection() is available
1928
const index = await generateAndWriteApiIndex()
2029

21-
const paths: { params: { version: string; section: string; page: string; tab: string } }[] = []
30+
const paths: {
31+
params: { version: string; section: string; page: string; tab: string }
32+
}[] = []
2233

2334
// Build paths from index structure
2435
for (const version of index.versions) {
@@ -67,7 +78,9 @@ export const GET: APIRoute = async ({ params }) => {
6778
const pageKey = createIndexKey(version, section, page)
6879
if (!index.pages[sectionKey]?.includes(page)) {
6980
return createJsonResponse(
70-
{ error: `Page '${page}' not found in section '${section}' for version '${version}'` },
81+
{
82+
error: `Page '${page}' not found in section '${section}' for version '${version}'`,
83+
},
7184
404,
7285
)
7386
}
@@ -91,16 +104,14 @@ export const GET: APIRoute = async ({ params }) => {
91104
collectionsToFetch.map((name) => getCollection(name)),
92105
)
93106

94-
const flatEntries = collections
95-
.flat()
96-
.map(({ data, filePath, ...rest }) => ({
97-
filePath,
98-
...rest,
99-
data: {
100-
...data,
101-
tab: data.tab || data.source || getDefaultTab(filePath),
102-
},
103-
}))
107+
const flatEntries = collections.flat().map(({ data, filePath, ...rest }) => ({
108+
filePath,
109+
...rest,
110+
data: {
111+
...data,
112+
tab: data.tab || data.source || getDefaultTab(filePath),
113+
},
114+
}))
104115

105116
// Find the matching entry
106117
const matchingEntry = flatEntries.find((entry: ContentEntry) => {
@@ -117,7 +128,7 @@ export const GET: APIRoute = async ({ params }) => {
117128
// Log warning - indicates index/content mismatch
118129
console.warn(
119130
`[API Warning] Index exists but content not found: ${version}/${section}/${page}/${tab}. ` +
120-
'This may indicate a mismatch between index generation and actual content.'
131+
'This may indicate a mismatch between index generation and actual content.',
121132
)
122133
return createJsonResponse(
123134
{

src/utils/apiIndex/generate.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ export async function generateApiIndex(): Promise<ApiIndex> {
8787
const pageTabs: Record<string, Set<string>> = {}
8888

8989
flatEntries.forEach((entry: any) => {
90-
if (!entry.data.section) return
90+
if (!entry.data.section) {
91+
return
92+
}
9193

9294
const section = entry.data.section
9395
const page = kebabCase(entry.data.id)
@@ -104,7 +106,8 @@ export async function generateApiIndex(): Promise<ApiIndex> {
104106
sectionPages[sectionKey].add(page)
105107

106108
// Collect tab
107-
const entryTab = entry.data.tab || entry.data.source || getDefaultTab(entry.filePath)
109+
const entryTab =
110+
entry.data.tab || entry.data.source || getDefaultTab(entry.filePath)
108111
const tab = addDemosOrDeprecated(entryTab, entry.id)
109112
if (!pageTabs[pageKey]) {
110113
pageTabs[pageKey] = new Set()

0 commit comments

Comments
 (0)