| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712 |
- import {expect} from 'chai';
- import {Service} from './service.js';
- import {format} from '@e22m4u/js-format';
- import {ServiceContainer} from './service-container.js';
- describe('ServiceContainer', function () {
- describe('get', function () {
- it('throws an error if no constructor given', function () {
- const container = new ServiceContainer();
- const throwable = v => () => container.get(v);
- const error = v =>
- format(
- 'The first argument of ServicesContainer.get 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('passes itself and given arguments to the given constructor', function () {
- let executed = 0;
- let givenContainer;
- let givenArgs;
- class MyService extends Service {
- constructor(container, ...args) {
- super(container);
- executed++;
- givenContainer = container;
- givenArgs = args;
- }
- }
- const container = new ServiceContainer();
- const service = container.get(MyService, 'foo', 'bar');
- expect(service).to.be.instanceof(MyService);
- expect(executed).to.be.eq(1);
- expect(givenContainer).to.be.eq(container);
- expect(givenArgs).to.be.eql(['foo', 'bar']);
- });
- it('instantiates from an existing factory function', function () {
- let executed = 0;
- let givenContainer;
- let givenArgs;
- class MyService extends Service {
- constructor(container, ...args) {
- super(container);
- executed++;
- givenContainer = container;
- givenArgs = args;
- }
- }
- const container = new ServiceContainer();
- container.add(MyService, 'foo', 'bar');
- expect(executed).to.be.eq(0);
- const service = container.get(MyService);
- expect(service).to.be.instanceof(MyService);
- expect(executed).to.be.eq(1);
- expect(givenContainer).to.be.eq(container);
- expect(givenArgs).to.be.eql(['foo', 'bar']);
- });
- it('overrides an existing factory function', function () {
- let executed = 0;
- let givenContainer;
- let givenArgs;
- class MyService extends Service {
- constructor(container, ...args) {
- super(container);
- executed++;
- givenContainer = container;
- givenArgs = args;
- }
- }
- const container = new ServiceContainer();
- container.add(MyService, 'foo', 'bar');
- expect(executed).to.be.eq(0);
- const service = container.get(MyService, 'baz', 'qux');
- expect(service).to.be.instanceof(MyService);
- expect(executed).to.be.eq(1);
- expect(givenContainer).to.be.eq(container);
- expect(givenArgs).to.be.eql(['baz', 'qux']);
- });
- it('caches a new instance', function () {
- let executed = 0;
- class MyService extends Service {
- constructor(container) {
- super(container);
- ++executed;
- }
- }
- const container = new ServiceContainer();
- const service1 = container.get(MyService);
- const service2 = container.get(MyService);
- expect(service1).to.be.instanceof(MyService);
- expect(service2).to.be.instanceof(MyService);
- expect(service1).to.be.eq(service2);
- expect(executed).to.be.eq(1);
- });
- it('overrides the cached instance', function () {
- let executed = 0;
- const givenArgs = [];
- class MyService extends Service {
- constructor(container, arg) {
- super(container);
- ++executed;
- givenArgs.push(arg);
- }
- }
- const container = new ServiceContainer();
- const service1 = container.get(MyService, 'foo');
- const service2 = container.get(MyService);
- const service3 = container.get(MyService, 'bar');
- const service4 = container.get(MyService);
- expect(service1).to.be.instanceof(MyService);
- expect(service2).to.be.instanceof(MyService);
- expect(service3).to.be.instanceof(MyService);
- expect(service4).to.be.instanceof(MyService);
- expect(service1).to.be.eq(service2);
- expect(service2).to.be.not.eq(service3);
- expect(service3).to.be.eq(service4);
- expect(executed).to.be.eq(2);
- expect(givenArgs).to.be.eql(['foo', 'bar']);
- });
- });
- describe('non-Service', function () {
- it('passes given arguments to the given constructor', function () {
- let executed = 0;
- let givenArgs;
- class MyService {
- constructor(...args) {
- executed++;
- givenArgs = args;
- }
- }
- const container = new ServiceContainer();
- const service = container.get(MyService, 'foo', 'bar');
- expect(service).to.be.instanceof(MyService);
- expect(executed).to.be.eq(1);
- expect(givenArgs).to.be.eql(['foo', 'bar']);
- });
- it('instantiates from an existing factory function', function () {
- let executed = 0;
- let givenArgs;
- class MyService {
- constructor(...args) {
- executed++;
- givenArgs = args;
- }
- }
- const container = new ServiceContainer();
- container.add(MyService, 'foo', 'bar');
- expect(executed).to.be.eq(0);
- const service = container.get(MyService);
- expect(service).to.be.instanceof(MyService);
- expect(executed).to.be.eq(1);
- expect(givenArgs).to.be.eql(['foo', 'bar']);
- });
- it('overrides an existing factory function', function () {
- let executed = 0;
- let givenArgs;
- class MyService {
- constructor(...args) {
- executed++;
- givenArgs = args;
- }
- }
- const container = new ServiceContainer();
- container.add(MyService, 'foo', 'bar');
- expect(executed).to.be.eq(0);
- const service = container.get(MyService, 'baz', 'qux');
- expect(service).to.be.instanceof(MyService);
- expect(executed).to.be.eq(1);
- expect(givenArgs).to.be.eql(['baz', 'qux']);
- });
- it('caches a new instance', function () {
- let executed = 0;
- class MyService {
- constructor() {
- ++executed;
- }
- }
- const container = new ServiceContainer();
- const service1 = container.get(MyService);
- const service2 = container.get(MyService);
- expect(service1).to.be.instanceof(MyService);
- expect(service2).to.be.instanceof(MyService);
- expect(service1).to.be.eq(service2);
- expect(executed).to.be.eq(1);
- });
- it('overrides the cached instance', function () {
- let executed = 0;
- const givenArgs = [];
- class MyService {
- constructor(arg) {
- ++executed;
- givenArgs.push(arg);
- }
- }
- const container = new ServiceContainer();
- const service1 = container.get(MyService, 'foo');
- const service2 = container.get(MyService);
- const service3 = container.get(MyService, 'bar');
- const service4 = container.get(MyService);
- expect(service1).to.be.instanceof(MyService);
- expect(service2).to.be.instanceof(MyService);
- expect(service3).to.be.instanceof(MyService);
- expect(service4).to.be.instanceof(MyService);
- expect(service1).to.be.eq(service2);
- expect(service2).to.be.not.eq(service3);
- expect(service3).to.be.eq(service4);
- expect(executed).to.be.eq(2);
- expect(givenArgs).to.be.eql(['foo', 'bar']);
- });
- });
- });
- describe('has', function () {
- describe('Service', function () {
- it('returns true when a given constructor has its cached instance or false', function () {
- const container = new ServiceContainer();
- class MyService extends Service {}
- expect(container.has(MyService)).to.be.false;
- container.get(MyService);
- expect(container.has(MyService)).to.be.true;
- });
- it('returns true when a given constructor has its factory function or false', function () {
- const container = new ServiceContainer();
- class MyService extends Service {}
- expect(container.has(MyService)).to.be.false;
- container.add(MyService);
- expect(container.has(MyService)).to.be.true;
- });
- });
- describe('non-Service', function () {
- it('returns true when a given constructor has its cached instance or false', function () {
- const container = new ServiceContainer();
- class MyService {}
- expect(container.has(MyService)).to.be.false;
- container.get(MyService);
- expect(container.has(MyService)).to.be.true;
- });
- it('returns true when a given constructor has its factory function or false', function () {
- const container = new ServiceContainer();
- class MyService {}
- expect(container.has(MyService)).to.be.false;
- container.add(MyService);
- expect(container.has(MyService)).to.be.true;
- });
- });
- });
- describe('add', function () {
- it('throws an error if no constructor given', function () {
- const container = new ServiceContainer();
- const throwable = v => () => container.add(v);
- const error = v =>
- format(
- 'The first argument of ServicesContainer.add must be ' +
- 'a class constructor, but %s given.',
- v,
- );
- expect(throwable()).to.throw(error('undefined'));
- 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(null)).to.throw(error('null'));
- expect(throwable([])).to.throw(error('Array'));
- expect(throwable({})).to.throw(error('Object'));
- });
- describe('Service', function () {
- it('returns itself', function () {
- class MyService extends Service {}
- const container = new ServiceContainer();
- const res = container.add(MyService);
- expect(res).to.be.eq(container);
- });
- it('provides given arguments to the factory function', function () {
- let executed = 0;
- let givenContainer;
- let givenArgs;
- class MyService extends Service {
- constructor(container, ...args) {
- super(container);
- executed++;
- givenContainer = container;
- givenArgs = args;
- }
- }
- const container = new ServiceContainer();
- container.add(MyService, 'foo', 'bar');
- expect(executed).to.be.eq(0);
- const service = container.get(MyService);
- expect(service).to.be.instanceof(MyService);
- expect(executed).to.be.eq(1);
- expect(givenContainer).to.be.eq(container);
- expect(givenArgs).to.be.eql(['foo', 'bar']);
- });
- it('overrides a cached instance of the given constructor', function () {
- class MyService extends Service {}
- const container = new ServiceContainer();
- const service1 = container.get(MyService);
- const service2 = container.get(MyService);
- expect(service1).to.be.eq(service2);
- container.add(MyService);
- const service3 = container.get(MyService);
- const service4 = container.get(MyService);
- expect(service3).to.be.eq(service4);
- expect(service3).to.be.not.eq(service1);
- });
- it('overrides constructor arguments of the factory function', function () {
- let executed = 0;
- let givenContainer;
- let givenArgs;
- class MyService extends Service {
- constructor(container, ...args) {
- super(container);
- executed++;
- givenContainer = container;
- givenArgs = args;
- }
- }
- const container = new ServiceContainer();
- container.add(MyService, 'foo', 'bar');
- expect(executed).to.be.eq(0);
- container.add(MyService, 'baz', 'qux');
- const service = container.get(MyService);
- expect(service).to.be.instanceof(MyService);
- expect(executed).to.be.eq(1);
- expect(givenContainer).to.be.eq(container);
- expect(givenArgs).to.be.eql(['baz', 'qux']);
- });
- });
- describe('non-Service', function () {
- it('returns itself', function () {
- class MyService {}
- const container = new ServiceContainer();
- const res = container.add(MyService);
- expect(res).to.be.eq(container);
- });
- it('provides given arguments to the factory function', function () {
- let executed = 0;
- let givenArgs;
- class MyService {
- constructor(...args) {
- executed++;
- givenArgs = args;
- }
- }
- const container = new ServiceContainer();
- container.add(MyService, 'foo', 'bar');
- expect(executed).to.be.eq(0);
- const service = container.get(MyService);
- expect(service).to.be.instanceof(MyService);
- expect(executed).to.be.eq(1);
- expect(givenArgs).to.be.eql(['foo', 'bar']);
- });
- it('overrides a cached instance of the given constructor', function () {
- class MyService {}
- const container = new ServiceContainer();
- const service1 = container.get(MyService);
- const service2 = container.get(MyService);
- expect(service1).to.be.eq(service2);
- container.add(MyService);
- const service3 = container.get(MyService);
- const service4 = container.get(MyService);
- expect(service3).to.be.eq(service4);
- expect(service3).to.be.not.eq(service1);
- });
- it('overrides constructor arguments of the factory function', function () {
- let executed = 0;
- let givenArgs;
- class MyService {
- constructor(...args) {
- executed++;
- givenArgs = args;
- }
- }
- const container = new ServiceContainer();
- container.add(MyService, 'foo', 'bar');
- expect(executed).to.be.eq(0);
- container.add(MyService, 'baz', 'qux');
- const service = container.get(MyService);
- expect(service).to.be.instanceof(MyService);
- expect(executed).to.be.eq(1);
- expect(givenArgs).to.be.eql(['baz', 'qux']);
- });
- });
- });
- describe('use', function () {
- it('throws an error if no constructor given', function () {
- const container = new ServiceContainer();
- const throwable = v => () => container.use(v);
- const error = v =>
- format(
- 'The first argument of ServicesContainer.use must be ' +
- 'a class constructor, but %s given.',
- v,
- );
- expect(throwable()).to.throw(error('undefined'));
- 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(null)).to.throw(error('null'));
- expect(throwable([])).to.throw(error('Array'));
- expect(throwable({})).to.throw(error('Object'));
- });
- describe('Service', function () {
- it('returns itself', function () {
- class MyService extends Service {}
- const container = new ServiceContainer();
- const res = container.use(MyService);
- expect(res).to.be.eq(container);
- });
- it('passes itself and given arguments to the given constructor', function () {
- let executed = 0;
- let givenContainer;
- let givenArgs;
- class MyService extends Service {
- constructor(container, ...args) {
- super(container);
- executed++;
- givenContainer = container;
- givenArgs = args;
- }
- }
- const container = new ServiceContainer();
- container.use(MyService, 'foo', 'bar');
- expect(executed).to.be.eq(1);
- expect(givenContainer).to.be.eq(container);
- expect(givenArgs).to.be.eql(['foo', 'bar']);
- });
- it('overrides an existing factory function', function () {
- let executed = 0;
- let givenContainer;
- let givenArgs;
- class MyService extends Service {
- constructor(container, ...args) {
- super(container);
- executed++;
- givenContainer = container;
- givenArgs = args;
- }
- }
- const container = new ServiceContainer();
- container.add(MyService, 'foo', 'bar');
- expect(executed).to.be.eq(0);
- container.use(MyService, 'baz', 'qux');
- expect(executed).to.be.eq(1);
- expect(givenContainer).to.be.eq(container);
- expect(givenArgs).to.be.eql(['baz', 'qux']);
- });
- it('caches a new instance', function () {
- let executed = 0;
- let service;
- class MyService extends Service {
- constructor(container) {
- super(container);
- ++executed;
- service = this;
- }
- }
- const container = new ServiceContainer();
- container.use(MyService);
- const cachedService = container.get(MyService);
- expect(cachedService).to.be.instanceof(MyService);
- expect(cachedService).to.be.eq(service);
- expect(executed).to.be.eq(1);
- });
- it('overrides the cached instance', function () {
- let executed = 0;
- let service;
- let givenArgs;
- class MyService extends Service {
- constructor(container, ...args) {
- super(container);
- ++executed;
- service = this;
- givenArgs = args;
- }
- }
- const container = new ServiceContainer();
- container.use(MyService, 'foo');
- expect(executed).to.be.eq(1);
- expect(service).to.be.instanceof(MyService);
- expect(givenArgs).to.be.eql(['foo']);
- const service1 = service;
- container.use(MyService, 'bar');
- expect(executed).to.be.eq(2);
- expect(service).to.be.instanceof(MyService);
- expect(givenArgs).to.be.eql(['bar']);
- const service2 = service;
- expect(service2).to.be.not.eq(service1);
- });
- });
- describe('non-Service', function () {
- it('returns itself', function () {
- class MyService {}
- const container = new ServiceContainer();
- const res = container.use(MyService);
- expect(res).to.be.eq(container);
- });
- it('passes given arguments to the given constructor', function () {
- let executed = 0;
- let givenArgs;
- class MyService {
- constructor(...args) {
- executed++;
- givenArgs = args;
- }
- }
- const container = new ServiceContainer();
- container.use(MyService, 'foo', 'bar');
- expect(executed).to.be.eq(1);
- expect(givenArgs).to.be.eql(['foo', 'bar']);
- });
- it('overrides an existing factory function', function () {
- let executed = 0;
- let givenArgs;
- class MyService {
- constructor(...args) {
- executed++;
- givenArgs = args;
- }
- }
- const container = new ServiceContainer();
- container.add(MyService, 'foo', 'bar');
- expect(executed).to.be.eq(0);
- container.use(MyService, 'baz', 'qux');
- expect(executed).to.be.eq(1);
- expect(givenArgs).to.be.eql(['baz', 'qux']);
- });
- it('caches a new instance', function () {
- let executed = 0;
- let service;
- class MyService {
- constructor() {
- ++executed;
- service = this;
- }
- }
- const container = new ServiceContainer();
- container.use(MyService);
- const cachedService = container.get(MyService);
- expect(cachedService).to.be.instanceof(MyService);
- expect(cachedService).to.be.eq(service);
- expect(executed).to.be.eq(1);
- });
- it('overrides the cached instance', function () {
- let executed = 0;
- let service;
- let givenArgs;
- class MyService {
- constructor(...args) {
- ++executed;
- service = this;
- givenArgs = args;
- }
- }
- const container = new ServiceContainer();
- container.use(MyService, 'foo');
- expect(executed).to.be.eq(1);
- expect(service).to.be.instanceof(MyService);
- expect(givenArgs).to.be.eql(['foo']);
- const service1 = service;
- container.use(MyService, 'bar');
- expect(executed).to.be.eq(2);
- expect(service).to.be.instanceof(MyService);
- expect(givenArgs).to.be.eql(['bar']);
- const service2 = service;
- expect(service2).to.be.not.eq(service1);
- });
- });
- });
- describe('set', function () {
- it('requires the "ctor" argument to be a class constructor', function () {
- const container = new ServiceContainer();
- const throwable = v => () => container.set(v, {});
- const error = v =>
- format(
- 'The first argument of ServicesContainer.set must be ' +
- 'a class constructor, but %s given.',
- v,
- );
- expect(throwable()).to.throw(error('undefined'));
- 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(null)).to.throw(error('null'));
- expect(throwable([])).to.throw(error('Array'));
- expect(throwable({})).to.throw(error('Object'));
- throwable(String)();
- });
- it('requires the "service" argument to be an Object', function () {
- const container = new ServiceContainer();
- const throwable = v => () => container.set(String, v);
- const error = v =>
- format(
- 'The second argument of ServicesContainer.set must be ' +
- 'an Object, but %s given.',
- v,
- );
- expect(throwable()).to.throw(error('undefined'));
- 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(null)).to.throw(error('null'));
- expect(throwable([])).to.throw(error('Array'));
- throwable({})();
- });
- describe('Service', function () {
- it('returns itself', function () {
- class MyService extends Service {}
- const container = new ServiceContainer();
- const res = container.set(MyService, {});
- expect(res).to.be.eq(container);
- });
- it('sets the given service', function () {
- class MyService extends Service {}
- const container = new ServiceContainer();
- const service = {};
- container.set(MyService, service);
- const res = container.get(MyService);
- expect(res).to.be.eq(service);
- });
- it('overrides by the given service', function () {
- class MyService extends Service {}
- const container = new ServiceContainer();
- const service1 = {foo: 'bar'};
- const service2 = {bar: 'baz'};
- container.set(MyService, service1);
- container.set(MyService, service2);
- const res = container.get(MyService);
- expect(res).to.be.eq(service2);
- });
- });
- describe('non-Service', function () {
- it('returns itself', function () {
- class MyService {}
- const container = new ServiceContainer();
- const res = container.set(MyService, {});
- expect(res).to.be.eq(container);
- });
- it('sets the given service', function () {
- class MyService {}
- const container = new ServiceContainer();
- const service = {};
- container.set(MyService, service);
- const res = container.get(MyService);
- expect(res).to.be.eq(service);
- });
- it('overrides by the given service', function () {
- class MyService {}
- const container = new ServiceContainer();
- const service1 = {foo: 'bar'};
- const service2 = {bar: 'baz'};
- container.set(MyService, service1);
- container.set(MyService, service2);
- const res = container.get(MyService);
- expect(res).to.be.eq(service2);
- });
- });
- });
- });
|