Skip to content

Commit

Permalink
chore: use of testing files instead of snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnvanhulle committed Sep 7, 2024
1 parent 1008dc1 commit 1027518
Show file tree
Hide file tree
Showing 105 changed files with 962 additions and 1,141 deletions.
76 changes: 41 additions & 35 deletions packages/core/mocks/index.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,58 @@
import { camelCase, pascalCase } from '../src/transformers/casing.ts'

import path from 'node:path'
import type { File, ResolvedFile } from '@kubb/fs/types'
import { getSource } from '../src/FileManager'
import type { PluginManager } from '../src/PluginManager.ts'
import type { Logger } from '../src/logger'
import { format } from './format'

export const mockedLogger = {
emit(type, message) {},
on(type, message) {},
consola: {},
} as Logger

export const mockedPluginManager = {
resolveName: ({ name, type }) => {
if (type === 'type') {
return pascalCase(name)
}

if (type === 'function') {
return camelCase(name)
}

return camelCase(name)
},
config: {
output: {
path: './path',
export const createMockedPluginManager = (name?: string) =>
({
resolveName: (result) => {
if (result.type === 'type') {
return pascalCase(name || result.name)
}

if (result.type === 'function') {
return camelCase(name || result.name)
}

return camelCase(name || result.name)
},
},
resolvePath: ({ baseName }) => baseName,
logger: {
emit(message) {
console.log(message)
config: {
output: {
path: './path',
},
},
on(eventName, args) {},
logLevel: 3,
},
getFile: ({ name, extName, pluginKey }) => {
const baseName = `${name}${extName}`

return {
path: baseName,
baseName,
meta: {
pluginKey,
resolvePath: ({ baseName }) => baseName,
logger: {
emit(message) {
console.log(message)
},
}
},
} as PluginManager
on(eventName, args) {},
logLevel: 3,
},
getFile: ({ name, extName, pluginKey }) => {
const baseName = `${name}${extName}`

return {
path: baseName,
baseName,
meta: {
pluginKey,
},
}
},
}) as PluginManager

export const mockedPluginManager = createMockedPluginManager('')

export async function matchFiles(files: Array<ResolvedFile | File> | undefined) {
if (!files) {
Expand All @@ -56,6 +61,7 @@ export async function matchFiles(files: Array<ResolvedFile | File> | undefined)

for (const file of files) {
const source = await getSource(file as ResolvedFile, { logger: mockedLogger })
expect(source).toMatchSnapshot()
const formattedSource = await format(source)
expect(formattedSource).toMatchFileSnapshot(path.join('__snapshots__', file.path))
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* @summary Create a pet
* @link /pets
*/
export async function createPet(data: CreatePet, options: Partial<Parameters<typeof client>[0]> = {}): Promise<ResponseConfig<CreatePet>['data']> {
const res = await client<CreatePet, CreatePet>({ method: 'post', url: `/pets`, data, ...options })
return res.data
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* @summary List all pets
* @link /pets
*/
export async function getPets(params?: GetPets, options: Partial<Parameters<typeof client>[0]> = {}): Promise<ResponseConfig<GetPets>['data']> {
const res = await client<GetPets>({ method: 'get', url: `/pets`, params, ...options })
return res.data
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* @summary List all pets
* @link /pets
*/
export async function getPetsFull(params?: GetPetsFull, options: Partial<Parameters<typeof client>[0]> = {}): Promise<ResponseConfig<GetPetsFull>> {
const res = await client<GetPetsFull>({ method: 'get', url: `/pets`, params, ...options })
return res
}
14 changes: 14 additions & 0 deletions packages/plugin-client/src/generators/__snapshots__/operations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const operations = {
listPets: {
path: '/pets',
method: 'get',
},
createPets: {
path: '/pets',
method: 'post',
},
showPetById: {
path: '/pets/:petId',
method: 'get',
},
} as const

This file was deleted.

12 changes: 12 additions & 0 deletions packages/plugin-client/src/generators/__snapshots__/showPetById.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @summary Info for a specific pet
* @link /pets/:petId
*/
export async function showPetById(
petId: ShowPetById['petId'],
testId: ShowPetById['testId'],
options: Partial<Parameters<typeof client>[0]> = {},
): Promise<ResponseConfig<ShowPetById>['data']> {
const res = await client<ShowPetById>({ method: 'get', url: `/pets/${petId}`, ...options })
return res.data
}
22 changes: 20 additions & 2 deletions packages/plugin-client/src/generators/clientGenerator.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { matchFiles, mockedPluginManager } from '@kubb/core/mocks'
import { createMockedPluginManager, matchFiles, mockedPluginManager } from '@kubb/core/mocks'

import path from 'node:path'
import type { Plugin } from '@kubb/core'
Expand All @@ -25,6 +25,24 @@ describe('clientGenerator operation', async () => {
method: 'get',
options: {},
},
{
name: 'getPetsFull',
input: '../../mocks/petStore.yaml',
path: '/pets',
method: 'get',
options: {
dataReturnType: 'full',
},
},
// {
// name: 'getPetsFullPathObject',
// input: '../../mocks/petStore.yaml',
// path: '/pets',
// method: 'get',
// options: {
// pathParamsType: "object"
// },
// },
{
name: 'createPet',
input: '../../mocks/petStore.yaml',
Expand Down Expand Up @@ -55,7 +73,7 @@ describe('clientGenerator operation', async () => {
const instance = new OperationGenerator(options, {
oas,
include: undefined,
pluginManager: mockedPluginManager,
pluginManager: createMockedPluginManager(props.name),
plugin,
contentType: undefined,
override: undefined,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { matchFiles, mockedPluginManager } from '@kubb/core/mocks'
import { createMockedPluginManager, matchFiles, mockedPluginManager } from '@kubb/core/mocks'

import path from 'node:path'
import type { Plugin } from '@kubb/core'
Expand Down Expand Up @@ -41,7 +41,7 @@ describe('operationsGenerator operations', async () => {
const instance = new OperationGenerator(options, {
oas,
include: undefined,
pluginManager: mockedPluginManager,
pluginManager: createMockedPluginManager(props.name),
plugin,
contentType: undefined,
override: undefined,
Expand Down
24 changes: 24 additions & 0 deletions packages/plugin-faker/src/generators/__snapshots__/createPet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @description Null response
*/
export function createPet(): NonNullable<CreatePet> {
return unknown
}

/**
* @description unexpected error
*/
export function createPet(): NonNullable<CreatePet> {
return createPet()
}

export function createPet(data: NonNullable<Partial<CreatePet>> = {}): NonNullable<CreatePet> {
return {
...{ name: faker.string.alpha(), tag: faker.string.alpha() },
...data,
}
}

export function createPet(): NonNullable<CreatePet> {
return unknown
}
Loading

0 comments on commit 1027518

Please sign in to comment.