Skip to content

Commit

Permalink
Upgrade integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Gnucki committed Jul 29, 2018
1 parent 66b3d55 commit 182dfa1
Show file tree
Hide file tree
Showing 2 changed files with 215 additions and 1 deletion.
68 changes: 67 additions & 1 deletion test/integration/building.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,26 @@
const expect = require('../expect');
const nead = require('../..');
const Container = require('../../src/Container');
const CyclicDependencyError = require('../../src/errors/CyclicDependency');

require('./injection');

function Foo() {
}
Foo.prototype.plop = 'plip';

const bar = {
f: function f() {
return this.foo.plop;
}
};

const foobar = {
g: function g() {
return `${this.foo.plop}-${this.foo.f()}-${this.foo.foo.plop}`;
}
};

describe('nead', () => {
describe('"createContainer" method', () => {
it('should create a new dependency injection container', () => {
Expand All @@ -19,7 +32,7 @@ describe('nead', () => {
});

describe('container', () => {
it('should allow to instantiate a service from a definition', () => {
it('should instantiate a service from a definition', () => {
const container = nead.createContainer();
container.create('service', 'foo', {
object: Foo
Expand All @@ -29,5 +42,58 @@ describe('nead', () => {
expect(foo).to.be.an.instanceOf(Foo);
expect(foo.plop).to.equal('plip');
});

it('should instantiate and inject service dependencies in the right order', () => {
const container = nead.createContainer();
container.create('service', 'foo', {
object: Foo
});
container.create('service', 'foobar', {
object: foobar,
singleton: true,
dependencies: {
foo: '#foo#bar'
}
});
container.create('service', 'bar', {
object: bar,
dependencies: {
foo: '#foo'
}
});
container.build();
const foobarInstance = container.get('foobar');
expect(foobarInstance.g()).equal('plip-plip-plip');
const barInstance = container.get('bar');
expect(barInstance.f()).to.equal('plip');
});

it('should fail on cyclic dependency', () => {
const container = nead.createContainer();
container.create('service', 'foo', {
object: Foo,
dependencies: {
bar: '#bar'
}
});
container.create('service', 'foobar', {
object: foobar,
singleton: true,
dependencies: {
foo: '#foo#bar'
}
});
container.create('service', 'bar', {
object: bar,
dependencies: {
foo: '#foo'
}
});

expect(() => container.build()).to.throw(
CyclicDependencyError,
'Cyclic dependency [\'foo\' < \'bar\' < \'foo\']'
);
});
});
});
148 changes: 148 additions & 0 deletions test/integration/injection.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

const expect = require('../expect');
const nead = require('../..');
const Registry = require('../../src/Registry');
const DependencyError = require('../../src/errors/Dependency');
const InaccessibleMemberError = require('../../src/errors/InaccessibleMember');
const factory = require('../fixtures/factory');

describe('nead', () => {
describe('"inject" method', () => {
Expand All @@ -14,4 +18,148 @@ describe('nead', () => {
expect(injectedObject.foo).to.equal('bar');
});
});

describe('"validate" method', () => {
it('should check dependency validity', () => {
const object = {
need: {
foo: {}
}
};
expect(() => nead.validate(object)).to.throw('[foo]: Dependency has not been injected');
});

it('should work with injection definers', () => {
const object = {
need: {
plop: {
property: 'plep',
value: {
type: 'object',
properties: {
foo: {
type: 'string'
},
bar: {
type: 'number',
default: 2
}
}
}
},
plip: {
optional: true
},
plap: {},
plup: {
interface: {
methods: ['f', 'g']
}
}

}
};

object.plep = {
foo: 'bar'
};
object.plap = 3;
object.plup = {
f: () => 'f',
g: () => 'g',
h: () => 'h'
};
const validatedObject = nead.validate(object);
expect(validatedObject.plep).to.deep.equal({
foo: 'bar',
bar: 2
});
expect(validatedObject.plap).to.deep.equal(3);
expect(validatedObject.plup.f()).to.equal('f');
expect(() => validatedObject.plup.h()).to.throw(InaccessibleMemberError);
expect(validatedObject.plop).to.equal(undefined);

object.plap = undefined;

expect(() => nead.validate(object)).to.throw(
DependencyError,
'[plap]: Dependency has not been injected'
);
});

it('should handle proxy injectors', () => {
const object = {
need: {
registry: {
proxy: 'registry',
value: {
type: 'string',
default: 'bar'
}
},
list: {
proxy: 'list',
value: {
type: 'string',
default: 'bar'
}
},
factory: {
proxy: 'factory',
value: {
type: 'object',
properties: {
foo: {
type: 'string'
},
bar: {
default: 'bar'
}
}
}
},
direct: {
proxy: 'direct',
value: {
type: 'string',
default: 'bar'
},
optional: true
},
no: {
value: {
type: 'string',
default: 'bar'
},
optional: true
}
}
};
const registry = new Registry();
registry.set('plop', 'foo');
registry.set('plip', null);

const list = ['foo', null];

const Foo = function Foo() {};
Foo.prototype.foo = 'foo';
factory.init(Foo);

const validatedObject = nead.injectSet(object, {
registry,
list,
factory
}, true);

expect(validatedObject.registry.get('plop')).to.equal('foo');
expect(validatedObject.registry.get('plip')).to.equal('bar');
expect(registry.get('plip')).to.equal(null);
expect(validatedObject.list[0]).to.equal('foo');
expect(validatedObject.list[1]).to.equal('bar');
expect(list[1]).to.equal(null);
expect(validatedObject.factory.create().foo).to.equal('foo');
expect(validatedObject.direct).to.equal('bar');
expect(validatedObject.no).to.equal('bar');
});
});
});

0 comments on commit 182dfa1

Please sign in to comment.