Skip to content

Commit

Permalink
chore(client): refactor and enhance OpenApiV2 test descriptions
Browse files Browse the repository at this point in the history
Refactored the OpenApiV2 test suite for better organization and clarity. Moved server URL tests into a new 'ServerUrl' describe block, improving readability and making it easier to understand the context of each test. Also, removed redundant tests previously under 'InferRequest' and restructured them under appropriate new describe blocks such as 'Parameters' and 'RequestBody'. This reorganization helps in maintaining a cleaner and more systematic approach to testing the OpenAPI V2 specifications.
  • Loading branch information
shorwood committed Nov 22, 2024
1 parent 92d1df6 commit 4e70570
Showing 1 changed file with 25 additions and 50 deletions.
75 changes: 25 additions & 50 deletions packages/client/openapi/OpenApiV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@
import type { OpenAPIV2 } from './OpenApiV2'

describe('OpenApiV2', () => {
describe('ServerUrl', () => {
it('should infer server url types when all properties are present', () => {
type Result = OpenAPIV2.ServerUrl<{ host: 'api.example.com'; basePath: '/v1'; schemes: ['https'] }>
expectTypeOf<Result>().toEqualTypeOf<'https://api.example.com/v1'>()
})

it('should infer an union of server url types when multiple schemes are present', () => {
type Result = OpenAPIV2.ServerUrl<{ host: 'api.example.com'; basePath: '/v1'; schemes: ['http', 'https'] }>
expectTypeOf<Result>().toEqualTypeOf<'http://api.example.com/v1' | 'https://api.example.com/v1'>()
})

it('should infer server url types when only host is present', () => {
type Result = OpenAPIV2.ServerUrl<{ host: 'api.example.com' }>
expectTypeOf<Result>().toEqualTypeOf<`${string}://api.example.com${string}`>()
})

it('should fallback to string when host is missing', () => {
type Result = OpenAPIV2.ServerUrl<{ basePath: '/v1'; schemes: ['https'] }>
expectTypeOf<Result>().toEqualTypeOf<string>()
})
})

describe('InferSchema', () => {
describe('primitive', () => {
it('should infer string schema types', () => {
Expand Down Expand Up @@ -109,55 +131,8 @@ describe('OpenApiV2', () => {
})
})

describe('InferRequest', () => {
describe('InferRequestBaseUrl', () => {
it('should infer server url types when all properties are present', () => {
type Result = OpenAPIV2.ServerUrl<{
swagger: '2.0'
info: { title: 'Example API'; version: '1.0' }
paths: {}
host: 'api.example.com'
basePath: '/v1'
schemes: ['https']
}>
expectTypeOf<Result>().toEqualTypeOf<'https://api.example.com/v1'>()
})

it('should infer if multiple schemes are present', () => {
type Result = OpenAPIV2.ServerUrl<{
swagger: '2.0'
info: { title: 'Example API'; version: '1.0' }
paths: {}
host: 'api.example.com'
basePath: '/v1'
schemes: ['http', 'https']
}>
expectTypeOf<Result>().toEqualTypeOf<'http://api.example.com/v1' | 'https://api.example.com/v1'>()
})

it('should infer server url types when only host is present', () => {
type Result = OpenAPIV2.ServerUrl<{
swagger: '2.0'
info: { title: 'Example API'; version: '1.0' }
paths: {}
host: 'api.example.com'
}>
expectTypeOf<Result>().toEqualTypeOf<`${string}://api.example.com${string}`>()
})

it('should fallback to string when host is missing', () => {
type Result = OpenAPIV2.ServerUrl<{
swagger: '2.0'
info: { title: 'Example API'; version: '1.0' }
basePath: '/v1'
schemes: ['https']
paths: {}
}>
expectTypeOf<Result>().toEqualTypeOf<string>()
})
})

describe('InferRequestParameters', () => {
describe('Request', () => {
describe('Parameters', () => {
describe('optional parameters', () => {
it('should infer request query parameters types', () => {
type Result = OpenAPIV2.Parameters<{ responses: {}; parameters: [{ in: 'query'; name: 'name'; type: 'number' }] }, 'query'>
Expand Down Expand Up @@ -227,7 +202,7 @@ describe('OpenApiV2', () => {
})
})

describe('InferRequestBody', () => {
describe('RequestBody', () => {
it('should infer request body types', () => {
type Result = OpenAPIV2.RequestBody<{
responses: {}
Expand Down

0 comments on commit 4e70570

Please sign in to comment.