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