Skip to content

Commit

Permalink
fix(msw): return mock as array of enums if array is the correct type (#…
Browse files Browse the repository at this point in the history
…1272)

Co-authored-by: Alfred Jonsson <[email protected]>
  • Loading branch information
AllieJonsson and Alfred Jonsson authored Mar 19, 2024
1 parent ee01104 commit 150f49e
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 1 deletion.
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

0 comments on commit 150f49e

Please sign in to comment.