diff --git a/src/transform.ts b/src/transform.ts index 71bdbe9..84f1a22 100644 --- a/src/transform.ts +++ b/src/transform.ts @@ -5,6 +5,8 @@ import camelCase from 'lodash/camelCase'; import { faker } from '@faker-js/faker'; import { ConfigOptions } from './types'; +const MAX_STRING_LENGTH = 42; + export interface ResponseMap { code: string; id: string; @@ -33,6 +35,7 @@ export function transformToGenerateResultFunctions( ): string { const context = { faker, + MAX_STRING_LENGTH, MAX_ARRAY_LENGTH: options?.maxArrayLength ?? 20, baseURL: baseURL ?? '', result: null, @@ -195,11 +198,11 @@ function transformStringBasedOnFormat(schema: OpenAPIV3.NonArraySchemaObject, ke return `faker.person.fullName()`; } else { if (minLength && maxLength) { - return `faker.string.alpha({ length: { min: ${minLength}, max: ${maxLength} })`; + return `faker.string.alpha({ length: { min: ${minLength}, max: ${maxLength} }})`; } else if (minLength) { - return `faker.string.alpha({ length: { min: ${minLength} })`; + return `faker.string.alpha({ length: { min: ${minLength}, max: MAX_STRING_LENGTH }})`; } else if (maxLength) { - return `faker.string.alpha({ length: { max: ${maxLength} })`; + return `faker.string.alpha({ length: { min: 0, max: ${maxLength} }})`; } else { return `faker.lorem.words()`; } diff --git a/test/fixture/strings.yaml b/test/fixture/strings.yaml new file mode 100644 index 0000000..d12c635 --- /dev/null +++ b/test/fixture/strings.yaml @@ -0,0 +1,33 @@ +openapi: 3.0.0 +info: + title: (title) + version: 0.0.0 +tags: [] +paths: + /test: + get: + operationId: strings + parameters: [] + responses: + '200': + description: The request has succeeded. + content: + application/json: + schema: + type: object + properties: + string5to10: + type: string + minLength: 5 + maxLength: 10 + atLeast3: + type: string + minLength: 3 + max7: + type: string + maxLength: 7 + required: + - string5to10 + - atLeast3 + - max7 +components: {} diff --git a/test/transform.spec.ts b/test/transform.spec.ts new file mode 100644 index 0000000..abeb6bc --- /dev/null +++ b/test/transform.spec.ts @@ -0,0 +1,34 @@ +import { describe, expect, it, vi } from 'vitest'; + +import { transformToGenerateResultFunctions } from '../src/transform'; +import { generateOperationCollection } from '../src/generate'; +import { getV3Doc } from '../src/swagger'; + +describe('transform:transformToGenerateResultFunctions', () => { + it('Generates a response function with epxected faker calls', async () => { + const apiDoc = await getV3Doc('./test/fixture/strings.yaml'); + const schema = generateOperationCollection(apiDoc, { output: '' }); + + const transform = transformToGenerateResultFunctions(schema, ''); + const src = transform.replace('export', '').replace('};', '}'); + + { + const faker = { + string: { + alpha: vi.fn(), + }, + }; + + const MAX_STRING_LENGTH = 897; + + // direct eval is safe here, we know the generated code + // and we need the local scope + const generateFunction = eval(`(${src})`); + const generatedResponse = generateFunction(); + + expect(faker.string.alpha).toHaveBeenNthCalledWith(1, { length: { min: 5, max: 10 } }); + expect(faker.string.alpha).toHaveBeenNthCalledWith(2, { length: { min: 3, max: MAX_STRING_LENGTH } }); + expect(faker.string.alpha).toHaveBeenNthCalledWith(3, { length: { min: 0, max: 7 } }); + } + }); +});