1+ /** biome-ignore-all lint/suspicious/noExplicitAny: any is used to allow for flexibility in the type */
12import { afterEach , beforeEach , expect , mock , test } from 'bun:test'
23import { DevupApi } from '../api'
34
@@ -24,7 +25,7 @@ test.each([
2425 [ 'http://localhost:3000' , 'http://localhost:3000' ] ,
2526 [ 'http://localhost:3000/' , 'http://localhost:3000' ] ,
2627] as const ) ( 'constructor removes trailing slash: %s -> %s' , ( baseUrl , expected ) => {
27- const api = new DevupApi ( baseUrl )
28+ const api = new DevupApi ( baseUrl , undefined , 'openapi.json' )
2829 expect ( api . getBaseUrl ( ) ) . toBe ( expected )
2930} )
3031
@@ -36,7 +37,11 @@ test.each([
3637 { headers : { Authorization : 'Bearer token' } } ,
3738 ] ,
3839] as const ) ( 'constructor accepts defaultOptions: %s -> %s' , ( defaultOptions , expected ) => {
39- const api = new DevupApi ( 'https://api.example.com' , defaultOptions )
40+ const api = new DevupApi (
41+ 'https://api.example.com' ,
42+ defaultOptions ,
43+ 'openapi.json' ,
44+ )
4045 expect ( api . getDefaultOptions ( ) ) . toEqual ( expected )
4146} )
4247
@@ -47,7 +52,7 @@ test.each([
4752 { headers : { 'Content-Type' : 'application/json' } } ,
4853 ] ,
4954] as const ) ( 'setDefaultOptions updates defaultOptions: %s -> %s' , ( options , expected ) => {
50- const api = new DevupApi ( 'https://api.example.com' )
55+ const api = new DevupApi ( 'https://api.example.com' , undefined , 'openapi.json' )
5156 api . setDefaultOptions ( options )
5257 expect ( api . getDefaultOptions ( ) ) . toEqual ( expected )
5358} )
@@ -64,10 +69,10 @@ test.each([
6469 [ 'PATCH' , 'patch' ] ,
6570 [ 'PATCH' , 'PATCH' ] ,
6671] as const ) ( 'HTTP method %s calls request with correct method' , async ( expectedMethod , methodName ) => {
67- const api = new DevupApi ( 'https://api.example.com' )
72+ const api = new DevupApi ( 'https://api.example.com' , undefined , 'openapi.json' )
6873 const mockFetch = globalThis . fetch as unknown as ReturnType < typeof mock >
6974
70- await api [ methodName ] ( '/test' as never )
75+ await ( api as any ) [ methodName ] ( '/test' as never )
7176
7277 expect ( mockFetch ) . toHaveBeenCalledTimes ( 1 )
7378 const call = mockFetch . mock . calls [ 0 ]
@@ -79,7 +84,7 @@ test.each([
7984} )
8085
8186test ( 'request serializes plain object body to JSON' , async ( ) => {
82- const api = new DevupApi ( 'https://api.example.com' )
87+ const api = new DevupApi ( 'https://api.example.com' , undefined , 'openapi.json' )
8388 const mockFetch = globalThis . fetch as unknown as ReturnType < typeof mock >
8489
8590 await api . post (
@@ -100,7 +105,7 @@ test('request serializes plain object body to JSON', async () => {
100105} )
101106
102107test ( 'request does not serialize non-plain object body' , async ( ) => {
103- const api = new DevupApi ( 'https://api.example.com' )
108+ const api = new DevupApi ( 'https://api.example.com' , undefined , 'openapi.json' )
104109 const mockFetch = globalThis . fetch as unknown as ReturnType < typeof mock >
105110 const formData = new FormData ( )
106111 formData . append ( 'file' , 'test' )
@@ -127,9 +132,13 @@ test('request does not serialize non-plain object body', async () => {
127132} )
128133
129134test ( 'request merges defaultOptions with request options' , async ( ) => {
130- const api = new DevupApi ( 'https://api.example.com' , {
131- headers : { 'X-Default' : 'default-value' } ,
132- } )
135+ const api = new DevupApi (
136+ 'https://api.example.com' ,
137+ {
138+ headers : { 'X-Default' : 'default-value' } ,
139+ } ,
140+ 'openapi.json' ,
141+ )
133142 const mockFetch = globalThis . fetch as unknown as ReturnType < typeof mock >
134143
135144 await api . get (
@@ -151,7 +160,7 @@ test('request merges defaultOptions with request options', async () => {
151160} )
152161
153162test ( 'request uses params to replace path parameters' , async ( ) => {
154- const api = new DevupApi ( 'https://api.example.com' )
163+ const api = new DevupApi ( 'https://api.example.com' , undefined , 'openapi.json' )
155164 const mockFetch = globalThis . fetch as unknown as ReturnType < typeof mock >
156165
157166 await api . get (
@@ -180,7 +189,7 @@ test('request returns response with data on success', async () => {
180189 ) ,
181190 ) as unknown as typeof fetch
182191
183- const api = new DevupApi ( 'https://api.example.com' )
192+ const api = new DevupApi ( 'https://api.example.com' , undefined , 'openapi.json' )
184193 const result = ( await api . get ( '/test' as never ) ) as {
185194 data ?: unknown
186195 error ?: unknown
@@ -191,7 +200,7 @@ test('request returns response with data on success', async () => {
191200 if ( 'data' in result && result . data !== undefined ) {
192201 expect ( result . data ) . toEqual ( { id : 1 , name : 'test' } )
193202 }
194- expect ( 'error' in result ) . toBe ( false )
203+ expect ( result . error ) . toBeUndefined ( )
195204 expect ( result . response ) . toBeDefined ( )
196205 expect ( result . response . ok ) . toBe ( true )
197206} )
@@ -206,7 +215,7 @@ test('request returns response with error on failure', async () => {
206215 ) ,
207216 ) as unknown as typeof fetch
208217
209- const api = new DevupApi ( 'https://api.example.com' )
218+ const api = new DevupApi ( 'https://api.example.com' , undefined , 'openapi.json' )
210219 const result = ( await api . get ( '/test' as never ) ) as {
211220 data ?: unknown
212221 error ?: unknown
@@ -217,7 +226,7 @@ test('request returns response with error on failure', async () => {
217226 if ( 'error' in result && result . error !== undefined ) {
218227 expect ( result . error ) . toEqual ( { message : 'Not found' } )
219228 }
220- expect ( 'data' in result ) . toBe ( false )
229+ expect ( result . data ) . toBeUndefined ( )
221230 expect ( result . response ) . toBeDefined ( )
222231 expect ( result . response . ok ) . toBe ( false )
223232} )
@@ -231,7 +240,7 @@ test('request handles 204 No Content response', async () => {
231240 ) ,
232241 ) as unknown as typeof fetch
233242
234- const api = new DevupApi ( 'https://api.example.com' )
243+ const api = new DevupApi ( 'https://api.example.com' , undefined , 'openapi.json' )
235244 const result = await api . delete ( '/test' as never )
236245
237246 if ( 'data' in result ) {
0 commit comments