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

feat(intersectionsfunction): add support for function intersections #127

Merged
merged 3 commits into from
Dec 31, 2019
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
43 changes: 12 additions & 31 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/transformer/descriptor/descriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { GetMockPropertiesFromSymbol } from './mock/mockProperties';
import { GetNullDescriptor } from './null/null';
import { GetNumberDescriptor } from './number/number';
import { GetObjectLiteralDescriptor } from './objectLiteral/objectLiteral';
import { GetParenthesizedDescriptor } from './parenthesized/parenthesized';
import { GetPropertyDescriptor } from './property/propertySignature';
import { GetStringDescriptor } from './string/string';
import { GetTypeAliasDescriptor } from './typeAlias/typeAlias';
Expand Down Expand Up @@ -94,6 +95,8 @@ export function GetDescriptor(node: ts.Node, scope: Scope): ts.Expression {
return GetEnumDeclarationDescriptor(node as ts.EnumDeclaration);
case ts.SyntaxKind.MappedType:
return GetMappedDescriptor(node as ts.MappedTypeNode, scope);
case ts.SyntaxKind.ParenthesizedType:
return GetParenthesizedDescriptor(node as ts.ParenthesizedTypeNode, scope);
case ts.SyntaxKind.ArrayType:
case ts.SyntaxKind.TupleType:
return GetArrayDescriptor();
Expand Down
7 changes: 7 additions & 0 deletions src/transformer/descriptor/parenthesized/parenthesized.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as ts from 'typescript';
import { Scope } from '../../scope/scope';
import { GetProperties } from '../properties/properties';

export function GetParenthesizedDescriptor(node: ts.ParenthesizedTypeNode, scope: Scope): ts.Expression {
return GetProperties(node.type, scope);
}
4 changes: 2 additions & 2 deletions src/transformer/descriptor/properties/properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export function GetProperties(node: ts.Node, scope: Scope): ts.Expression {
const type: ts.Type = typeChecker.getTypeAtLocation(node);
const symbols: ts.Symbol[] = typeChecker.getPropertiesOfType(type);

if (!symbols.length) {
return GetPropertiesFromMembers(node as ts.TypeLiteralNode, scope);
if (!symbols.length && ts.isTypeLiteralNode(node)) {
return GetPropertiesFromMembers(node, scope);
} else {
const signatures: Array<ts.Signature> = [];

Expand Down
4 changes: 4 additions & 0 deletions src/transformer/descriptor/type/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,9 @@ export function GetType(node: ts.Node, scope: Scope): ts.Node {
return GetType(node.type, scope);
}

if (ts.isParenthesizedTypeNode(node)) {
return GetType(node.type, scope);
}

return node;
}
17 changes: 17 additions & 0 deletions test/transformer/descriptor/intersection/functions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { createMock } from 'ts-auto-mock';

describe('functions', () => {
it('should assign the first function', () => {
type A = () => number;

type B = () => string;

interface C {
prop: A & B;
}

const mock: C = createMock<C>();

expect(mock.prop()).toBe(0);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { createMock } from 'ts-auto-mock';

describe('interface without properties and with methods', () => {
it('should assign the first method', () => {
interface A {
// tslint:disable-next-line
(): string;
}

interface B {
// tslint:disable-next-line
(): number;
}

interface C {
prop: A & B;
}

const mock: C = createMock<C>();

expect(mock.prop()).toBe('');
});
});
32 changes: 32 additions & 0 deletions test/transformer/descriptor/parenthesized/intersection.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { createMock } from 'ts-auto-mock';

describe('parenthesized intersection ', () => {
it('should return undefined for primitive and object ', () => {
interface B {}

type A = (string) & B;

expect(createMock<A>()).toBeUndefined();
});

it('should intersect correctly for function and object ', () => {
interface B {
prop: number;
}

type A = (() => string) & B;

const mock: A = createMock<A>();
expect(mock()).toBe('');
expect(mock.prop).toBe(0);
});

it('should return the correct type for objects intersections', () => {
type A = ({a: string} & {b: number});

const mock: A = createMock<A>();

expect(mock.a).toBe('');
expect(mock.b).toBe(0);
});
});
19 changes: 19 additions & 0 deletions test/transformer/descriptor/parenthesized/type.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { createMock } from 'ts-auto-mock';

describe('parenthesized type ', () => {
it('should return the correct type for functions', () => {
type A = (() => string);

const mock: A = createMock<A>();

expect(mock()).toBe('');
});

it('should return the correct type for objects', () => {
type A = ({a: string});

const mock: A = createMock<A>();

expect(mock.a).toBe('');
});
});