update: refactor into typescript

This commit is contained in:
ThatGuySam 2025-02-10 19:19:12 -06:00
parent 1136a9d59c
commit d94d48b6cd

View file

@ -1,15 +1,25 @@
import { describe, expect, test } from 'vitest'
/**
* Tests API client responses for various endpoints
* Verifies that the API returns expected data structures
*
* @example
* $ na vitest test/api/client.test.ts
*/
import { expect, test } from 'vitest'
import { generateAPI } from '~/helpers/api/client.js'
import { isString } from '~/helpers/check-types.js'
import {
generateAPI
} from '~/helpers/api/client.js'
type DoesItAPIClient = any // TODO: Add proper type from client.js
import {
isString
} from '~/helpers/check-types.js'
interface APICase {
generateOptions: Record<string, unknown>;
method(api: DoesItAPIClient): any;
expected: Record<string, unknown> | ((result: any) => boolean);
}
const listingsCases = [
type TestCase = [string, APICase]
const listingsCases: TestCase[] = [
// Spotify
[
'/api/app/spotify.json',
@ -58,52 +68,39 @@ const listingsCases = [
{
generateOptions: {},
method: DoesItAPI => DoesItAPI('kind/app')(2),
expected: result => isString( result.nextPage )
expected: (result: any) => isString(result.nextPage)
}
]
]
test( 'API has valid responses', async t => {
// const { listingsDetails } = t.context
for ( const [ caseEndpoint, listingCase ] of listingsCases ) {
// const apiPath = listingsDetails[ caseEndpoint ].apiEndpointPath
const DoesItAPI = generateAPI( listingCase.generateOptions )
const apiMethod = listingCase.method( DoesItAPI )
test('API has valid responses', async () => {
for (const [caseEndpoint, listingCase] of listingsCases) {
const DoesItAPI = generateAPI(listingCase.generateOptions)
const apiMethod = listingCase.method(DoesItAPI)
// Assert that the apiMethod url is correct
// t.is( (new URL(apiMethod.url)).pathname, caseEndpoint, `API endpoint '${ caseEndpoint }'` )
expect(
(new URL(apiMethod.url)).pathname,
`API endpoint '${ caseEndpoint }'`
`API endpoint '${caseEndpoint}'`
).toBe(caseEndpoint)
// Run get request to fetch our data
const result = await apiMethod.get()
// If expected is a function then call it
if ( typeof listingCase.expected === 'function' ) {
// t.assert( listingCase.expected( result ), `API case method check for '${ caseEndpoint }'` )
if (typeof listingCase.expected === 'function') {
expect(
listingCase.expected(result),
`API case method check for '${ caseEndpoint }'`
`API case method check for '${caseEndpoint}'`
).toBeTruthy()
continue
}
// t.like( result, listingCase.expected, `${ caseEndpoint } has a valid api endpoint` )
expect(
result,
`${ caseEndpoint } has a valid api endpoint`
`${caseEndpoint} has a valid api endpoint`
).toEqual(
expect.objectContaining(listingCase.expected)
)
}
})
})