From 24d6dbdb2e00a73dff9d59d15352d736f932ada2 Mon Sep 17 00:00:00 2001 From: Vittorio Date: Wed, 13 May 2020 17:41:06 +0100 Subject: [PATCH] fix(union): ensure union type with null or unknown will mock the first type --- src/transformer/descriptor/union/union.ts | 5 +- .../descriptor/union/union.test.ts | 39 +++++++---- .../descriptor/union/unionOptional.test.ts | 67 ++++++++----------- 3 files changed, 56 insertions(+), 55 deletions(-) diff --git a/src/transformer/descriptor/union/union.ts b/src/transformer/descriptor/union/union.ts index 7044028ab..559768df5 100644 --- a/src/transformer/descriptor/union/union.ts +++ b/src/transformer/descriptor/union/union.ts @@ -17,8 +17,5 @@ export function GetUnionDescriptor(node: ts.UnionTypeNode, scope: Scope): ts.Exp } function isNotDefinedType(typeNode: ts.Node): boolean { - return typeNode.kind === ts.SyntaxKind.VoidKeyword - || typeNode.kind === ts.SyntaxKind.NullKeyword - || typeNode.kind === ts.SyntaxKind.UnknownKeyword - || typeNode.kind === ts.SyntaxKind.UndefinedKeyword; + return typeNode.kind === ts.SyntaxKind.VoidKeyword || typeNode.kind === ts.SyntaxKind.UndefinedKeyword; } diff --git a/test/transformer/descriptor/union/union.test.ts b/test/transformer/descriptor/union/union.test.ts index 85d0f7b67..08b95fc9b 100644 --- a/test/transformer/descriptor/union/union.test.ts +++ b/test/transformer/descriptor/union/union.test.ts @@ -1,4 +1,5 @@ import { createMock } from 'ts-auto-mock'; +import { ImportInterface } from '../utils/interfaces/importInterface'; describe('for union', () => { interface Interface { @@ -10,22 +11,14 @@ describe('for union', () => { expect(properties.a).toBe(''); }); - describe('union optional', () => { + describe('type interface import', () => { class MyClass { - public test: string | void; - public test2: string | null; - public test3: string | unknown; - public test4: string | undefined; - public test5: undefined | string | number; + public test: string | ImportInterface; } - it('should not set the value', () => { + it('should set the value', () => { const properties: MyClass = createMock(); - expect(properties.test).toBeUndefined(); - expect(properties.test2).toBeUndefined(); - expect(properties.test3).toBeUndefined(); - expect(properties.test4).toBeUndefined(); - expect(properties.test5).toBeUndefined(); + expect(properties.test).toBe(''); }); }); @@ -69,4 +62,26 @@ describe('for union', () => { expect(properties.test).toEqual([]); }); }); + + describe('union type null', () => { + interface Interface { + a: string | null; + } + + it('should set the first type', () => { + const properties: Interface = createMock(); + expect(properties.a).toBe(''); + }); + }); + + describe('union type unknown', () => { + interface Interface { + a: string | unknown; + } + + it('should set the first type', () => { + const properties: Interface = createMock(); + expect(properties.a).toBe(''); + }); + }); }); diff --git a/test/transformer/descriptor/union/unionOptional.test.ts b/test/transformer/descriptor/union/unionOptional.test.ts index 1b2e0ce06..a8a8512fc 100644 --- a/test/transformer/descriptor/union/unionOptional.test.ts +++ b/test/transformer/descriptor/union/unionOptional.test.ts @@ -1,10 +1,10 @@ import { createMock } from 'ts-auto-mock'; -import { ImportInterface } from '../utils/interfaces/importInterface'; import { ImportType } from '../utils/types/type'; describe('union optional', () => { describe('type reference', () => { - type Type = null; + type Type = void; + class MyClass { public test: string | Type; } @@ -26,20 +26,20 @@ describe('union optional', () => { }); }); - describe('type interface import', () => { + describe('type object declared', () => { class MyClass { - public test: string | ImportInterface; + public test: string | { a: string } | undefined; } it('should not set the value', () => { const properties: MyClass = createMock(); - expect(properties.test).toBe(''); + expect(properties.test).toBeUndefined(); }); }); - describe('type object declared', () => { + describe('type declared value', () => { class MyClass { - public test: string | { a: string } | null; + public test: '2' | undefined; } it('should not set the value', () => { @@ -48,9 +48,11 @@ describe('union optional', () => { }); }); - describe('type declared value', () => { + describe('type reference optional', () => { + type TypeOptional = string | void; + class MyClass { - public test: '2' | null; + public test: TypeOptional | number; } it('should not set the value', () => { @@ -59,42 +61,29 @@ describe('union optional', () => { }); }); - describe('type reference optional', () => { - type TypeOptional = string | null; - - class MyClass { - public test: TypeOptional | number; - } - - it('should not set the value', () => { - const properties: MyClass = createMock(); - expect(properties.test).toBeUndefined(); - }); - }); - - describe('type reference optional second', () => { - type TypeOptional = string | null; + describe('type reference optional as second type', () => { + type TypeOptional = string | void; - class MyClass { - public test: number | TypeOptional; - } + class MyClass { + public test: number | TypeOptional; + } - it('should not set the value', () => { - const properties: MyClass = createMock(); - expect(properties.test).toBeUndefined(); - }); + it('should not set the value', () => { + const properties: MyClass = createMock(); + expect(properties.test).toBeUndefined(); + }); }); describe('type reference optional and extends', () => { - type TypeOptional = { a: string} & { b: number} | null; + type TypeOptional = { a: string } & { b: number } | void; - class MyClass { - public test: number | TypeOptional; - } + class MyClass { + public test: number | TypeOptional; + } - it('should not set the value', () => { - const properties: MyClass = createMock(); - expect(properties.test).toBeUndefined(); - }); + it('should not set the value', () => { + const properties: MyClass = createMock(); + expect(properties.test).toBeUndefined(); + }); }); });