Skip to content

Commit

Permalink
Fix example usage in README
Browse files Browse the repository at this point in the history
* Addresses issue #15
* Added examples of usage
  • Loading branch information
iaincollins committed Mar 8, 2020
1 parent 1de4782 commit 1ac057e
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 22 deletions.
48 changes: 29 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,32 +195,42 @@ You can integrate Structured Data Testing Tool with a CD/CI pipeline by using th

```javascript
const { structuredDataTest } = require('structured-data-testing-tool')
const { ReportageNewsArticle, Twitter, Facebook } = require('structured-data-testing-tool/presets')
const { Google, Twitter, Facebook } = require('structured-data-testing-tool/presets')

const url = 'https://www.bbc.co.uk/news/world-us-canada-49060410'

structuredDataTest(url, { presets: [ ReportageNewsArticle, Twitter, Facebook ] })

let result

structuredDataTest(url, {
// Check for compliance with Google, Twitter and Facebook recommendations
presets: [ Google, Twitter, Facebook ],
// Check the page includes a specific Schema (see https://schema.org/docs/full.html for a list)
schemas: [ 'ReportageNewsArticle' ]
})
.then(res => {
// If you end up here, then there were no errors
console.log("All tests passed.")
console.log('Passed:',res.passed.length)
console.log('Failed:',res.failed.length)
console.log('Warnings:',res.warnings.length)
console.log('✅ All tests passed!')
result = res
})
.catch(err => {
// If any test fails, the promise is rejected
if (err.type === 'VALIDATION_FAILED') {
console.log("Some tests failed.")
console.log('Passed:',err.res.passed.length)
console.log('Failed:',err.res.failed.length)
console.log('Warnings:',err.res.warnings.length)
// Loop over validation errors
err.res.failed.forEach(test => {
console.error(test)
})
console.log('❌ Some tests failed.')
result = err.res
} else {
// Handle other errors here (e.g. an error fetching a URL)
console.log(err)
console.log(err) // Handle other errors here (e.g. an error fetching a URL)
}
})
.finally(() => {
if (result) {
console.log(
`Passed: ${result.passed.length},`,
`Failed: ${result.failed.length},`,
`Warnings: ${result.warnings.length}`,
)
console.log(`Schemas found: ${result.schemas.join(',')}`)

// Loop over validation errors
if (result.failed.length > 0)
console.log("⚠️ Errors:\n", result.failed.map(test => test))
}
})
```
Expand Down
41 changes: 41 additions & 0 deletions examples/test-multiple-urls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const { structuredDataTest } = require('structured-data-testing-tool')
const { Google, Twitter, Facebook } = require('structured-data-testing-tool/presets')

const urls = [
'https://www.bbc.co.uk/news/world-us-canada-49060410',
'https://www.nytimes.com/2020/03/08/world/coronavirus-news.html',
'https://www.ft.com/content/75583754-5fab-11ea-b0ab-339c2307bcd4',
'https://news.sky.com/story/coronavirus-italy-sees-largest-daily-rise-in-deaths-and-confirmed-cases-11953192'
]

urls.forEach(url => {
let result
structuredDataTest(url, {
presets: [ Google, Twitter, Facebook ],
})
.then(res => {
console.log(`✅ ${url}`)
result = res
})
.catch(err => {
if (err.type === 'VALIDATION_FAILED') {
console.log(`❌ ${url}`)
result = err.res
} else {
console.log(err) // Handle other errors here (e.g. an error fetching a URL)
}
})
.finally(() => {
if (result) {
console.log(
` Passed: ${result.passed.length},`,
` Failed: ${result.failed.length},`,
` Warnings: ${result.warnings.length}`,
)
console.log(` Schemas found: ${result.schemas.join(',')}`)

// Loop over validation errors
//if (result.failed.length > 0) console.log("⚠️ Errors:\n", result.failed.map(test => test))
}
})
})
39 changes: 39 additions & 0 deletions examples/test-url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const { structuredDataTest } = require('structured-data-testing-tool')
const { Google, Twitter, Facebook } = require('structured-data-testing-tool/presets')

const url = 'https://www.bbc.co.uk/news/world-us-canada-49060410'

let result

structuredDataTest(url, {
// Check for compliance with Google, Twitter and Facebook recommendations
presets: [ Google, Twitter, Facebook ],
// Check the page includes a specific Schema (see https://schema.org/docs/full.html for a list)
schemas: [ 'ReportageNewsArticle' ]
})
.then(res => {
console.log('✅ All tests passed!')
result = res
})
.catch(err => {
if (err.type === 'VALIDATION_FAILED') {
console.log('❌ Some tests failed.')
result = err.res
} else {
console.log(err) // Handle other errors here (e.g. an error fetching a URL)
}
})
.finally(() => {
if (result) {
console.log(
`Passed: ${result.passed.length},`,
`Failed: ${result.failed.length},`,
`Warnings: ${result.warnings.length}`,
)
console.log(`Schemas found: ${result.schemas.join(',')}`)

// Loop over validation errors
if (result.failed.length > 0)
console.log("⚠️ Errors:\n", result.failed.map(test => test))
}
})
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,11 @@ const _structuredDataTest = (structuredData, options) => {
// This is a recursive function scoped to this function
const _addTestsFromPresets = (presets, structuredData, tests, testsSkipped, testGroups) => {
presets.forEach(preset => {
if (!preset.name) {
if (!preset)
throw new Error(`Invalid preset specified`)

if (!preset.name)
throw new Error(`Preset specified does not have a 'name' (required)`)
}

const groups = (Array.isArray(testGroups)) ? testGroups.concat(preset.name) : [preset.name]

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1ac057e

Please sign in to comment.