Skip to content

Commit 40abb4b

Browse files
committed
fix up tests with updated content
1 parent 22a6668 commit 40abb4b

File tree

1 file changed

+48
-74
lines changed

1 file changed

+48
-74
lines changed

scripts/generate-schema-docs.test.js

Lines changed: 48 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -323,92 +323,73 @@ describe('Schema Documentation Generator', () => {
323323
})
324324

325325
describe('createIndexFile', () => {
326-
it('creates index README with schema links and correct content', () => {
327-
const mockDocsDir = '/mock/docs/dir'
326+
it('creates index.md with schema sections and links', () => {
327+
const allSchemaFiles = [
328+
'form-definition-v2-schema.md',
329+
'form-definition-schema.md',
330+
'form-metadata-schema.md',
331+
'component-schema-v2.md',
332+
'component-schema.md',
333+
'list-schema-v2.md',
334+
'list-schema.md',
335+
'form-definition-schema-defs-condition-group-schema.md'
336+
]
328337

329-
path.join.mockReturnValue(`${mockDocsDir}/README.md`)
338+
fs.readdirSync.mockReturnValue(allSchemaFiles)
330339

331340
path.basename.mockImplementation(
332341
(/** @type {string} */ filePath, /** @type {string=} */ ext) => {
333-
filePath = filePath ?? ''
334-
ext = ext ?? ''
335-
336-
if (filePath === 'schema1.json') return 'schema1'
337-
if (filePath === 'schema2.json') return 'schema2'
338-
339-
const parts = filePath.split('/')
342+
const pathStr = String(filePath ?? '')
343+
const extStr = ext ? String(ext) : ''
344+
const parts = pathStr.split('/')
340345
const fileName = parts[parts.length - 1] || ''
341-
return fileName.replace(ext, '')
342-
}
343-
)
344-
345-
let capturedContent = ''
346-
fs.writeFileSync.mockImplementation(
347-
/**
348-
* @param {string} filePath
349-
* @param {string} content
350-
*/
351-
(filePath, content) => {
352-
if (filePath === `${mockDocsDir}/README.md`) {
353-
capturedContent = content
354-
}
346+
return extStr ? fileName.replace(extStr, '') : fileName
355347
}
356348
)
357349

358-
const schemaFiles = ['schema1.json', 'schema2.json']
350+
path.join.mockImplementation((...args) => args.join('/'))
359351

360-
fs.writeFileSync.mockImplementation((path, content) => {
361-
if (path.includes('README.md')) {
362-
capturedContent = `# Defra Forms Model Schema Reference\n\n* [schema1](schema1.md)\n* [schema2](schema2.md)`
363-
}
352+
let capturedContent = ''
353+
fs.writeFileSync.mockImplementation((filePath, content) => {
354+
capturedContent = content
364355
})
365356

366-
createIndexFile(schemaFiles)
357+
createIndexFile()
367358

368359
expect(fs.writeFileSync).toHaveBeenCalledWith(
369-
`${mockDocsDir}/README.md`,
360+
'/mock/docs/dir/index.md',
370361
expect.any(String)
371362
)
372363

373-
expect(capturedContent).toContain('# Defra Forms Model Schema Reference')
374-
expect(capturedContent).toContain('* [schema1](schema1.md)')
375-
expect(capturedContent).toContain('* [schema2](schema2.md)')
376-
})
377-
378-
it('categorizes schemas correctly into core and advanced', () => {
379-
path.basename.mockImplementation((filename) =>
380-
filename.replace('.json', '')
381-
)
382-
let capturedContent = ''
383-
fs.writeFileSync.mockImplementation((path, content) => {
384-
capturedContent = content
385-
})
386-
387-
const schemaFiles = [
388-
'component-schema-v2.json', // core
389-
'form-metadata-author-schema.json', // advanced
390-
'uncategorized-schema.json' // neither
391-
]
392-
393-
const consoleSpy = jest.spyOn(console, 'log').mockImplementation()
394-
395-
createIndexFile(schemaFiles)
396-
364+
expect(capturedContent).toContain('# Defra Forms Schema Reference')
397365
expect(capturedContent).toContain(
398-
'* [component-schema-v2](component-schema-v2.md)'
366+
'* [form-definition-v2-schema](form-definition-v2-schema.md)'
399367
)
400-
401368
expect(capturedContent).toContain(
402-
'* [form-metadata-author-schema](form-metadata-author-schema.md)'
369+
'* [form-metadata-schema](form-metadata-schema.md)'
403370
)
371+
})
404372

405-
expect(consoleSpy).toHaveBeenCalledWith(
406-
expect.stringContaining(
407-
"Schema 'uncategorized-schema' is not categorised"
408-
)
373+
it('throws an error if a referenced schema has no generated markdown file', () => {
374+
// Provide only a subset — the rest of INDEX_SECTIONS are missing
375+
fs.readdirSync.mockReturnValue([
376+
'form-definition-v2-schema.md',
377+
'form-metadata-schema.md'
378+
])
379+
380+
path.basename.mockImplementation(
381+
(/** @type {string} */ filePath, /** @type {string=} */ ext) => {
382+
const pathStr = String(filePath ?? '')
383+
const extStr = ext ? String(ext) : ''
384+
const parts = pathStr.split('/')
385+
const fileName = parts[parts.length - 1] || ''
386+
return extStr ? fileName.replace(extStr, '') : fileName
387+
}
409388
)
410389

411-
consoleSpy.mockRestore()
390+
expect(() => createIndexFile()).toThrow(
391+
'Schema index references schemas that no longer exist'
392+
)
412393
})
413394
})
414395

@@ -864,17 +845,9 @@ describe('Schema Documentation Generator', () => {
864845
expect(fs.writeFileSync).toHaveBeenCalledTimes(2)
865846

866847
fs.writeFileSync.mock.calls.forEach((call) => {
867-
const path = String(call[0])
868848
const content = call[1]
869-
870-
if (path.includes('test-schema.md')) {
871-
expect(content).toContain('title: Test Schema')
872-
} else if (path.includes('another-schema.md')) {
873-
expect(content).toContain('title: Another Schema')
874-
}
875-
876-
expect(content).toMatch(/^---\nlayout: default/)
877-
expect(content).toContain('parent: Schema Reference')
849+
// Source writes back original content unchanged (no front matter added)
850+
expect(content).toBe('# Content without frontmatter')
878851
})
879852
})
880853

@@ -891,8 +864,9 @@ describe('Schema Documentation Generator', () => {
891864

892865
expect(fs.writeFileSync).toHaveBeenCalledTimes(1)
893866

867+
// Source writes back the original content unchanged (no front matter added)
894868
const content = fs.writeFileSync.mock.calls[0][1]
895-
expect(content).toContain('title: Complex File Name With Multiple Parts')
869+
expect(content).toBe('# Complex content')
896870
})
897871

898872
it('skips files that already have frontmatter', () => {

0 commit comments

Comments
 (0)