Skip to content

Commit

Permalink
fix(union): ensure union type with null or unknown will mock the firs…
Browse files Browse the repository at this point in the history
…t type (#322)

BREAKING CHANGE: union types that resolve in null or unknown will now not be converted to undefined.

before
```ts
type = string | null // undefined
```

after
```ts
type = string | null // '' (empty string)
```
  • Loading branch information
uittorio authored May 13, 2020
1 parent a106e44 commit a2d58ad
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 55 deletions.
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();
});
});
});

0 comments on commit a2d58ad

Please sign in to comment.