Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(msw): return mock as array of enums if array is the correct type #1272

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/mock/src/faker/getters/scalar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,9 @@ export const getMockScalar = ({
];
}

value = `faker.helpers.arrayElement(${enumValue})`;
value = item.path?.endsWith('[]')
? `faker.helpers.arrayElements(${enumValue})`
: `faker.helpers.arrayElement(${enumValue})`;
}

return {
Expand Down
11 changes: 11 additions & 0 deletions tests/configs/swr.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,15 @@ export default defineConfig({
target: '../specifications/arrays.yaml',
},
},
enums: {
output: {
target: '../generated/swr/enums/endpoints.ts',
schemas: '../generated/swr/enums/model',
client: 'swr',
mock: true,
},
input: {
target: '../specifications/enums.yaml',
},
},
});
134 changes: 134 additions & 0 deletions tests/specifications/enums.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
openapi: 3.0.3
info:
title: Arrays
version: 1.0.0
paths:
/api/cat:
get:
summary: sample cat
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Cat'
/api/required-cat:
get:
summary: sample required cat
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/RequiredCat'
/api/dog:
get:
summary: sample dog
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Dog'
/api/required-dog:
get:
summary: sample required dog
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/RequiredDog'
/api/duck:
get:
summary: sample duck
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Duck'
components:
schemas:
Siamese:
type: object
properties:
colours:
type: array
items:
type: string
enum:
- BLACK
- BROWN
- WHITE
- GREY
RequiredSiamese:
type: object
required: ['colours']
properties:
colours:
type: array
items:
type: string
enum:
- BLACK
- BROWN
- WHITE
- GREY
Cat:
type: object
properties:
petsRequested:
type: array
items:
$ref: '#/components/schemas/Siamese'
RequiredCat:
type: object
properties:
petsRequested:
type: array
items:
$ref: '#/components/schemas/RequiredSiamese'
Bulldog:
type: object
properties:
colour:
type: string
enum:
- BLACK
- BROWN
RequiredBulldog:
type: object
required: ['colour']
properties:
colour:
type: string
enum:
- BLACK
- BROWN
Dog:
type: object
properties:
petsRequested:
type: array
items:
$ref: '#/components/schemas/Bulldog'
RequiredDog:
type: object
properties:
petsRequested:
type: array
items:
$ref: '#/components/schemas/RequiredBulldog'
Duck:
type: object
properties:
petsRequested:
type: array
items:
type: string