Skip to content

Commit

Permalink
fix(jest): fixes after review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkluijk committed Nov 20, 2018
1 parent 84d3265 commit 2b7d2a5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 45 deletions.
24 changes: 3 additions & 21 deletions projects/spectator/jest/src/mock.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Provider, Type } from '@angular/core';
import { CompatibleSpy, SpyObject as BaseSpyObject } from '@netbasal/spectator';
import { CompatibleSpy, SpyObject as BaseSpyObject, installProtoMethods } from '@netbasal/spectator';

export type SpyObject<T> = BaseSpyObject<T> & { [P in keyof T]: T[P] & jest.Mock<T> };

export function createSpyObject<T>(type: Type<T>): SpyObject<T> {
const mock: any = {};

function createGuinnessCompatibleSpy(name): CompatibleSpy {
installProtoMethods(mock, type.prototype, () => {
const jestFn = jest.fn();
const newSpy: CompatibleSpy = jestFn as any;

Expand All @@ -25,25 +25,7 @@ export function createSpyObject<T>(type: Type<T>): SpyObject<T> {
};

return newSpy;
}

function installProtoMethods(proto: any) {
if (proto === null || proto === Object.prototype) {
return;
}

for (const key of Object.getOwnPropertyNames(proto)) {
const descriptor = Object.getOwnPropertyDescriptor(proto, key);

if (typeof descriptor.value === 'function' && key !== 'constructor') {
mock[key] = createGuinnessCompatibleSpy(key);
}
}

installProtoMethods(Object.getPrototypeOf(proto));
}

installProtoMethods(type.prototype);
});

return mock;
}
Expand Down
4 changes: 2 additions & 2 deletions projects/spectator/src/lib/matchers-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ declare namespace jasmine {

toHaveProperty(prop: string, val: string | boolean): boolean;

toHaveText(text: string | ((_: string) => boolean)): boolean;
toHaveText(text: string | Function): boolean;

toHaveValue(value: string): boolean;

Expand Down Expand Up @@ -72,7 +72,7 @@ declare namespace jest {

toHaveProperty(prop: string, val: string | boolean): boolean;

toHaveText(text: string | ((_: string) => boolean)): boolean;
toHaveText(text: string | Function): boolean;

toHaveValue(value: string): boolean;

Expand Down
42 changes: 20 additions & 22 deletions projects/spectator/src/lib/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,36 @@ export interface CompatibleSpy extends jasmine.Spy {

export type SpyObject<T> = T & { [P in keyof T]: T[P] & CompatibleSpy };

export function installProtoMethods(mock: any, proto: any, createSpyFn: Function) {
if (proto === null || proto === Object.prototype) {
return;
}

for (const key of Object.getOwnPropertyNames(proto)) {
const descriptor = Object.getOwnPropertyDescriptor(proto, key);

if (typeof descriptor.value === 'function' && key !== 'constructor') {
mock[key] = createSpyFn(key);
}
}

installProtoMethods(mock, Object.getPrototypeOf(proto), createSpyFn);

return mock;
}

export function createSpyObject<T>(type: Type<T>): SpyObject<T> {
const mock: any = {};

function createGuinnessCompatibleSpy(name): CompatibleSpy {
return installProtoMethods(mock, type.prototype, name => {
const newSpy: CompatibleSpy = jasmine.createSpy(name) as any;
newSpy.andCallFake = <any>newSpy.and.callFake;
newSpy.andReturn = <any>newSpy.and.returnValue;
newSpy.reset = <any>newSpy.calls.reset;
// revisit return null here (previously needed for rtts_assert).
newSpy.and.returnValue(null);
return newSpy;
}

function installProtoMethods(proto: any) {
if (proto === null || proto === Object.prototype) {
return;
}

for (const key of Object.getOwnPropertyNames(proto)) {
const descriptor = Object.getOwnPropertyDescriptor(proto, key);

if (typeof descriptor.value === 'function' && key !== 'constructor') {
mock[key] = createGuinnessCompatibleSpy(key);
}
}

installProtoMethods(Object.getPrototypeOf(proto));
}

installProtoMethods(type.prototype);

return mock;
});
}

export function mockProvider<T>(type: Type<T>): Provider {
Expand Down

0 comments on commit 2b7d2a5

Please sign in to comment.