|
| 1 | +import { GET } from '../../../../../../../../../pages/api/[version]/[section]/[page]/[tab]/examples/[example]' |
| 2 | +import { access, readFile } from 'fs/promises' |
| 3 | + |
| 4 | +jest.mock('fs/promises') |
| 5 | +const mockReadFile = readFile as jest.MockedFunction<typeof readFile> |
| 6 | +const mockAccess = access as jest.MockedFunction<typeof access> |
| 7 | + |
| 8 | +jest.mock('../../../../../../../../../content', () => ({ |
| 9 | + content: [ |
| 10 | + { |
| 11 | + name: 'react-component-docs', |
| 12 | + base: '/mock/monorepo/packages/react-core', |
| 13 | + pattern: '**/*.md', |
| 14 | + version: 'v6', |
| 15 | + }, |
| 16 | + ], |
| 17 | +})) |
| 18 | + |
| 19 | +jest.mock('astro:content', () => ({ |
| 20 | + getCollection: jest.fn((collectionName: string) => { |
| 21 | + const mockData: Record<string, any[]> = { |
| 22 | + 'react-component-docs': [ |
| 23 | + { |
| 24 | + id: 'components/alert/react', |
| 25 | + slug: 'components/alert/react', |
| 26 | + body: '', |
| 27 | + filePath: 'patternfly-docs/components/Alert/examples/Alert.md', |
| 28 | + data: { |
| 29 | + id: 'Alert', |
| 30 | + title: 'Alert', |
| 31 | + section: 'components', |
| 32 | + tab: 'react', |
| 33 | + }, |
| 34 | + collection: 'react-component-docs', |
| 35 | + }, |
| 36 | + ], |
| 37 | + } |
| 38 | + return Promise.resolve(mockData[collectionName] || []) |
| 39 | + }), |
| 40 | +})) |
| 41 | + |
| 42 | +jest.mock('../../../../../../../../../utils', () => ({ |
| 43 | + kebabCase: jest.fn((id: string) => { |
| 44 | + if (!id) { return '' } |
| 45 | + return id |
| 46 | + .replace(/([a-z])([A-Z])/g, '$1-$2') |
| 47 | + .replace(/[\s_]+/g, '-') |
| 48 | + .toLowerCase() |
| 49 | + }), |
| 50 | + getDefaultTabForApi: jest.fn((filePath?: string) => { |
| 51 | + if (!filePath) { return 'react' } |
| 52 | + if (filePath.includes('react')) { return 'react' } |
| 53 | + return 'react' |
| 54 | + }), |
| 55 | + addDemosOrDeprecated: jest.fn((tabName: string, filePath?: string) => { |
| 56 | + if (!filePath || !tabName) { return '' } |
| 57 | + return tabName |
| 58 | + }), |
| 59 | + addSubsection: jest.fn((page: string, subsection?: string) => { |
| 60 | + if (!subsection) { return page } |
| 61 | + return `${subsection.toLowerCase()}_${page}` |
| 62 | + }), |
| 63 | +})) |
| 64 | + |
| 65 | +jest.mock('../../../../../../../../../utils/apiIndex/generate', () => ({ |
| 66 | + generateAndWriteApiIndex: jest.fn().mockResolvedValue({ |
| 67 | + versions: ['v6'], |
| 68 | + sections: { v6: ['components'] }, |
| 69 | + pages: { 'v6::components': ['alert'] }, |
| 70 | + tabs: { 'v6::components::alert': ['react'] }, |
| 71 | + examples: { |
| 72 | + 'v6::components::alert::react': [ |
| 73 | + { exampleName: 'AlertBasic' }, |
| 74 | + ], |
| 75 | + }, |
| 76 | + }), |
| 77 | +})) |
| 78 | + |
| 79 | +beforeEach(() => { |
| 80 | + jest.clearAllMocks() |
| 81 | +}) |
| 82 | + |
| 83 | +const mdxContent = ` |
| 84 | +import AlertBasic from './AlertBasic.tsx?raw' |
| 85 | +import AlertCustomIcon from './AlertCustomIcon.tsx?raw' |
| 86 | +` |
| 87 | + |
| 88 | +it('resolves example files relative to base in monorepo setups', async () => { |
| 89 | + // Simulate monorepo: raw filePath doesn't exist at CWD, so access rejects |
| 90 | + mockAccess.mockRejectedValueOnce(new Error('ENOENT')) |
| 91 | + |
| 92 | + // First call reads the content entry file, second reads the example file |
| 93 | + mockReadFile |
| 94 | + .mockResolvedValueOnce(mdxContent) |
| 95 | + .mockResolvedValueOnce('const AlertBasic = () => <Alert />') |
| 96 | + |
| 97 | + const response = await GET({ |
| 98 | + params: { |
| 99 | + version: 'v6', |
| 100 | + section: 'components', |
| 101 | + page: 'alert', |
| 102 | + tab: 'react', |
| 103 | + example: 'AlertBasic', |
| 104 | + }, |
| 105 | + } as any) |
| 106 | + |
| 107 | + expect(response.status).toBe(200) |
| 108 | + const text = await response.text() |
| 109 | + expect(text).toBe('const AlertBasic = () => <Alert />') |
| 110 | + |
| 111 | + // Content entry file should be resolved with base |
| 112 | + expect(mockReadFile).toHaveBeenCalledWith( |
| 113 | + '/mock/monorepo/packages/react-core/patternfly-docs/components/Alert/examples/Alert.md', |
| 114 | + 'utf8' |
| 115 | + ) |
| 116 | + |
| 117 | + // Example file should be resolved with base + content entry dir |
| 118 | + expect(mockReadFile).toHaveBeenCalledWith( |
| 119 | + '/mock/monorepo/packages/react-core/patternfly-docs/components/Alert/examples/AlertBasic.tsx', |
| 120 | + 'utf8' |
| 121 | + ) |
| 122 | +}) |
| 123 | + |
| 124 | +it('returns 404 when example is not found in imports', async () => { |
| 125 | + mockAccess.mockRejectedValueOnce(new Error('ENOENT')) |
| 126 | + mockReadFile.mockResolvedValueOnce(mdxContent) |
| 127 | + |
| 128 | + const response = await GET({ |
| 129 | + params: { |
| 130 | + version: 'v6', |
| 131 | + section: 'components', |
| 132 | + page: 'alert', |
| 133 | + tab: 'react', |
| 134 | + example: 'NonExistent', |
| 135 | + }, |
| 136 | + } as any) |
| 137 | + |
| 138 | + expect(response.status).toBe(404) |
| 139 | + const body = await response.json() |
| 140 | + expect(body.error).toContain('NonExistent') |
| 141 | +}) |
| 142 | + |
| 143 | +it('returns 404 when example file does not exist on disk', async () => { |
| 144 | + mockAccess.mockRejectedValueOnce(new Error('ENOENT')) |
| 145 | + |
| 146 | + const enoentError = new Error('ENOENT: no such file or directory') as NodeJS.ErrnoException |
| 147 | + enoentError.code = 'ENOENT' |
| 148 | + |
| 149 | + mockReadFile |
| 150 | + .mockResolvedValueOnce(mdxContent as any) |
| 151 | + .mockRejectedValueOnce(enoentError) |
| 152 | + |
| 153 | + const response = await GET({ |
| 154 | + params: { |
| 155 | + version: 'v6', |
| 156 | + section: 'components', |
| 157 | + page: 'alert', |
| 158 | + tab: 'react', |
| 159 | + example: 'AlertBasic', |
| 160 | + }, |
| 161 | + } as any) |
| 162 | + |
| 163 | + const body = await response.json() |
| 164 | + expect(response.status).toBe(404) |
| 165 | + expect(body.error).toContain('Example file not found') |
| 166 | +}) |
| 167 | + |
| 168 | +it('returns 400 when required parameters are missing', async () => { |
| 169 | + const response = await GET({ |
| 170 | + params: { |
| 171 | + version: 'v6', |
| 172 | + section: 'components', |
| 173 | + page: 'alert', |
| 174 | + tab: 'react', |
| 175 | + }, |
| 176 | + } as any) |
| 177 | + |
| 178 | + expect(response.status).toBe(400) |
| 179 | + const body = await response.json() |
| 180 | + expect(body.error).toContain('required') |
| 181 | +}) |
| 182 | + |
| 183 | +it('returns 404 when content entry is not found', async () => { |
| 184 | + const response = await GET({ |
| 185 | + params: { |
| 186 | + version: 'v6', |
| 187 | + section: 'components', |
| 188 | + page: 'nonexistent', |
| 189 | + tab: 'react', |
| 190 | + example: 'AlertBasic', |
| 191 | + }, |
| 192 | + } as any) |
| 193 | + |
| 194 | + const body = await response.json() |
| 195 | + expect(response.status).toBe(404) |
| 196 | + expect(body.error).toContain('Content entry not found') |
| 197 | +}) |
0 commit comments