Test that listing have valid api endpoint

This commit is contained in:
Sam Carlton 2022-05-06 18:25:28 -05:00
parent 29df79504e
commit 0accd4c73e

View file

@ -10,49 +10,90 @@ import {
isNonEmptyString, isNonEmptyString,
isPositiveNumberString isPositiveNumberString
} from '~/helpers/check-types.js' } from '~/helpers/check-types.js'
import { ListingDetails } from '~/helpers/listing-page.js' import {
makeApiPathFromEndpoint,
ListingDetails
} from '~/helpers/listing-page.js'
import { PageHead } from '~/helpers/config.js' import { PageHead } from '~/helpers/config.js'
const listings = [ const listingsCases = {
// Spotify // Spotify
{ '/app/spotify': {
endpoint: '/app/spotify' endpoint: '/app/spotify',
apiEndpointPath: '/api/app/spotify.json',
}, },
// Electron // Electron
{ '/app/electron-framework': {
endpoint: '/app/electron-framework' endpoint: '/app/electron-framework',
apiEndpointPath: '/api/app/electron-framework.json',
},
// Express VPN Benchmarks
'/app/expressvpn/benchmarks/': {
endpoint: '/app/expressvpn/benchmarks/',
apiEndpointPath: '/api/app/expressvpn.json',
} }
] }
const listingCaseEntries = Object.entries( listingsCases )
test.before(async t => { test.before(async t => {
t.context.listings = await Promise.all(
listings.map(async listing => {
const { endpoint } = listing
const apiPath = `/api${ endpoint }.json` t.context.listings = {}
const localPath = `./static/api${ endpoint }.json`
// Check if the endpoint exists locally for ( const [ caseEndpoint, listingCase ] of listingCaseEntries ) {
// so we don't have to wait for the API const { endpoint } = listingCase
if ( await fs.pathExists( localPath ) ) {
console.log('Using local endpoint data for', endpoint)
return await fs.readJson( localPath )
}
const { data } = await axios.get(`${ process.env.PUBLIC_API_DOMAIN }${ apiPath }`) const apiPath = makeApiPathFromEndpoint( caseEndpoint )
const localPath = `./static${ apiPath }`
return data // Check if the endpoint exists locally
}) // so we don't have to wait for the API
) if ( await fs.pathExists( localPath ) ) {
console.log('Using local endpoint data for', endpoint)
t.context.listings[ caseEndpoint ] = await fs.readJson( localPath )
continue
}
const { data } = await axios.get(`${ process.env.PUBLIC_API_DOMAIN }${ apiPath }`)
t.context.listings[ caseEndpoint ] = data
}
t.context.listingsDetails = {}
for ( const [ caseEndpoint ] of listingCaseEntries ) {
t.context.listingsDetails[ caseEndpoint ] = new ListingDetails( t.context.listings[ caseEndpoint ] )
}
}) })
function parseHTML ( htmlString ) {
const dom = new JSDOM( htmlString )
return {
dom,
window: dom.window,
document: dom.window.document
}
}
test( 'Listings have valid api endpoints', async t => {
const { listingsDetails } = t.context
for ( const [ caseEndpoint, listingCase ] of listingCaseEntries ) {
const apiPath = listingsDetails[ caseEndpoint ].apiEndpointPath
t.assert( listingCase.apiEndpointPath === apiPath, `${ caseEndpoint } has a valid api endpoint` )
}
})
const headPropertyTypes = { const headPropertyTypes = {
'meta[charset]': { 'meta[charset]': {
@ -134,22 +175,14 @@ const headPropertyTypes = {
}, },
} }
function parseHTML ( htmlString ) {
const dom = new JSDOM( htmlString )
return {
dom,
window: dom.window,
document: dom.window.document
}
}
test('Listings have valid headings', async t => { test('Listings have valid headings', async t => {
const { listings } = t.context const { listingsDetails } = t.context
for ( const [ caseEndpoint, listingCase ] of listingCaseEntries ) {
for ( const listing of listings ) {
// Build listing details // Build listing details
const listingDetails = new ListingDetails( listing ) const listingDetails = listingsDetails[ caseEndpoint ]
const listingPageHead = new PageHead( listingDetails.headOptions ) const listingPageHead = new PageHead( listingDetails.headOptions )
// console.log( 'pageMeta', listingPageHead.metaMarkup ) // console.log( 'pageMeta', listingPageHead.metaMarkup )