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(union): ensure union type with null or unknown will mock the first type #322

Merged
merged 1 commit into from
May 13, 2020
Merged
Show file tree
Hide file tree
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
5 changes: 1 addition & 4 deletions src/transformer/descriptor/union/union.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
39 changes: 27 additions & 12 deletions test/transformer/descriptor/union/union.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createMock } from 'ts-auto-mock';
import { ImportInterface } from '../utils/interfaces/importInterface';

describe('for union', () => {
interface Interface {
Expand All @@ -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<MyClass>();
expect(properties.test).toBeUndefined();
expect(properties.test2).toBeUndefined();
expect(properties.test3).toBeUndefined();
expect(properties.test4).toBeUndefined();
expect(properties.test5).toBeUndefined();
expect(properties.test).toBe('');
});
});

Expand Down Expand Up @@ -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<Interface>();
expect(properties.a).toBe('');
});
});

describe('union type unknown', () => {
interface Interface {
a: string | unknown;
}

it('should set the first type', () => {
const properties: Interface = createMock<Interface>();
expect(properties.a).toBe('');
});
});
});
67 changes: 28 additions & 39 deletions test/transformer/descriptor/union/unionOptional.test.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Expand All @@ -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<MyClass>();
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', () => {
Expand All @@ -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', () => {
Expand All @@ -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<MyClass>();
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<MyClass>();
expect(properties.test).toBeUndefined();
});
it('should not set the value', () => {
const properties: MyClass = createMock<MyClass>();
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<MyClass>();
expect(properties.test).toBeUndefined();
});
it('should not set the value', () => {
const properties: MyClass = createMock<MyClass>();
expect(properties.test).toBeUndefined();
});
});
});