Skip to content

Commit e5c9b90

Browse files
move tests outside of pages dir so that astro doesn't try to render them
1 parent 0fdbcfd commit e5c9b90

File tree

5 files changed

+67
-25
lines changed

5 files changed

+67
-25
lines changed

jest.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const config: Config = {
1313
'^.+\\.m?jsx?$': 'babel-jest',
1414
},
1515
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.[jt]sx?$',
16+
testPathIgnorePatterns: ['/node_modules/', '/__tests__/helpers/'],
1617
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
1718
moduleNameMapper: {
1819
'\\.(css|less)$': '<rootDir>/src/__mocks__/styleMock.ts',

src/pages/api/__tests__/[version].test.ts renamed to src/__tests__/pages/api/__tests__/[version].test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { GET } from '../[version]'
1+
import { GET } from '../../../../pages/api/[version]'
22

33
/**
44
* Mock apiIndex.json with multiple versions (v5, v6)
55
* to test section retrieval for different versions
66
*/
7-
jest.mock('../../../apiIndex.json', () => ({
7+
jest.mock('../../../../apiIndex.json', () => ({
88
versions: ['v5', 'v6'],
99
sections: {
1010
v5: ['getting-started'],

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

Lines changed: 62 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,83 @@
1-
import { GET } from '../../../../[version]/[section]/[page]/[tab]'
1+
import { GET } from '../../../../../../../pages/api/[version]/[section]/[page]/[tab]'
22

33
/**
44
* Mock content collections with entries that have body content
55
* to test markdown/MDX content retrieval
66
*/
7-
jest.mock('../../../../../../content', () => {
8-
const { mockContentCollections } = jest.requireActual('../../../../testHelpers.ts')
9-
return { content: mockContentCollections.v6 }
10-
})
7+
jest.mock('../../../../../../../content', () => ({
8+
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+
]
12+
}))
1113

1214
/**
1315
* Mock getCollection to return entries with body (markdown content)
1416
* simulating real documentation pages with content
1517
*/
16-
jest.mock('astro:content', () => {
17-
const { mockEntriesWithBody, createGetCollectionMock } = jest.requireActual(
18-
'../../../../testHelpers.ts',
19-
)
20-
return {
21-
getCollection: createGetCollectionMock({
22-
'react-component-docs': mockEntriesWithBody['react-component-docs'],
23-
'core-docs': mockEntriesWithBody['core-docs'],
24-
}),
25-
}
26-
})
18+
jest.mock('astro:content', () => ({
19+
getCollection: jest.fn((collectionName: string) => {
20+
const mockData: Record<string, any[]> = {
21+
'react-component-docs': [
22+
{
23+
id: 'components/alert/react',
24+
slug: 'components/alert/react',
25+
body: '# Alert Component\n\nReact Alert documentation content',
26+
data: { id: 'Alert', title: 'Alert', section: 'components', tab: 'react' },
27+
collection: 'react-component-docs'
28+
},
29+
{
30+
id: 'components/alert/html',
31+
slug: 'components/alert/html',
32+
body: '# Alert HTML\n\nHTML Alert documentation content',
33+
data: { id: 'Alert', title: 'Alert', section: 'components', tab: 'html' },
34+
collection: 'react-component-docs'
35+
},
36+
{
37+
id: 'components/alert/react-demos',
38+
slug: 'components/alert/react-demos',
39+
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+
}
43+
],
44+
'core-docs': []
45+
}
46+
return Promise.resolve(mockData[collectionName] || [])
47+
})
48+
}))
2749

2850
/**
2951
* Mock utilities for tab identification and transformation
3052
*/
31-
jest.mock('../../../../../../utils', () => {
32-
const { mockUtils } = jest.requireActual('../../../../testHelpers.ts')
33-
return mockUtils
34-
})
53+
jest.mock('../../../../../../../utils', () => ({
54+
kebabCase: jest.fn((id: string) => {
55+
if (!id) return ''
56+
return id
57+
.replace(/PatternFly/g, 'Patternfly')
58+
.replace(/([a-z])([A-Z])/g, '$1-$2')
59+
.replace(/[\s_]+/g, '-')
60+
.toLowerCase()
61+
}),
62+
getDefaultTab: jest.fn((filePath?: string) => {
63+
if (!filePath) return 'react'
64+
if (filePath.includes('react')) return 'react'
65+
if (filePath.includes('html')) return 'html'
66+
return 'react'
67+
}),
68+
addDemosOrDeprecated: jest.fn((tabName: string, filePath?: string) => {
69+
if (!filePath || !tabName) return ''
70+
let result = tabName
71+
if (filePath.includes('demos') && !tabName.includes('-demos')) result += '-demos'
72+
if (filePath.includes('deprecated') && !tabName.includes('-deprecated')) result += '-deprecated'
73+
return result
74+
})
75+
}))
3576

3677
/**
3778
* Mock API index to validate paths
3879
*/
39-
jest.mock('../../../../../../utils/apiIndex/get', () => ({
80+
jest.mock('../../../../../../../utils/apiIndex/get', () => ({
4081
getApiIndex: jest.fn().mockResolvedValue({
4182
versions: ['v6'],
4283
sections: {

src/pages/api/__tests__/versions.test.ts renamed to src/__tests__/pages/api/__tests__/versions.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { GET } from '../versions'
1+
import { GET } from '../../../../pages/api/versions'
22

33
/**
44
* Mock apiIndex.json with multiple versions
55
*/
6-
jest.mock('../../../apiIndex.json', () => ({
6+
jest.mock('../../../../apiIndex.json', () => ({
77
versions: ['v5', 'v6'],
88
sections: {},
99
pages: {},

0 commit comments

Comments
 (0)