Skip to content
This repository has been archived by the owner on Nov 10, 2022. It is now read-only.

Commit

Permalink
feat: update tests and NoopContextManager
Browse files Browse the repository at this point in the history
Leave checking special cases for the actual ContextManager and remove
the short-circuit from the API implementation.
  • Loading branch information
rauno56 committed Jun 2, 2021
1 parent 4e2d434 commit c31b6c2
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 8 deletions.
5 changes: 1 addition & 4 deletions src/api/context.ts
Expand Up @@ -83,10 +83,7 @@ export class ContextAPI {
* @param target function or event emitter to bind
*/
public bind<T>(context: Context, target: T): T {
if (context === undefined) {
return target;
}
return this._getContextManager().bind(target, context);
return this._getContextManager().bind(context, target);
}

private _getContextManager(): ContextManager {
Expand Down
2 changes: 1 addition & 1 deletion src/context/NoopContextManager.ts
Expand Up @@ -31,7 +31,7 @@ export class NoopContextManager implements types.ContextManager {
return fn.call(thisArg, ...args);
}

bind<T>(target: T, _context?: types.Context): T {
bind<T>(_context: types.Context, target: T): T {
return target;
}

Expand Down
2 changes: 1 addition & 1 deletion src/context/types.ts
Expand Up @@ -65,7 +65,7 @@ export interface ContextManager {
* @param target Any object to which a context need to be set
* @param [context] Optionally specify the context which you want to assign
*/
bind<T>(target: T, context?: Context): T;
bind<T>(context: Context, target: T): T;

/**
* Enable context management
Expand Down
4 changes: 2 additions & 2 deletions test/context/NoopContextManager.test.ts
Expand Up @@ -119,13 +119,13 @@ describe('NoopContextManager', () => {
describe('.bind()', () => {
it('should return the same target (when enabled)', () => {
const test = { a: 1 };
assert.deepStrictEqual(contextManager.bind(test), test);
assert.deepStrictEqual(contextManager.bind(contextManager.active(), test), test);
});

it('should return the same target (when disabled)', () => {
contextManager.disable();
const test = { a: 1 };
assert.deepStrictEqual(contextManager.bind(test), test);
assert.deepStrictEqual(contextManager.bind(contextManager.active(), test), test);
contextManager.enable();
});
});
Expand Down

0 comments on commit c31b6c2

Please sign in to comment.