Skip to content

Commit

Permalink
fix(properties): create property when undefined/void, do not emit pro…
Browse files Browse the repository at this point in the history
…perty only when optional
  • Loading branch information
Pmyl committed Sep 15, 2020
1 parent 3a11e36 commit 70d65ed
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 24 deletions.
4 changes: 4 additions & 0 deletions src/transformer/descriptor/mock/mockProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ export function GetMockPropertiesFromDeclarations(
return false;
}

if (member.questionToken) {
return false;
}

if (!modifiers) {
return true;
}
Expand Down
4 changes: 0 additions & 4 deletions src/transformer/descriptor/mock/mockPropertiesAssignments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ export function GetMockPropertiesAssignments(
(acc: PropertyAssignments, member: PropertyLike): PropertyAssignments => {
const descriptor: ts.Expression = GetDescriptor(member, scope);

if (descriptor.kind === ts.SyntaxKind.VoidExpression) {
return acc;
}

if (ts.isCallLikeExpression(descriptor)) {
acc.lazy.push(GetLazyMockProperty(descriptor, member));
} else {
Expand Down
5 changes: 0 additions & 5 deletions src/transformer/descriptor/property/propertySignature.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as ts from 'typescript';
import { Scope } from '../../scope/scope';
import { GetDescriptor } from '../descriptor';
import { GetUndefinedDescriptor } from '../undefined/undefined';
import { PropertySignatureCache } from './cache';

type PropertyNode = ts.PropertySignature | ts.PropertyDeclaration;
Expand All @@ -13,10 +12,6 @@ export function GetPropertyDescriptor(
PropertySignatureCache.instance.set(node.name);

if (node.type) {
if (node.questionToken) {
return GetUndefinedDescriptor();
}

return GetDescriptor(node.type, scope);
}

Expand Down
8 changes: 5 additions & 3 deletions test/transformer/descriptor/optional/optional.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { createMock } from 'ts-auto-mock';

describe('optional', () => {
class MyClass {
public test?: string;
public testOptional?: string;
public testUndefined: undefined;
}

it('should not set the value', () => {
it('should not set the value of the optional property', () => {
const properties: MyClass = createMock<MyClass>();
expect(properties.test).toBeUndefined();
expect(properties.hasOwnProperty('testOptional')).toBe(false);
expect(properties.hasOwnProperty('testUndefined')).toBe(true);
});
});
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { createMock } from 'ts-auto-mock';
import { ImportType } from '../utils/types/type';

describe('union optional', () => {
describe('union undefined', () => {
describe('type reference', () => {
type Type = void;

class MyClass {
public test: string | Type;
}

it('should not set the value', () => {
it('should set undefined', () => {
const properties: MyClass = createMock<MyClass>();
expect(properties.test).toBeUndefined();
});
Expand All @@ -20,7 +20,7 @@ describe('union optional', () => {
public test: string | ImportType;
}

it('should not set the value', () => {
it('should set undefined', () => {
const properties: MyClass = createMock<MyClass>();
expect(properties.test).toBeUndefined();
});
Expand All @@ -31,7 +31,7 @@ describe('union optional', () => {
public test: string | { a: string } | undefined;
}

it('should not set the value', () => {
it('should set undefined', () => {
const properties: MyClass = createMock<MyClass>();
expect(properties.test).toBeUndefined();
});
Expand All @@ -42,7 +42,7 @@ describe('union optional', () => {
public test: '2' | undefined;
}

it('should not set the value', () => {
it('should set undefined', () => {
const properties: MyClass = createMock<MyClass>();
expect(properties.test).toBeUndefined();
});
Expand All @@ -55,33 +55,33 @@ describe('union optional', () => {
public test: TypeOptional | number;
}

it('should not set the value', () => {
it('should set undefined', () => {
const properties: MyClass = createMock<MyClass>();
expect(properties.test).toBeUndefined();
});
});

describe('type reference optional as second type', () => {
describe('type reference void as second type', () => {
type TypeOptional = string | void;

class MyClass {
public test: number | TypeOptional;
}

it('should not set the value', () => {
it('should set undefined', () => {
const properties: MyClass = createMock<MyClass>();
expect(properties.test).toBeUndefined();
});
});

describe('type reference optional and extends', () => {
describe('type reference void and extends', () => {
type TypeOptional = ({ a: string } & { b: number }) | void;

class MyClass {
public test: number | TypeOptional;
}

it('should not set the value', () => {
it('should set undefined', () => {
const properties: MyClass = createMock<MyClass>();
expect(properties.test).toBeUndefined();
});
Expand Down
14 changes: 12 additions & 2 deletions ui/src/views/types-supported.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,13 @@ mock // { a: "" }
```
## Optional
The property is not defined in the mock
```ts
class MyClass {
test?: string;
}
const mock = createMock<MyClass>();
mock // { test: undefined }
mock // { }
```
## Typescript libraries (Array, Number... ecc)
Expand All @@ -188,14 +189,23 @@ const mock = createMock<MyClass>();
mock // { tuple: ['', 0] }
```
## Union (it will convert to the first type)
## Union
It will convert to the first type of the union unless undefined/void are part of the union,
in that case it will convert to undefined, being the "smallest type"
```ts
class MyClass {
union: string | number;
}
const mock = createMock<MyClass>();
mock // { union: "" }
class MyClass1 {
union: string | undefined;
}
const mock1 = createMock<MyClass1>();
mock1 // { union: undefined }
```
## Dictionary
Expand Down

0 comments on commit 70d65ed

Please sign in to comment.