Skip to content

Commit

Permalink
Merge e259f45 into 0ea29d8
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed Jan 17, 2020
2 parents 0ea29d8 + e259f45 commit 836452e
Show file tree
Hide file tree
Showing 10 changed files with 493 additions and 107 deletions.
Expand Up @@ -186,8 +186,8 @@ describe('Interceptor', () => {
}
}

// No listeners yet
expect(ctx.listenerCount('bind')).to.eql(0);
// No invocation context related listeners yet
const listenerCount = ctx.listenerCount('bind');
const controller = new MyController();

// Run the invocation 5 times
Expand All @@ -199,7 +199,7 @@ describe('Interceptor', () => {
'greet',
['John'],
);
// New listeners are added to `ctx`
// New listeners are added to `ctx` by the invocation context
expect(ctx.listenerCount('bind')).to.be.greaterThan(count);

// Wait until the invocation finishes
Expand All @@ -208,7 +208,7 @@ describe('Interceptor', () => {

// Listeners added by invocation context are gone now
// There is one left for ctx.observers
expect(ctx.listenerCount('bind')).to.eql(1);
expect(ctx.listenerCount('bind')).to.eql(listenerCount + 1);
});

it('invokes static interceptors', async () => {
Expand Down
49 changes: 48 additions & 1 deletion packages/context/src/__tests__/unit/binding-filter.unit.ts
Expand Up @@ -4,7 +4,7 @@
// License text available at https://opensource.org/licenses/MIT

import {expect} from '@loopback/testlab';
import {Binding, filterByKey, filterByTag} from '../..';
import {Binding, filterByKey, filterByTag, isBindingTagFilter} from '../..';

const key = 'foo';

Expand Down Expand Up @@ -72,6 +72,53 @@ describe('BindingFilter', () => {
});
});

describe('BindingTagFilter', () => {
it('allows tag name as string', () => {
const filter = filterByTag('controller');
expect(filter.bindingTagPattern).to.eql('controller');
});

it('allows tag name wildcard as string', () => {
const filter = filterByTag('controllers.*');
expect(filter.bindingTagPattern).to.eql(/^controllers\.[^.:]*$/);
});

it('allows tag name as regexp', () => {
const filter = filterByTag(/controller/);
expect(filter.bindingTagPattern).to.eql(/controller/);
});

it('allows tag name as map', () => {
const filter = filterByTag({controller: 'controller', rest: true});
expect(filter.bindingTagPattern).to.eql({
controller: 'controller',
rest: true,
});
});
});

describe('isBindingTagFilter', () => {
it('returns true for binding tag filter functions', () => {
const filter = filterByTag('controller');
expect(isBindingTagFilter(filter)).to.be.true();
});

it('returns false for binding filter functions without tag', () => {
const filter = () => true;
expect(isBindingTagFilter(filter)).to.be.false();
});

it('returns false for undefined', () => {
expect(isBindingTagFilter(undefined)).to.be.false();
});

it('returns false if the bindingTagPattern with wrong type', () => {
const filter = () => true;
filter.bindingTagPattern = true; // wrong type
expect(isBindingTagFilter(filter)).to.be.false();
});
});

describe('filterByKey', () => {
it('accepts bindings MATCHING the provided key', () => {
const filter = filterByKey(key);
Expand Down
15 changes: 5 additions & 10 deletions packages/context/src/__tests__/unit/context-observer.unit.ts
Expand Up @@ -21,8 +21,8 @@ const setImmediateAsync = promisify(setImmediate);
* for assertions
*/
class TestContext extends Context {
get parentEventListeners() {
return this._parentEventListeners;
get eventListener() {
return this.parentEventListener;
}
/**
* Wait until the context event queue is empty or an error is thrown
Expand Down Expand Up @@ -63,10 +63,9 @@ describe('Context', () => {

it('registers observers on context with parent', () => {
const childCtx = new TestContext(ctx, 'child');
expect(childCtx.parentEventListeners).to.be.undefined();
expect(childCtx.eventListener).to.be.undefined();
childCtx.subscribe(nonMatchingObserver);
expect(childCtx.parentEventListeners!.has('bind')).to.be.true();
expect(childCtx.parentEventListeners!.has('unbind')).to.be.true();
expect(childCtx.eventListener).to.be.a.Function();
expect(childCtx.isSubscribed(nonMatchingObserver)).to.true();
expect(ctx.isSubscribed(nonMatchingObserver)).to.false();
});
Expand All @@ -84,12 +83,8 @@ describe('Context', () => {
it('un-registers observers on context chain during close', () => {
const childCtx = new TestContext(ctx, 'child');
childCtx.subscribe(nonMatchingObserver);
const parentEventListeners = new Map(childCtx.parentEventListeners!);
childCtx.close();
for (const [event, listener] of parentEventListeners) {
expect(ctx.listeners(event)).to.not.containEql(listener);
}
expect(childCtx.parentEventListeners).to.be.undefined();
expect(childCtx.eventListener).to.be.undefined();
expect(childCtx.isSubscribed(nonMatchingObserver)).to.false();
});

Expand Down
22 changes: 20 additions & 2 deletions packages/context/src/__tests__/unit/context-view.unit.ts
Expand Up @@ -7,6 +7,7 @@ import {expect} from '@loopback/testlab';
import {
Binding,
BindingScope,
BindingTag,
compareBindingsByTag,
Context,
ContextView,
Expand All @@ -16,7 +17,7 @@ import {

describe('ContextView', () => {
let app: Context;
let server: Context;
let server: ServerContext;

let bindings: Binding<unknown>[];
let taggedAsFoo: ContextView;
Expand All @@ -27,6 +28,11 @@ describe('ContextView', () => {
expect(taggedAsFoo.bindings).to.eql(bindings);
});

it('leverages findByTag for binding tag filter', () => {
expect(taggedAsFoo.bindings).to.eql(bindings);
expect(server.findByTagInvoked).to.be.true();
});

it('sorts matched bindings', () => {
const view = new ContextView(
server,
Expand Down Expand Up @@ -199,9 +205,21 @@ describe('ContextView', () => {
taggedAsFoo = server.createView(filterByTag('foo'));
}

class ServerContext extends Context {
findByTagInvoked = false;
constructor(parent: Context, name: string) {
super(parent, name);
}

_findByTagIndex(tag: BindingTag) {
this.findByTagInvoked = true;
return super._findByTagIndex(tag);
}
}

function givenContext() {
app = new Context('app');
server = new Context(app, 'server');
server = new ServerContext(app, 'server');
bindings.push(
server
.bind('bar')
Expand Down

0 comments on commit 836452e

Please sign in to comment.