algolia-indices/test.js

120 строки
5.4 KiB
JavaScript

const test = require('tape')
const isURL = require('is-url')
const indices = require('./indices')
const exportedIndices = require('.')
test('electron-search', t => {
// ensure the object exported by `require('electron-algolia-indices')`
// is the same as what we are testing in this file
t.deepEqual(indices, exportedIndices, 'exports and indices object')
// All Indices
// ----------------------------------------------------------------------
const indexNames = ['apis', 'apps', 'glossary', 'packages', 'tutorials']
t.deepEqual(Object.keys(indices), indexNames, 'defines expected indexNames as keys')
t.deepEqual(Object.values(indices).map(index => index.name), indexNames, 'has expected index names')
Object.values(indices).forEach(index => {
t.ok(index.validate(), `index is valid: ${index.name}`)
})
// APIs
// ----------------------------------------------------------------------
const apis = indices.apis.records
t.ok(apis.length > 450, 'lots of APIs')
const staticMethod = apis.find(api => api.fullSignature === 'Menu.getApplicationMenu()')
t.equal(staticMethod.url, 'https://electronjs.org/docs/api/menu#menugetapplicationmenu', 'sets proper URL on static methods')
const event = apis.find(api => api.fullSignature === "browserWindow.on('page-title-updated')")
t.equal(event.url, 'https://electronjs.org/docs/api/browser-window#event-page-title-updated', 'sets expected URL on events')
apis.forEach(api => {
t.equal(typeof api.fullSignature, 'string', `${api.fullSignature} has a fullSignature`)
t.equal(typeof api.name, 'string', `${api.fullSignature} has a name`)
t.ok(!api.tldr || (api.tldr.endsWith('.') && !api.tldr.endsWith('..')), `${api.fullSignature} has a valid tldr, or no tldr`)
t.ok(api.keyValuePairs.includes('is:api'), `${api.fullSignature} has is:api key-value pair`)
t.ok(api.keyValuePairs.includes('is:doc'), `${api.fullSignature} has is:api key-value pair`)
})
const apisWithTldrs = apis.filter(api => api.tldr && api.tldr.length > 10)
const tldrThreshold = 80
t.ok(apisWithTldrs.length / apis.length * 100 > tldrThreshold, `At least ${tldrThreshold}% of APIs have a tldr`)
// method URLs should match the slugs generated by github.com (and hubdown)
const apiUrls = apis.map(api => api.url)
const expectedUrl = 'https://electronjs.org/docs/api/web-request#webrequestonheadersreceivedfilter-listener'
t.ok(apiUrls.includes(expectedUrl), `API with URL exists: ${expectedUrl}`)
// Tutorials
// ----------------------------------------------------------------------
const tutorials = indices.tutorials.records
t.ok(tutorials.length > 25, 'lots of tutorials')
tutorials.forEach(tutorial => {
if (!tutorial.title) console.log(tutorial)
t.equal(typeof tutorial.title, 'string', `${tutorial.title} has a title`)
t.equal(typeof tutorial.body, 'string', `${tutorial.title} has a body`)
t.ok(isURL(tutorial.githubUrl), `${tutorial.title} has a valid GitHub URL`)
t.ok(isURL(tutorial.url), `${tutorial.title} has a valid website URL`)
t.ok(tutorial.keyValuePairs.includes('is:doc'), `${tutorial.title} has is:doc key-value pair`)
t.ok(tutorial.keyValuePairs.includes('is:tutorial'), `${tutorial.title} has is:tutorial key-value pair`)
})
// Glossary
// ----------------------------------------------------------------------
const glossarys = indices.glossary.records
t.ok(glossarys.length > 15, 'lots of glossary')
glossarys.forEach(glossary => {
if (!glossary.term) console.log(glossary)
t.equal(typeof glossary.term, 'string', `${glossary.title} has a title`)
t.equal(typeof glossary.description, 'string', `${glossary.title} has a body`)
t.ok(isURL(glossary.url), `${glossary.title} has a valid website URL`)
t.ok(glossary.keyValuePairs.includes('is:doc'), `${glossary.title} has is:doc key-value pair`)
t.ok(glossary.keyValuePairs.includes('is:glossary'), `${glossary.title} has is:glossary key-value pair`)
})
// Packages
// ----------------------------------------------------------------------
const packages = indices.packages.records
t.ok(packages.length > 25, 'lots of packages')
packages.forEach(pkg => {
if (!pkg.name) console.log(pkg)
t.equal(typeof pkg.name, 'string', `${pkg.name} has a name`)
t.ok(pkg.keyValuePairs.includes('is:pkg'), `${pkg.name} has is:pkg key-value pair`)
t.ok(pkg.keyValuePairs.includes('is:pkg'), `${pkg.name} has is:package key-value pair`)
// t.ok(isURL(pkg.githubUrl), `${pkg.title} has a valid GitHub URL`)
// t.ok(isURL(pkg.repository), `${pkg.title} has a valid repository`)
})
// Apps
// ----------------------------------------------------------------------
const apps = indices.apps.records
t.ok(apps.length > 500, 'lots of APPS')
apps.forEach(app => {
if (!app.name) console.log(app)
t.equal(typeof app.name, 'string', `${app.name} has a name`)
t.ok(app.keyValuePairs.includes('is:app'), `${app.name} has is:pkg key-value pair`)
t.ok(app.keyValuePairs.includes('is:app'), `${app.name} has is:package key-value pair`)
t.equal(typeof app.category, 'string', `${app.name} has category`)
t.equal(typeof app.icon, 'string', `${app.name} has icon`)
// Skipped, not all apps have a website or repository.
// t.ok(isURL(app.website), `${app.title} has a valid website URL`)
// t.ok(isURL(app.repository), `${app.title} has a valid repository`)
})
// TODO: Repos
// ----------------------------------------------------------------------
t.end()
})