1
1
mirror of https://github.com/primer/css.git synced 2024-12-21 05:01:45 +03:00
css/tests/modules/test-document-styles.js

61 lines
1.7 KiB
JavaScript
Raw Normal View History

2017-07-24 23:52:56 +03:00
const test = require("ava")
const css = require(process.env.PWD)
const fs = require("fs")
const glob = require("glob")
2017-07-24 23:52:56 +03:00
var selectors, classnames = null
const classRegex = /class="([^"]+)"/ig
2017-07-24 23:52:56 +03:00
2017-07-26 01:25:30 +03:00
// Find unique selectors from the cssstats selector list
2017-07-24 23:52:56 +03:00
function uniqueSelectors(s) {
2017-07-26 01:25:30 +03:00
s = s.map(s => {
2017-07-24 23:52:56 +03:00
// split multi-selectors into last class used .foo .bar .baz
return s.split(" ").pop()
2017-07-26 01:25:30 +03:00
}).filter(s => {
// remove any selector that aren't just regular classnames eg. ::hover [type]
return s.match(/^\.[a-z\-_]+$/ig)
2017-07-24 23:52:56 +03:00
})
2017-07-26 01:25:30 +03:00
// return only the unique selectors
return [...new Set(s)]
2017-07-24 23:52:56 +03:00
}
2017-07-26 01:25:30 +03:00
// From the given glob sources array, read the files and return found classnames
2017-07-24 23:52:56 +03:00
function documentedClassnames(sources) {
var cn = []
sources.forEach( f => {
glob.sync(f).forEach( g => {
var match = null
2017-07-26 01:25:30 +03:00
// While we match a classRegex in the source
2017-07-26 21:30:00 +03:00
while ((match = classRegex.exec(fs.readFileSync(g, "utf8"))) != null) {
2017-07-26 01:25:30 +03:00
// Get the matched classnames "..." and split by space into classes
cn = cn.concat(match[1].split(" "))
}
})
})
2017-07-26 01:25:30 +03:00
// return only the unique classnames
return [...new Set(cn)]
2017-07-24 23:52:56 +03:00
}
2017-07-26 01:25:30 +03:00
// Before all the tests get the selectors and classnames
2017-07-26 21:26:41 +03:00
test.before(t => {
selectors = uniqueSelectors(css.cssstats.selectors.values)
classnames = documentedClassnames([
2017-07-24 23:52:56 +03:00
'docs/*.md',
'README.md'
])
})
test("Every selector class is documented", t => {
var undocumented = []
selectors.forEach( selector => {
if (!classnames.includes(selector.replace(".", ""))) {
undocumented.push(selector)
}
})
t.is(undocumented.length, 0, `I did not find documentation for the "${undocumented.join(", ")}" selector(s) in the ${process.env.npm_package_name} module.`);
2017-07-24 23:52:56 +03:00
})