Skip to content

Commit 150f49e

Browse files
AllieJonssonAlfred Jonsson
and
Alfred Jonsson
authoredMar 19, 2024
fix(msw): return mock as array of enums if array is the correct type (#1272)
Co-authored-by: Alfred Jonsson <alfred.jonsson@decerno.se>
1 parent ee01104 commit 150f49e

File tree

3 files changed

+148
-1
lines changed

3 files changed

+148
-1
lines changed
 

‎packages/mock/src/faker/getters/scalar.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,9 @@ export const getMockScalar = ({
232232
];
233233
}
234234

235-
value = `faker.helpers.arrayElement(${enumValue})`;
235+
value = item.path?.endsWith('[]')
236+
? `faker.helpers.arrayElements(${enumValue})`
237+
: `faker.helpers.arrayElement(${enumValue})`;
236238
}
237239

238240
return {

‎tests/configs/swr.config.ts

+11
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,15 @@ export default defineConfig({
156156
target: '../specifications/arrays.yaml',
157157
},
158158
},
159+
enums: {
160+
output: {
161+
target: '../generated/swr/enums/endpoints.ts',
162+
schemas: '../generated/swr/enums/model',
163+
client: 'swr',
164+
mock: true,
165+
},
166+
input: {
167+
target: '../specifications/enums.yaml',
168+
},
169+
},
159170
});

‎tests/specifications/enums.yaml

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
openapi: 3.0.3
2+
info:
3+
title: Arrays
4+
version: 1.0.0
5+
paths:
6+
/api/cat:
7+
get:
8+
summary: sample cat
9+
responses:
10+
'200':
11+
description: OK
12+
content:
13+
application/json:
14+
schema:
15+
$ref: '#/components/schemas/Cat'
16+
/api/required-cat:
17+
get:
18+
summary: sample required cat
19+
responses:
20+
'200':
21+
description: OK
22+
content:
23+
application/json:
24+
schema:
25+
$ref: '#/components/schemas/RequiredCat'
26+
/api/dog:
27+
get:
28+
summary: sample dog
29+
responses:
30+
'200':
31+
description: OK
32+
content:
33+
application/json:
34+
schema:
35+
$ref: '#/components/schemas/Dog'
36+
/api/required-dog:
37+
get:
38+
summary: sample required dog
39+
responses:
40+
'200':
41+
description: OK
42+
content:
43+
application/json:
44+
schema:
45+
$ref: '#/components/schemas/RequiredDog'
46+
/api/duck:
47+
get:
48+
summary: sample duck
49+
responses:
50+
'200':
51+
description: OK
52+
content:
53+
application/json:
54+
schema:
55+
$ref: '#/components/schemas/Duck'
56+
components:
57+
schemas:
58+
Siamese:
59+
type: object
60+
properties:
61+
colours:
62+
type: array
63+
items:
64+
type: string
65+
enum:
66+
- BLACK
67+
- BROWN
68+
- WHITE
69+
- GREY
70+
RequiredSiamese:
71+
type: object
72+
required: ['colours']
73+
properties:
74+
colours:
75+
type: array
76+
items:
77+
type: string
78+
enum:
79+
- BLACK
80+
- BROWN
81+
- WHITE
82+
- GREY
83+
Cat:
84+
type: object
85+
properties:
86+
petsRequested:
87+
type: array
88+
items:
89+
$ref: '#/components/schemas/Siamese'
90+
RequiredCat:
91+
type: object
92+
properties:
93+
petsRequested:
94+
type: array
95+
items:
96+
$ref: '#/components/schemas/RequiredSiamese'
97+
Bulldog:
98+
type: object
99+
properties:
100+
colour:
101+
type: string
102+
enum:
103+
- BLACK
104+
- BROWN
105+
RequiredBulldog:
106+
type: object
107+
required: ['colour']
108+
properties:
109+
colour:
110+
type: string
111+
enum:
112+
- BLACK
113+
- BROWN
114+
Dog:
115+
type: object
116+
properties:
117+
petsRequested:
118+
type: array
119+
items:
120+
$ref: '#/components/schemas/Bulldog'
121+
RequiredDog:
122+
type: object
123+
properties:
124+
petsRequested:
125+
type: array
126+
items:
127+
$ref: '#/components/schemas/RequiredBulldog'
128+
Duck:
129+
type: object
130+
properties:
131+
petsRequested:
132+
type: array
133+
items:
134+
type: string

0 commit comments

Comments
 (0)
Please sign in to comment.