Skip to content

Commit

Permalink
fix(context): clean up the circular dependency tests
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed Jan 16, 2018
1 parent eea3c08 commit 5c35ccd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 31 deletions.
4 changes: 2 additions & 2 deletions packages/context/src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ export class ResolutionSession {
}
if (this.bindings.indexOf(binding) !== -1) {
throw new Error(
`Circular dependency detected for '${
`Circular dependency detected on path '${this.getBindingPath()} --> ${
binding.key
}' on path '${this.getBindingPath()}'`,
}'`,
);
}
this.bindings.push(binding);
Expand Down
46 changes: 17 additions & 29 deletions packages/context/test/unit/resolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,23 +92,25 @@ describe('constructor injection', () => {

it('reports circular dependencies of two bindings', () => {
const context = new Context();

// Declare two interfaces so that they can be used for typing
interface XInterface {}
interface YInterface {}

class XClass implements XInterface {
constructor(@inject('y') public y: YInterface) {}
@inject('y') public y: YInterface;
}

class YClass implements YInterface {
constructor(@inject('x') public x: XInterface) {}
@inject('x') public x: XInterface;
}

context.bind('x').toClass(XClass);
context.bind('y').toClass(YClass);
expect(() => context.getSync('x')).to.throw(/Circular dependency/);
expect(() => context.getSync('y')).to.throw(/Circular dependency/);
expect(() => context.getSync('x')).to.throw(
"Circular dependency detected on path 'x --> y --> x'",
);
expect(() => context.getSync('y')).to.throw(
"Circular dependency detected on path 'y --> x --> y'",
);
});

it('reports circular dependencies of three bindings', () => {
Expand All @@ -134,9 +136,15 @@ describe('constructor injection', () => {
context.bind('x').toClass(XClass);
context.bind('y').toClass(YClass);
context.bind('z').toClass(ZClass);
expect(() => context.getSync('x')).to.throw(/Circular dependency/);
expect(() => context.getSync('y')).to.throw(/Circular dependency/);
expect(() => context.getSync('z')).to.throw(/Circular dependency/);
expect(() => context.getSync('x')).to.throw(
"Circular dependency detected on path 'x --> y --> z --> x'",
);
expect(() => context.getSync('y')).to.throw(
"Circular dependency detected on path 'y --> z --> x --> y'",
);
expect(() => context.getSync('z')).to.throw(
"Circular dependency detected on path 'z --> x --> y --> z'",
);
});

it('will not report circular dependencies if two bindings', () => {
Expand Down Expand Up @@ -321,24 +329,6 @@ describe('property injection', () => {
const t = instantiateClass(TestClass, ctx) as TestClass;
expect(t.fooBar).to.eql('FOO:BAR');
});

it('reports circular dependencies of two bindings', () => {
const context = new Context();
class XClass {
// tslint:disable-next-line:no-any
@inject('y') public y: any;
}

class YClass {
// tslint:disable-next-line:no-any
@inject('x') public x: any;
}

context.bind('x').toClass(XClass);
context.bind('y').toClass(YClass);
expect(() => context.getSync('x')).to.throw(/Circular dependency/);
expect(() => context.getSync('y')).to.throw(/Circular dependency/);
});
});

describe('async property injection', () => {
Expand Down Expand Up @@ -458,7 +448,6 @@ describe('sync constructor & async property injection', () => {
});

function customDecorator(def: Object) {
// tslint:disable-next-line:no-any
return inject('foo', def, (c: Context, injection: Injection) => {
const barKey = injection.metadata && injection.metadata.x;
const b = c.getSync(barKey);
Expand All @@ -468,7 +457,6 @@ function customDecorator(def: Object) {
}

function customAsyncDecorator(def: Object) {
// tslint:disable-next-line:no-any
return inject('foo', def, async (c: Context, injection: Injection) => {
const barKey = injection.metadata && injection.metadata.x;
const b = await c.get(barKey);
Expand Down

0 comments on commit 5c35ccd

Please sign in to comment.