Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types): tests for nested enums implementation #36

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 73 additions & 9 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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',
Expand Down
Loading