|
@@ -399,215 +399,4 @@ describe('ServiceContainer', function () {
|
|
|
});
|
|
});
|
|
|
});
|
|
});
|
|
|
});
|
|
});
|
|
|
-
|
|
|
|
|
- describe('find', function () {
|
|
|
|
|
- it('throws an error if no constructor given', function () {
|
|
|
|
|
- const container = new ServiceContainer();
|
|
|
|
|
- const throwable = v => () => container.find(v);
|
|
|
|
|
- const error = v =>
|
|
|
|
|
- format(
|
|
|
|
|
- 'The first argument of ServicesContainer.find must be ' +
|
|
|
|
|
- 'a class constructor, but %s given.',
|
|
|
|
|
- v,
|
|
|
|
|
- );
|
|
|
|
|
- expect(throwable('str')).to.throw(error('"str"'));
|
|
|
|
|
- expect(throwable(10)).to.throw(error('10'));
|
|
|
|
|
- expect(throwable(true)).to.throw(error('true'));
|
|
|
|
|
- expect(throwable(false)).to.throw(error('false'));
|
|
|
|
|
- expect(throwable(undefined)).to.throw(error('undefined'));
|
|
|
|
|
- expect(throwable(null)).to.throw(error('null'));
|
|
|
|
|
- expect(throwable([])).to.throw(error('Array'));
|
|
|
|
|
- expect(throwable({})).to.throw(error('Object'));
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- describe('Service', function () {
|
|
|
|
|
- it('returns an empty array if no bound classes of the given super-class', function () {
|
|
|
|
|
- const container = new ServiceContainer();
|
|
|
|
|
- class MyService extends Service {}
|
|
|
|
|
- const result1 = container.find(MyService);
|
|
|
|
|
- expect(result1).to.be.instanceof(Array);
|
|
|
|
|
- expect(result1).to.be.empty;
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- it('returns cached instances of the given super-class', function () {
|
|
|
|
|
- class MyService extends Service {}
|
|
|
|
|
- class FooService extends MyService {}
|
|
|
|
|
- class BarService extends MyService {}
|
|
|
|
|
- const container = new ServiceContainer();
|
|
|
|
|
- const foo = container.get(FooService);
|
|
|
|
|
- const bar = container.get(BarService);
|
|
|
|
|
- const result = container.find(MyService);
|
|
|
|
|
- expect(result).to.be.instanceof(Array);
|
|
|
|
|
- expect(result).to.have.lengthOf(2);
|
|
|
|
|
- expect(result[0]).to.be.instanceof(FooService);
|
|
|
|
|
- expect(result[0]).to.be.eq(foo);
|
|
|
|
|
- expect(result[1]).to.be.instanceof(BarService);
|
|
|
|
|
- expect(result[1]).to.be.eq(bar);
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- it('returns instances from factory functions of the given super-class', function () {
|
|
|
|
|
- class MyService extends Service {}
|
|
|
|
|
- class FooService extends MyService {}
|
|
|
|
|
- class BarService extends MyService {}
|
|
|
|
|
- const container = new ServiceContainer();
|
|
|
|
|
- container.add(FooService);
|
|
|
|
|
- container.add(BarService);
|
|
|
|
|
- const result = container.find(MyService);
|
|
|
|
|
- expect(result).to.be.instanceof(Array);
|
|
|
|
|
- expect(result).to.have.lengthOf(2);
|
|
|
|
|
- expect(result[0]).to.be.instanceof(FooService);
|
|
|
|
|
- expect(result[1]).to.be.instanceof(BarService);
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- it('does not return a cached instance of the given constructor', function () {
|
|
|
|
|
- class MyService extends Service {}
|
|
|
|
|
- const container = new ServiceContainer();
|
|
|
|
|
- container.get(MyService);
|
|
|
|
|
- const result = container.find(MyService);
|
|
|
|
|
- expect(result).to.be.instanceof(Array);
|
|
|
|
|
- expect(result).to.be.empty;
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- it('does not return an instance from a factory function of the given constructor', function () {
|
|
|
|
|
- class MyService extends Service {}
|
|
|
|
|
- const container = new ServiceContainer();
|
|
|
|
|
- container.add(MyService);
|
|
|
|
|
- const result = container.find(MyService);
|
|
|
|
|
- expect(result).to.be.instanceof(Array);
|
|
|
|
|
- expect(result).to.be.empty;
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- it('caches resolved instances from factory functions', function () {
|
|
|
|
|
- class MyService extends Service {}
|
|
|
|
|
- class FooService extends MyService {}
|
|
|
|
|
- class BarService extends MyService {}
|
|
|
|
|
- const container = new ServiceContainer();
|
|
|
|
|
- container.add(FooService);
|
|
|
|
|
- container.add(BarService);
|
|
|
|
|
- const result = container.find(MyService);
|
|
|
|
|
- expect(result).to.be.instanceof(Array);
|
|
|
|
|
- expect(result).to.have.lengthOf(2);
|
|
|
|
|
- expect(result[0]).to.be.instanceof(FooService);
|
|
|
|
|
- expect(result[1]).to.be.instanceof(BarService);
|
|
|
|
|
- const foo = container.get(FooService);
|
|
|
|
|
- const bar = container.get(BarService);
|
|
|
|
|
- expect(foo).to.be.eq(result[0]);
|
|
|
|
|
- expect(bar).to.be.eq(result[1]);
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- it('uses constructor arguments provided to the factory function', function () {
|
|
|
|
|
- class MyService extends Service {}
|
|
|
|
|
- let givenContainer;
|
|
|
|
|
- let givenArgs;
|
|
|
|
|
- class ChildService extends MyService {
|
|
|
|
|
- constructor(container, ...args) {
|
|
|
|
|
- super(container);
|
|
|
|
|
- givenContainer = container;
|
|
|
|
|
- givenArgs = args;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- const container = new ServiceContainer();
|
|
|
|
|
- container.add(ChildService, 'foo', 'bar');
|
|
|
|
|
- const result = container.find(MyService);
|
|
|
|
|
- expect(result[0]).to.be.instanceof(ChildService);
|
|
|
|
|
- expect(givenContainer).to.be.eq(container);
|
|
|
|
|
- expect(givenArgs).to.be.eql(['foo', 'bar']);
|
|
|
|
|
- });
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- describe('non-Service', function () {
|
|
|
|
|
- it('returns an empty array if no bound classes of the given super-class', function () {
|
|
|
|
|
- const container = new ServiceContainer();
|
|
|
|
|
- class MyService {}
|
|
|
|
|
- const result1 = container.find(MyService);
|
|
|
|
|
- expect(result1).to.be.instanceof(Array);
|
|
|
|
|
- expect(result1).to.be.empty;
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- it('returns cached instances of the given super-class', function () {
|
|
|
|
|
- class MyService {}
|
|
|
|
|
- class FooService extends MyService {}
|
|
|
|
|
- class BarService extends MyService {}
|
|
|
|
|
- const container = new ServiceContainer();
|
|
|
|
|
- const foo = container.get(FooService);
|
|
|
|
|
- const bar = container.get(BarService);
|
|
|
|
|
- const result = container.find(MyService);
|
|
|
|
|
- expect(result).to.be.instanceof(Array);
|
|
|
|
|
- expect(result).to.have.lengthOf(2);
|
|
|
|
|
- expect(result[0]).to.be.instanceof(FooService);
|
|
|
|
|
- expect(result[0]).to.be.eq(foo);
|
|
|
|
|
- expect(result[1]).to.be.instanceof(BarService);
|
|
|
|
|
- expect(result[1]).to.be.eq(bar);
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- it('returns instances from factory functions of the given super-class', function () {
|
|
|
|
|
- class MyService {}
|
|
|
|
|
- class FooService extends MyService {}
|
|
|
|
|
- class BarService extends MyService {}
|
|
|
|
|
- const container = new ServiceContainer();
|
|
|
|
|
- container.add(FooService);
|
|
|
|
|
- container.add(BarService);
|
|
|
|
|
- const result = container.find(MyService);
|
|
|
|
|
- expect(result).to.be.instanceof(Array);
|
|
|
|
|
- expect(result).to.have.lengthOf(2);
|
|
|
|
|
- expect(result[0]).to.be.instanceof(FooService);
|
|
|
|
|
- expect(result[1]).to.be.instanceof(BarService);
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- it('does not return a cached instance of the given constructor', function () {
|
|
|
|
|
- class MyService {}
|
|
|
|
|
- const container = new ServiceContainer();
|
|
|
|
|
- container.get(MyService);
|
|
|
|
|
- const result = container.find(MyService);
|
|
|
|
|
- expect(result).to.be.instanceof(Array);
|
|
|
|
|
- expect(result).to.be.empty;
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- it('does not return an instance from a factory function of the given constructor', function () {
|
|
|
|
|
- class MyService {}
|
|
|
|
|
- const container = new ServiceContainer();
|
|
|
|
|
- container.add(MyService);
|
|
|
|
|
- const result = container.find(MyService);
|
|
|
|
|
- expect(result).to.be.instanceof(Array);
|
|
|
|
|
- expect(result).to.be.empty;
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- it('caches resolved instances from factory functions', function () {
|
|
|
|
|
- class MyService {}
|
|
|
|
|
- class FooService extends MyService {}
|
|
|
|
|
- class BarService extends MyService {}
|
|
|
|
|
- const container = new ServiceContainer();
|
|
|
|
|
- container.add(FooService);
|
|
|
|
|
- container.add(BarService);
|
|
|
|
|
- const result = container.find(MyService);
|
|
|
|
|
- expect(result).to.be.instanceof(Array);
|
|
|
|
|
- expect(result).to.have.lengthOf(2);
|
|
|
|
|
- expect(result[0]).to.be.instanceof(FooService);
|
|
|
|
|
- expect(result[1]).to.be.instanceof(BarService);
|
|
|
|
|
- const foo = container.get(FooService);
|
|
|
|
|
- const bar = container.get(BarService);
|
|
|
|
|
- expect(foo).to.be.eq(result[0]);
|
|
|
|
|
- expect(bar).to.be.eq(result[1]);
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- it('uses constructor arguments provided to the factory function', function () {
|
|
|
|
|
- class MyService {}
|
|
|
|
|
- let givenContainer;
|
|
|
|
|
- let givenArgs;
|
|
|
|
|
- class ChildService extends MyService {
|
|
|
|
|
- constructor(...args) {
|
|
|
|
|
- super();
|
|
|
|
|
- givenContainer = container;
|
|
|
|
|
- givenArgs = args;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- const container = new ServiceContainer();
|
|
|
|
|
- container.add(ChildService, 'foo', 'bar');
|
|
|
|
|
- const result = container.find(MyService);
|
|
|
|
|
- expect(result[0]).to.be.instanceof(ChildService);
|
|
|
|
|
- expect(givenContainer).to.be.eq(container);
|
|
|
|
|
- expect(givenArgs).to.be.eql(['foo', 'bar']);
|
|
|
|
|
- });
|
|
|
|
|
- });
|
|
|
|
|
- });
|
|
|
|
|
});
|
|
});
|