Skip to content

Commit

Permalink
chore(*): Rename __factory identifier to __ident and apply it to mock…
Browse files Browse the repository at this point in the history
…ed methods as well
  • Loading branch information
martinjlowm committed May 16, 2020
1 parent ef93ab5 commit aba964b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 31 deletions.
7 changes: 6 additions & 1 deletion src/extension/method/provider/provider.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { applyIdentityProperty } from '../../../utils/applyIdentityProperty';
import { functionMethod } from './functionMethod';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Method = (name: string, value: any) => () => any;
Expand Down Expand Up @@ -45,6 +46,10 @@ export class Provider {
return this._method(name, value());
}

return this._method(name, value);
// FIXME: Do this smarter, it's a bit counter intuitive to return a new
// proxy every single time this function is called. It should probably mock
// based on name if that ends up being a string representing the type
// signature.
return applyIdentityProperty(this._method, name)(name, value);
}
}
33 changes: 3 additions & 30 deletions src/repository/repository.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { applyIdentityProperty } from '../utils/applyIdentityProperty';

// eslint-disable-next-line
type Factory = (...args: any[]) => any;

Expand All @@ -16,36 +18,7 @@ export class Repository {
}

public registerFactory(key: string, factory: Factory): void {
const proxy: Factory = new Proxy(
factory,
{
apply(target: Factory, _this: unknown, args: Parameters<Factory>): ReturnType<Factory> {
const mock: ReturnType<Factory> = target(...args);

if (typeof mock === 'undefined') {
return;
}

if (!(mock instanceof Object)) {
return mock;
}

if (typeof mock.__factory !== 'undefined') {
return mock;
}

Object.defineProperty(mock, '__factory', {
enumerable: false,
writable: false,
value: key,
});

return mock;
},
},
);

this._repository[key] = proxy;
this._repository[key] = applyIdentityProperty(factory, key);
}

public getFactory(key: string): Factory {
Expand Down
34 changes: 34 additions & 0 deletions src/utils/applyIdentityProperty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// eslint-disable-next-line
type Function<K> = (...args: any[]) => K;
type IdentityFlavored<K> = K & { __ident?: string };

export function applyIdentityProperty<K extends object, T extends Function<K>>(target: T, identity: string): T {
return new Proxy(
target,
{
apply(func: T, _this: unknown, args: Parameters<T>): IdentityFlavored<K> | undefined {
const t: IdentityFlavored<K> = func(...args);

if (typeof t === 'undefined') {
return;
}

if (!(t instanceof Object)) {
return t;
}

if (typeof t.__ident !== 'undefined') {
return t;
}

Object.defineProperty(t, '__ident', {
enumerable: false,
writable: false,
value: identity,
});

return t;
},
},
);
}

0 comments on commit aba964b

Please sign in to comment.