From b11e8acfa1f5d5c94e0c5aeceb9d1c8f111a67dc Mon Sep 17 00:00:00 2001 From: Nicolas Froidure Date: Tue, 24 Sep 2024 15:50:54 +0200 Subject: [PATCH] fix(types): tests for nested enums implementation concerns #35 --- src/index.test.ts | 82 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 73 insertions(+), 9 deletions(-) diff --git a/src/index.test.ts b/src/index.test.ts index 4e7c596..885ca3a 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -376,15 +376,15 @@ describe('generateJSONSchemaTypes()', () => { }; expect( - toSource( - await generateJSONSchemaTypes(schema, { - brandedTypes: [], - generateRealEnums: true, - tuplesFromFixedArraysLengthLimit: 5, - exportNamespaces: true - }) - ) -).toMatchInlineSnapshot(` + toSource( + await generateJSONSchemaTypes(schema, { + brandedTypes: [], + generateRealEnums: true, + tuplesFromFixedArraysLengthLimit: 5, + exportNamespaces: true, + }), + ), + ).toMatchInlineSnapshot(` "export type Main = Enums.Limit; export namespace Enums { export enum Limit { @@ -1195,6 +1195,70 @@ describe('generateTypeDeclaration()', () => { `); }); + test.only('should work with reused enums in schemas', async () => { + const schema: JSONSchema7 = { + title: 'ReusedEnumTest', + type: 'object', + allOf: [ + { + type: 'object', + required: ['type'], + properties: { + type: { enum: ['type1', 'type2'] }, + }, + }, + { + oneOf: [ + { + type: 'object', + required: ['type'], + properties: { + type: { const: 'type1' }, + type1SpecificProp: { type: 'string' }, + }, + }, + { + type: 'object', + required: ['type'], + properties: { + type: { const: 'type2' }, + type2SpecificProp: { type: 'string' }, + }, + }, + ], + }, + ], + }; + + expect( + toSource( + await generateJSONSchemaTypes(schema, { + baseName: 'ReusedEnumTest', + generateRealEnums: true, + brandedTypes: [], + tuplesFromFixedArraysLengthLimit: 3, + exportNamespaces: false + }) + ) +).toMatchInlineSnapshot(` +"declare type ReusedEnumTest = {} & ({ + type: Enums.Type; +} & ({ + type: Enums.Type.Type1; + type1SpecificProp?: string; +} | { + type: Enums.Type.Type2; + type2SpecificProp?: string; +})); +declare namespace Enums { + export enum Type { + Type1 = "type1", + Type2 = "type2" + } +}" +`); + }); + test('should work with tuples and rest test case schemas', async () => { const schema: JSONSchema7 = { type: 'array',