Skip to content

Commit

Permalink
Test that categories all have the same child type (github#25392)
Browse files Browse the repository at this point in the history
* Track category child types

* Test that category children are all of the same type

* Add custom error message

Co-authored-by: Grace Park <gracepark@github.com>

Co-authored-by: Grace Park <gracepark@github.com>
  • Loading branch information
rsese and gracepark committed Feb 16, 2022
1 parent 278f0bb commit ef9b6eb
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions tests/content/category-pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ describe('category pages', () => {
describe.each(categoryTuples)(
'category index "%s"',
(indexRelPath, indexAbsPath, indexLink) => {
let publishedArticlePaths, availableArticlePaths, indexTitle, categoryVersions
let publishedArticlePaths,
availableArticlePaths,
indexTitle,
categoryVersions,
categoryChildTypes
const articleVersions = {}

beforeAll(async () => {
Expand All @@ -76,9 +80,24 @@ describe('category pages', () => {
const indexContents = await readFileAsync(indexAbsPath, 'utf8')
const { data } = matter(indexContents)
categoryVersions = getApplicableVersions(data.versions, indexAbsPath)
categoryChildTypes = []
const articleLinks = data.children.filter((child) => {
const mdPath = getPath(productDir, indexLink, child)
return fs.existsSync(mdPath) && fs.statSync(mdPath).isFile()

const fileExists = fs.existsSync(mdPath)

// We're checking each item in the category's 'children' frontmatter
// to see if the child is an article by tacking on `.md` to it. If
// that file exists it's an article, otherwise it's a map topic. A
// category needs to have all the same type of children so we track
// that here so we can test to make sure all the types are the same.
if (fileExists) {
categoryChildTypes.push('article')
} else {
categoryChildTypes.push('mapTopic')
}

return fileExists && fs.statSync(mdPath).isFile()
})

// Save the index title for later testing
Expand Down Expand Up @@ -152,6 +171,19 @@ describe('category pages', () => {
})
})

test('categories contain all the same type of children', () => {
let errorType = ''
expect(
categoryChildTypes.every((categoryChildType) => {
errorType = categoryChildType
return categoryChildType === categoryChildTypes[0]
}),
`${indexRelPath.replace('index.md', '')} contains a mix of ${errorType}s and ${
categoryChildTypes[0]
}s, category children must be of the same type`
).toBe(true)
})

// TODO: Unskip this test once the related script has been executed
// Docs Engineering issue: 963
test.skip('slugified title matches parent directory name', () => {
Expand Down

0 comments on commit ef9b6eb

Please sign in to comment.