Skip to content

Commit

Permalink
chore: add test cases for variablesType
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnvanhulle committed Mar 10, 2024
1 parent 3da2307 commit 73d6f31
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 34 deletions.
121 changes: 87 additions & 34 deletions packages/swagger-tanstack-query/src/components/Mutation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,41 +32,94 @@ describe('<Mutation/>', async () => {
return name
}

const options: GetOperationGeneratorOptions<OperationGenerator> = {
framework: 'react',
infinite: undefined,
suspense: undefined,
dataReturnType: 'data',
pathParamsType: 'inline',
templates: {
query: Query.templates,
queryKey: QueryKey.templates,
queryOptions: QueryOptions.templates,
mutation: Mutation.templates,
},
client: {
importPath: '@kubb/swagger-client/client',
},
parser: undefined,
query: {},
mutate: { variablesType: 'mutate' },
}
test('pets with veriableType `hook`', async () => {
const options: GetOperationGeneratorOptions<OperationGenerator> = {
framework: 'react',
infinite: undefined,
suspense: undefined,
dataReturnType: 'data',
pathParamsType: 'inline',
templates: {
query: Query.templates,
queryKey: QueryKey.templates,
queryOptions: QueryOptions.templates,
mutation: Mutation.templates,
},
client: {
importPath: '@kubb/swagger-client/client',
},
parser: undefined,
query: {},
mutate: { variablesType: 'hook' },
}

const plugin = { options } as Plugin<PluginOptions>
const og = await new OperationGenerator(
options,
{
oas,
exclude: [],
include: undefined,
pluginManager: mockedPluginManager,
plugin,
contentType: undefined,
override: undefined,
},
)

const operation = oas.operation('/pets/{uuid}', 'post')
const context: AppContextProps<PluginOptions['appMeta']> = { meta: { pluginManager: mockedPluginManager, plugin } }

const Component = () => {
return (
<Oas oas={oas} operations={[operation]} getSchemas={(...props) => og.getSchemas(...props)}>
<Oas.Operation operation={operation}>
<Mutation.File />
</Oas.Operation>
</Oas>
)
}
const root = createRootServer({ logger: mockedPluginManager.logger })
const output = await root.renderToString(<Component />, context)

expect(output).toMatchFileSnapshot('./__snapshots__/Mutation/useCreatePets.ts')

Check failure on line 85 in packages/swagger-tanstack-query/src/components/Mutation.test.tsx

View workflow job for this annotation

GitHub Actions / Tests

TypeError: expect(output).toMatchFileSnapshot is not a function. (In 'expect(output).toMatchFileSnapshot("./__snapshots__/Mutation/useCreatePets.ts")'

at /home/runner/work/kubb/kubb/packages/swagger-tanstack-query/src/components/Mutation.test.tsx:85:5
})

test('pets with veriableType `mutate`', async () => {
const options: GetOperationGeneratorOptions<OperationGenerator> = {
framework: 'react',
infinite: undefined,
suspense: undefined,
dataReturnType: 'data',
pathParamsType: 'inline',
templates: {
query: Query.templates,
queryKey: QueryKey.templates,
queryOptions: QueryOptions.templates,
mutation: Mutation.templates,
},
client: {
importPath: '@kubb/swagger-client/client',
},
parser: undefined,
query: {},
mutate: { variablesType: 'mutate' },
}

const plugin = { options } as Plugin<PluginOptions>
const og = await new OperationGenerator(
options,
{
oas,
exclude: [],
include: undefined,
pluginManager: mockedPluginManager,
plugin,
contentType: undefined,
override: undefined,
},
)
const plugin = { options } as Plugin<PluginOptions>
const og = await new OperationGenerator(
options,
{
oas,
exclude: [],
include: undefined,
pluginManager: mockedPluginManager,
plugin,
contentType: undefined,
override: undefined,
},
)

test('pets', async () => {
const operation = oas.operation('/pets/{uuid}', 'post')
const context: AppContextProps<PluginOptions['appMeta']> = { meta: { pluginManager: mockedPluginManager, plugin } }

Expand All @@ -82,6 +135,6 @@ describe('<Mutation/>', async () => {
const root = createRootServer({ logger: mockedPluginManager.logger })
const output = await root.renderToString(<Component />, context)

expect(output).toMatchFileSnapshot('./__snapshots__/Mutation/Pets.ts')
expect(output).toMatchFileSnapshot('./__snapshots__/Mutation/useCreatePetsMutate.ts')

Check failure on line 138 in packages/swagger-tanstack-query/src/components/Mutation.test.tsx

View workflow job for this annotation

GitHub Actions / Tests

TypeError: expect(output).toMatchFileSnapshot is not a function. (In 'expect(output).toMatchFileSnapshot("./__snapshots__/Mutation/useCreatePetsMutate.ts")'

at /home/runner/work/kubb/kubb/packages/swagger-tanstack-query/src/components/Mutation.test.tsx:138:5
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
type CreatePetsClient = typeof client<CreatePetsMutationResponse, never, CreatePetsMutationRequest>
type CreatePets = {
data: CreatePetsMutationResponse
error: never
request: CreatePetsMutationRequest
pathParams: CreatePetsPathParams
queryParams: CreatePetsQueryParams
headerParams: CreatePetsHeaderParams
response: CreatePetsMutationResponse
client: {
parameters: Partial<Parameters<CreatePetsClient>[0]>
return: Awaited<ReturnType<CreatePetsClient>>
}
}

/**
* @summary Create a pet
* @link /pets/:uuid */

export function useCreatePets(
uuid: CreatePetsPathParams['uuid'],
headers: CreatePets['headerParams'],
params?: CreatePets['queryParams'],
options: {
mutation?: UseMutationOptions<CreatePets['response'], CreatePets['error'], CreatePets['request']>
client?: CreatePets['client']['parameters']
} = {},
): UseMutationResult<CreatePets['response'], CreatePets['error'], CreatePets['request']> {
const { mutation: mutationOptions, client: clientOptions = {} } = options ?? {}

return useMutation<CreatePets['response'], CreatePets['error'], CreatePets['request']>({
mutationFn: async (data) => {
const res = await client<CreatePets['data'], CreatePets['error'], CreatePets['request']>({
method: 'post',
url: `/pets/${uuid}`,
params,
data,
headers: { ...headers, ...clientOptions.headers },
...clientOptions,
})

return res.data
},
...mutationOptions,
})
}

0 comments on commit 73d6f31

Please sign in to comment.