-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Matthijs van Henten <[email protected]>
- Loading branch information
Showing
3 changed files
with
73 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 } }); | ||
} | ||
}); | ||
}); |