1- import { fileURLToPath } from "node:url" ;
21import { transformSchema } from "../../../src/index.js" ;
32import { astToString } from "../../../src/lib/ts.js" ;
4- import type { GlobalContext } from "../../../src/types.js" ;
53import { DEFAULT_CTX , type TestCase } from "../../test-helpers.js" ;
64
7- const DEFAULT_OPTIONS = DEFAULT_CTX ;
8-
9- const schema = {
10- openapi : "3.0.0" ,
11- info : {
12- title : "Status API" ,
13- version : "1.0.0" ,
14- } ,
15- paths : {
16- "/status" : {
17- get : {
18- summary : "Get current status" ,
19- responses : {
20- "200" : {
21- description : "Status response" ,
22- content : {
23- "application/json" : {
24- schema : {
25- type : "object" ,
26- properties : {
27- required : {
28- "status" : true ,
29- "statusEnum" : true ,
30- } ,
31- status : {
32- $ref : "#/components/schemas/StatusResponse" ,
33- } ,
34- statusEnum : {
35- $ref : "#/components/schemas/StatusEnumResponse" ,
36- }
37- }
38- } ,
39- } ,
40- } ,
41- } ,
42- } ,
43- } ,
44- } ,
45- } ,
46- components : {
47- schemas : {
48- StatusResponse : {
49- type : "object" ,
50- properties : {
51- status : {
52- $ref : "#/components/schemas/Status" ,
53- } ,
54- } ,
55- } ,
56- Status : {
57- type : "string" ,
58- enum : [ "pending" , "active" , "done" ] ,
59- } ,
60- StatusEnumResponse : {
61- type : "object" ,
62- properties : {
63- status : {
64- $ref : "#/components/schemas/StatusEnum" ,
65- } ,
66- } ,
67- } ,
68- StatusEnum : {
69- type : "string" ,
70- enum : [ "pending" , "active" , "done" ] ,
71- "x-enum-varnames" : [ "Pending" , "Active" , "Done" ] ,
72- "x-enum-descriptions" : [
73- "The task is pending" ,
74- "The task is active" ,
75- "The task is done" ,
76- ] ,
77- } ,
78- } ,
79- } ,
80- } ;
81-
82- describe ( "transformComponentsObject" , ( ) => {
83- const tests : TestCase < any , GlobalContext > [ ] = [
84- [
85- "options > enum: true and conditionalEnums: false" ,
86- {
87- given : schema ,
88- want : `export interface paths {
5+ const tests : TestCase [ ] = [
6+ [
7+ "options > enum: true and conditionalEnums: false" ,
8+ {
9+ given : mockSchema ( ) ,
10+ want : `export interface paths {
8911 "/status": {
9012 parameters: {
9113 query?: never;
@@ -162,14 +84,14 @@ export enum StatusEnum {
16284 Done = "done"
16385}
16486export type operations = Record<string, never>;` ,
165- options : { ... DEFAULT_OPTIONS , enum : true , conditionalEnums : false } ,
166- } ,
167- ] ,
168- [
169- "options > enum: true and conditionalEnums: true" ,
170- {
171- given : schema ,
172- want : `export interface paths {
87+ options : { ctx : createTestContext ( { enum : true , conditionalEnums : false } ) } ,
88+ } ,
89+ ] ,
90+ [
91+ "options > enum: true and conditionalEnums: true" ,
92+ {
93+ given : mockSchema ( ) ,
94+ want : `export interface paths {
17395 "/status": {
17496 parameters: {
17597 query?: never;
@@ -232,19 +154,6 @@ export interface components {
232154 pathItems: never;
233155}
234156export type $defs = Record<string, never>;
235- export enum Status {
236- pending = "pending",
237- active = "active",
238- done = "done"
239- }
240- export enum StatusEnum {
241- // The task is pending
242- Pending = "pending",
243- // The task is active
244- Active = "active",
245- // The task is done
246- Done = "done"
247- }
248157export enum StatusEnum {
249158 // The task is pending
250159 Pending = "pending",
@@ -254,23 +163,105 @@ export enum StatusEnum {
254163 Done = "done"
255164}
256165export type operations = Record<string, never>;` ,
257- options : { ... DEFAULT_OPTIONS , enum : true , conditionalEnums : true } ,
258- } ,
259- ] ,
260- ] ;
166+ options : { ctx : createTestContext ( { enum : true , conditionalEnums : true } ) } ,
167+ } ,
168+ ] ,
169+ ] ;
261170
262- for ( const [ testName , { given, want, options, ci } ] of tests ) {
171+ describe ( "transformComponentsObject" , ( ) => {
172+ describe . each ( tests ) ( "Case: %s" , ( name , { given, want, options, ci } ) => {
263173 test . skipIf ( ci ?. skipIf ) (
264- testName ,
174+ "it matches the snapshot" ,
265175 async ( ) => {
266- const result = astToString ( transformSchema ( given , options ?? DEFAULT_OPTIONS ) ) ;
267- if ( want instanceof URL ) {
268- await expect ( result ) . toMatchFileSnapshot ( fileURLToPath ( want ) ) ;
269- } else {
270- expect ( result . trim ( ) ) . toBe ( want . trim ( ) ) ;
271- }
176+ assert ( typeof want === "string" ) ;
177+ const result = astToString ( transformSchema ( given , options ?. ctx ?? DEFAULT_CTX ) , { fileName : name } ) ;
178+ expect ( result . trim ( ) ) . toBe ( want . trim ( ) ) ;
272179 } ,
273180 ci ?. timeout ,
274181 ) ;
275- }
182+ } ) ;
276183} ) ;
184+
185+ function mockSchema ( ) {
186+ return {
187+ openapi : "3.0.0" ,
188+ info : {
189+ title : "Status API" ,
190+ version : "1.0.0" ,
191+ } ,
192+ paths : {
193+ "/status" : {
194+ get : {
195+ summary : "Get current status" ,
196+ responses : {
197+ "200" : {
198+ description : "Status response" ,
199+ content : {
200+ "application/json" : {
201+ schema : {
202+ type : "object" ,
203+ properties : {
204+ required : {
205+ status : true ,
206+ statusEnum : true ,
207+ } ,
208+ status : {
209+ $ref : "#/components/schemas/StatusResponse" ,
210+ } ,
211+ statusEnum : {
212+ $ref : "#/components/schemas/StatusEnumResponse" ,
213+ } ,
214+ } ,
215+ } ,
216+ } ,
217+ } ,
218+ } ,
219+ } ,
220+ } ,
221+ } ,
222+ } ,
223+ components : {
224+ schemas : {
225+ StatusResponse : {
226+ type : "object" ,
227+ properties : {
228+ status : {
229+ $ref : "#/components/schemas/Status" ,
230+ } ,
231+ } ,
232+ } ,
233+ Status : {
234+ type : "string" ,
235+ enum : [ "pending" , "active" , "done" ] ,
236+ } ,
237+ StatusEnumResponse : {
238+ type : "object" ,
239+ properties : {
240+ status : {
241+ $ref : "#/components/schemas/StatusEnum" ,
242+ } ,
243+ } ,
244+ } ,
245+ StatusEnum : {
246+ type : "string" ,
247+ enum : [ "pending" , "active" , "done" ] ,
248+ "x-enum-varnames" : [ "Pending" , "Active" , "Done" ] ,
249+ "x-enum-descriptions" : [ "The task is pending" , "The task is active" , "The task is done" ] ,
250+ } ,
251+ } ,
252+ } ,
253+ } ;
254+ }
255+
256+ function createTestContext ( overrides : Partial < typeof DEFAULT_CTX > = { } ) {
257+ return {
258+ ...DEFAULT_CTX ,
259+ ...overrides ,
260+ // Deep copy mutable properties to avoid scope pollution
261+ discriminators : {
262+ objects : { } ,
263+ refsHandled : [ ] ,
264+ } ,
265+ injectFooter : [ ] ,
266+ } ;
267+ }
0 commit comments