Skip to content

Commit f580f5d

Browse files
authoredJun 14, 2024
fix(msw): correctly add imports for enum references (#1456)
* fix: add import for enum reference, fixes #1455 * fix: add tests to import enum reference
1 parent da1f789 commit f580f5d

File tree

3 files changed

+132
-4
lines changed

3 files changed

+132
-4
lines changed
 

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,18 @@ export const getMockScalar = ({
115115
`faker.number.int({min: ${item.minimum}, max: ${item.maximum}})`,
116116
item.nullable,
117117
);
118+
let numberImports: GeneratorImport[] = [];
118119
if (item.enum) {
119120
// By default the value isn't a reference, so we don't have the object explicitly defined.
120121
// So we have to create an array with the enum values and force them to be a const.
121-
const joindEnumValues = item.enum.filter(Boolean).join(',');
122+
const joinedEnumValues = item.enum.filter(Boolean).join(',');
122123

123-
let enumValue = `[${joindEnumValues}] as const`;
124+
let enumValue = `[${joinedEnumValues}] as const`;
124125

125126
// But if the value is a reference, we can use the object directly via the imports and using Object.values.
126127
if (item.isRef) {
127128
enumValue = `Object.values(${item.name})`;
128-
imports = [
129+
numberImports = [
129130
{
130131
name: item.name,
131132
values: true,
@@ -142,7 +143,7 @@ export const getMockScalar = ({
142143
}
143144
return {
144145
value,
145-
imports: [],
146+
imports: numberImports,
146147
name: item.name,
147148
};
148149
}

‎tests/configs/mock.config.ts

+12
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,16 @@ export default defineConfig({
114114
target: '../specifications/null-type.yaml',
115115
},
116116
},
117+
enumRefs: {
118+
output: {
119+
mode: 'tags',
120+
schemas: '../generated/mock/enumRefs/model',
121+
target: '../generated/mock/enumRefs/endpoints.ts',
122+
client: 'axios',
123+
mock: true,
124+
},
125+
input: {
126+
target: '../specifications/enum-refs.yaml',
127+
},
128+
},
117129
});

‎tests/specifications/enum-refs.yaml

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
openapi: 3.0.1
2+
info:
3+
title: Sample API
4+
description: 'A sample api'
5+
version: '1.0'
6+
paths:
7+
'/sampleApi':
8+
get:
9+
tags:
10+
- Sample
11+
responses:
12+
'200':
13+
description: Success
14+
content:
15+
application/json:
16+
schema:
17+
$ref: '#/components/schemas/SampleStepsResponse'
18+
components:
19+
schemas:
20+
SampleAccountStatus:
21+
enum:
22+
- 0
23+
- 1
24+
- 2
25+
- 3
26+
type: integer
27+
format: int32
28+
x-enumNames:
29+
- Unknown
30+
- NotStarted
31+
- InProgress
32+
- Completed
33+
SampleStepResponse:
34+
type: object
35+
properties:
36+
stepKey:
37+
type: string
38+
nullable: true
39+
groupKey:
40+
type: string
41+
nullable: true
42+
displayName:
43+
type: string
44+
nullable: true
45+
description:
46+
type: string
47+
nullable: true
48+
staffInstructions:
49+
type: string
50+
nullable: true
51+
clientInstructions:
52+
type: string
53+
nullable: true
54+
lastModifiedOn:
55+
type: string
56+
format: date-time
57+
lastModifiedBy:
58+
type: string
59+
nullable: true
60+
stepStatus:
61+
$ref: '#/components/schemas/SampleStepStatus'
62+
statusChanges:
63+
type: array
64+
items:
65+
$ref: '#/components/schemas/SampleStepStatusChangeResponse'
66+
nullable: true
67+
additionalProperties: false
68+
SampleStepStatus:
69+
enum:
70+
- 0
71+
- 1
72+
- 2
73+
- 3
74+
- 4
75+
- 5
76+
- 6
77+
type: integer
78+
format: int32
79+
x-enumNames:
80+
- Unknown
81+
- NotStarted
82+
- InProgress
83+
- Completed
84+
- Skipped
85+
- Waiting
86+
- ReadyForReview
87+
SampleStepStatusChangeResponse:
88+
type: object
89+
properties:
90+
oldStatus:
91+
$ref: '#/components/schemas/SampleStepStatus'
92+
newStatus:
93+
$ref: '#/components/schemas/SampleStepStatus'
94+
stepKey:
95+
type: string
96+
nullable: true
97+
modifiedOn:
98+
type: string
99+
format: date-time
100+
modifiedBy:
101+
type: string
102+
nullable: true
103+
additionalProperties: false
104+
SampleStepsResponse:
105+
type: object
106+
properties:
107+
organizationId:
108+
type: string
109+
format: uuid
110+
steps:
111+
type: array
112+
items:
113+
$ref: '#/components/schemas/SampleStepResponse'
114+
nullable: true
115+
additionalProperties: false

0 commit comments

Comments
 (0)
Please sign in to comment.