From d94d48b6cdb8a315d3e391662846b15127bc7094 Mon Sep 17 00:00:00 2001 From: ThatGuySam Date: Mon, 10 Feb 2025 19:19:12 -0600 Subject: [PATCH] update: refactor into typescript --- test/api/{client.test.js => client.test.ts} | 59 ++++++++++----------- 1 file changed, 28 insertions(+), 31 deletions(-) rename test/api/{client.test.js => client.test.ts} (58%) diff --git a/test/api/client.test.js b/test/api/client.test.ts similarity index 58% rename from test/api/client.test.js rename to test/api/client.test.ts index 5aece2b..c8415ad 100644 --- a/test/api/client.test.js +++ b/test/api/client.test.ts @@ -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; + method(api: DoesItAPIClient): any; + expected: Record | ((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) ) } -}) +}) \ No newline at end of file