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

[RFC] enhancement(extension,repository): Add hidden __ident property to mocks and functions to differentiate them in conditional branching #313

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
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);
}
}
7 changes: 5 additions & 2 deletions src/repository/repository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
type Factory = Function;
import { applyIdentityProperty } from '../utils/applyIdentityProperty';

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

export class Repository {
private readonly _repository: { [key: string]: Factory };
Expand All @@ -15,7 +18,7 @@ export class Repository {
}

public registerFactory(key: string, factory: Factory): void {
this._repository[key] = factory;
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;
},
},
);
}