Skip to content

Commit d94d48b

Browse files
committed
update: refactor into typescript
1 parent 1136a9d commit d94d48b

File tree

1 file changed

+32
-35
lines changed

1 file changed

+32
-35
lines changed
Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
1-
import { describe, expect, test } from 'vitest'
2-
3-
import {
4-
generateAPI
5-
} from '~/helpers/api/client.js'
6-
7-
import {
8-
isString
9-
} from '~/helpers/check-types.js'
10-
11-
const listingsCases = [
12-
1+
/**
2+
* Tests API client responses for various endpoints
3+
* Verifies that the API returns expected data structures
4+
*
5+
* @example
6+
* $ na vitest test/api/client.test.ts
7+
*/
8+
import { expect, test } from 'vitest'
9+
import { generateAPI } from '~/helpers/api/client.js'
10+
import { isString } from '~/helpers/check-types.js'
11+
12+
type DoesItAPIClient = any // TODO: Add proper type from client.js
13+
14+
interface APICase {
15+
generateOptions: Record<string, unknown>;
16+
method(api: DoesItAPIClient): any;
17+
expected: Record<string, unknown> | ((result: any) => boolean);
18+
}
19+
20+
type TestCase = [string, APICase]
21+
22+
const listingsCases: TestCase[] = [
1323
// Spotify
1424
[
1525
'/api/app/spotify.json',
@@ -58,52 +68,39 @@ const listingsCases = [
5868
{
5969
generateOptions: {},
6070
method: DoesItAPI => DoesItAPI('kind/app')(2),
61-
expected: result => isString( result.nextPage )
71+
expected: (result: any) => isString(result.nextPage)
6272
}
6373
]
6474
]
6575

66-
67-
test( 'API has valid responses', async t => {
68-
// const { listingsDetails } = t.context
69-
70-
for ( const [ caseEndpoint, listingCase ] of listingsCases ) {
71-
72-
// const apiPath = listingsDetails[ caseEndpoint ].apiEndpointPath
73-
74-
const DoesItAPI = generateAPI( listingCase.generateOptions )
75-
76-
const apiMethod = listingCase.method( DoesItAPI )
76+
test('API has valid responses', async () => {
77+
for (const [caseEndpoint, listingCase] of listingsCases) {
78+
const DoesItAPI = generateAPI(listingCase.generateOptions)
79+
const apiMethod = listingCase.method(DoesItAPI)
7780

7881
// Assert that the apiMethod url is correct
79-
// t.is( (new URL(apiMethod.url)).pathname, caseEndpoint, `API endpoint '${ caseEndpoint }'` )
8082
expect(
8183
(new URL(apiMethod.url)).pathname,
82-
`API endpoint '${ caseEndpoint }'`
84+
`API endpoint '${caseEndpoint}'`
8385
).toBe(caseEndpoint)
8486

8587
// Run get request to fetch our data
8688
const result = await apiMethod.get()
8789

8890
// If expected is a function then call it
89-
if ( typeof listingCase.expected === 'function' ) {
90-
// t.assert( listingCase.expected( result ), `API case method check for '${ caseEndpoint }'` )
91+
if (typeof listingCase.expected === 'function') {
9192
expect(
9293
listingCase.expected(result),
93-
`API case method check for '${ caseEndpoint }'`
94+
`API case method check for '${caseEndpoint}'`
9495
).toBeTruthy()
95-
9696
continue
9797
}
9898

99-
100-
101-
// t.like( result, listingCase.expected, `${ caseEndpoint } has a valid api endpoint` )
10299
expect(
103100
result,
104-
`${ caseEndpoint } has a valid api endpoint`
101+
`${caseEndpoint} has a valid api endpoint`
105102
).toEqual(
106103
expect.objectContaining(listingCase.expected)
107104
)
108105
}
109-
})
106+
})

0 commit comments

Comments
 (0)