Skip to content

Commit

Permalink
Writing some new docs tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jonrohan committed Mar 30, 2021
1 parent a4a52c6 commit ca0c85f
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 21 deletions.
7 changes: 0 additions & 7 deletions __tests__/css.test.js

This file was deleted.

27 changes: 21 additions & 6 deletions __tests__/docs.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
const fs = require('fs')
const yaml = require('js-yaml')
const {getNavigationLinks, getContentFrontmatter} = require('./utils/docs')

let nav
let navLinks, contentFM

beforeAll(() => {
nav = yaml.load(fs.readFileSync('./docs/src/@primer/gatsby-theme-doctocat/nav.yml', 'utf8'))
beforeAll(async () => {
contentFM = await getContentFrontmatter()
navLinks = getNavigationLinks()
})

describe('frontmatter', () => {
it('page title matches link title', () => {
navLinks.forEach(link => {
const content = contentFM[link['url']]
expect(content).not.toBeNull()
expect(content['title']).toBe(link['title'])
})
})

it('contains path attribute', () => {
Object.values(contentFM).forEach( v => {
expect(v['path']).not.toBeNull()
})
})
})

describe('navigation', () => {
it('has a file for each nav item', () => {

navLinks.forEach(link => {
const content = contentFM[link['url']]
expect(content).not.toBeNull()
})
})
})
43 changes: 43 additions & 0 deletions __tests__/utils/docs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const fs = require('fs')
const frontMatter = require('front-matter')
const yaml = require('js-yaml')
const globby = require('globby')
const {join} = require('path')

const docsPath = join(__dirname, '../../docs')

function collectNavLinks(links) {
let foundLinks = []
links.forEach(link => {
foundLinks.push({
title: link['title'],
url: link['url']
})
if(link['children']) {
foundLinks = foundLinks.concat(collectNavLinks(link['children']))
}
})
return foundLinks
}

function getNavigationLinks() {
const nav = yaml.load(fs.readFileSync(join(docsPath, './src/@primer/gatsby-theme-doctocat/nav.yml'), 'utf8'))
return collectNavLinks(nav)
}

async function getContentFrontmatter() {
let fm = {}

const paths = await globby(join(docsPath, './content/**/*.md*'))
paths.forEach(path => {
const contents = fs.readFileSync(path, 'utf8')
const fmContents = frontMatter(contents)
fm[path.replace(join(docsPath, './content'), '').replace(/(\/index)?\.mdx?/,'')] = fmContents['attributes']
})
return fm
}

module.exports = {
getContentFrontmatter,
getNavigationLinks
}
2 changes: 1 addition & 1 deletion docs/content/components/marketing-buttons.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Marketing Buttons
title: Marketing buttons
path: components/marketing-buttons
status: New
source: 'https://github.com/primer/css/tree/main/src/marketing/buttons'
Expand Down
2 changes: 1 addition & 1 deletion docs/content/objects/table-object.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Table object (deprecated)
title: Table object
path: objects/table-object
status: Deprecated
source: 'https://github.com/primer/css/tree/main/src/table-object'
Expand Down
2 changes: 1 addition & 1 deletion docs/content/utilities/marketing-filters.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Marketing Filters
title: Marketing filters
path: utilities/marketing-filters
status: Stable
status_issue: 'https://github.com/github/design-systems/issues/302'
Expand Down
2 changes: 1 addition & 1 deletion docs/content/utilities/marketing-layout.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Marketing Layout
title: Marketing layout
sort_title: Layout Marketing
path: utilities/marketing-layout
status: Stable
Expand Down
2 changes: 1 addition & 1 deletion docs/content/utilities/marketing-margin.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Marketing Margin
title: Marketing margin
sort_title: Margin Marketing
path: utilities/marketing-margin
status: Stable
Expand Down
2 changes: 1 addition & 1 deletion docs/content/utilities/marketing-padding.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Marketing Padding
title: Marketing padding
sort_title: Padding Marketing
path: utilities/marketing-padding
status: Stable
Expand Down
2 changes: 1 addition & 1 deletion docs/content/utilities/marketing-type.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Marketing Typography
title: Marketing typography
path: utilities/marketing-type
status: New
source: 'https://github.com/primer/css/tree/main/src/marketing/type'
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"eslint-plugin-jsx-a11y": "^6.4.1",
"eslint-plugin-react": "^7.23.1",
"filesize": "4.1.2",
"front-matter": "4.0.2",
"fs-extra": "^4.0.2",
"gh-pages": "^1.0.0",
"globby": "^6.1.0",
Expand Down Expand Up @@ -84,6 +85,10 @@
"unist-util-stringify-position": "^2.0.1"
},
"jest": {
"testEnvironment": "node"
"testEnvironment": "node",
"testPathIgnorePatterns": [
"/node_modules/",
"/__tests__/utils/"
]
}
}
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6815,6 +6815,13 @@ from2@^2.1.0:
inherits "^2.0.1"
readable-stream "^2.0.0"

front-matter@4.0.2:
version "4.0.2"
resolved "https://registry.yarnpkg.com/front-matter/-/front-matter-4.0.2.tgz#b14e54dc745cfd7293484f3210d15ea4edd7f4d5"
integrity sha512-I8ZuJ/qG92NWX8i5x1Y8qyj3vizhXS31OxjKDu3LKP+7/qBgfIKValiZIEwoVoJKUHlhWtYrktkxV1XsX+pPlg==
dependencies:
js-yaml "^3.13.1"

fs-extra@^0.30.0:
version "0.30.0"
resolved "https://registry.npmjs.org/fs-extra/-/fs-extra-0.30.0.tgz"
Expand Down

0 comments on commit ca0c85f

Please sign in to comment.