UNPKG

@akala/core

Version:
139 lines (130 loc) 4.92 kB
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; import { SimpleInjector } from '../injectors/simple-injector.js'; import assert from 'assert'; import { useInjector, inject } from '../injectors/reflection-injector.js'; import { it } from 'node:test'; /*var oldProxy = Proxy; global['Proxy'] = new oldProxy(oldProxy, { get: function (target, key) { if (typeof (key) == 'symbol' && key == Symbol.hasInstance) { return function (o) { return o[Symbol.for('isChain')]; } } return Reflect.get(target, key); } }); var func = function (key?) { console.log('original function'); return new Promise((resolve, reject) => { resolve(key); }) }; var func2 = function (dummy, key?) { console.log('original function2'); console.log(key); }; var i1 = new Injector(); i1.register('$config', chain(func, function (keys, ...args) { return [keys.join('.')]; })); i1.register('$updateConfig', chain(func2, function (keys, dummy, key?: string) { if (key) keys.push(key); return [dummy, keys.join('.')]; })); i1.injectWithName(['$config.pwet.a.b.c'], function (config) { config.then((result) => { console.log('key: ' + result); }) })(); i1.injectWithName(['$updateConfig.pwet.a.b.c'], function (config) { config({ x: 'y' }, 'd'); })();*/ it('injector should work', () => { const i = new SimpleInjector(); i.register('os', 'linux'); i.register('vendor', 'microsoft'); i.register('action', 'loves'); i.register('otherVendor', 'node'); const subI = new SimpleInjector(i); let A = class A { os; vendor; otherVendor; constructor(otherVendor) { this.otherVendor = otherVendor; } do(action) { return `${this.vendor} ${action} ${this.os}`; } doOtherVendor(action) { return `${this.vendor} ${action} ${this.otherVendor}`; } }; __decorate([ inject('os'), __metadata("design:type", String) ], A.prototype, "os", void 0); __decorate([ inject(), __metadata("design:type", String) ], A.prototype, "vendor", void 0); __decorate([ __param(0, inject('action')), __metadata("design:type", Function), __metadata("design:paramtypes", [String]), __metadata("design:returntype", void 0) ], A.prototype, "do", null); __decorate([ __param(0, inject('action')), __metadata("design:type", Function), __metadata("design:paramtypes", [String]), __metadata("design:returntype", void 0) ], A.prototype, "doOtherVendor", null); A = __decorate([ useInjector(subI), __param(0, inject('otherVendor')), __metadata("design:paramtypes", [String]) ], A); const a = new A(); assert.strictEqual(a.os, i.resolve('os'), 'named injection does not work'); assert.strictEqual(a.vendor, i.resolve('vendor'), 'implicit injection does not work'); assert.strictEqual(a.otherVendor, i.resolve('otherVendor'), 'constructor injection does not work'); assert.strictEqual(a.do('likes'), `${i.resolve('vendor')} ${i.resolve('action')} ${i.resolve('os')}`, 'parameter injection does not work'); class B extends A { } const b = new B(); assert.strictEqual(b.os, i.resolve('os'), 'named injection does not work on inherited classes'); assert.strictEqual(b.vendor, i.resolve('vendor'), 'implicit injection does not work on inherited classes'); assert.strictEqual(b.do('loves'), 'microsoft loves linux', 'parameter injection does not work on inherited classes'); assert.strictEqual(b.doOtherVendor('loves'), 'microsoft loves node', 'parameter injection does not work on inherited classes'); // nested resolutions const i1 = new SimpleInjector(); const i2 = i1.register('a', new SimpleInjector()); i2.register('b.c', 'x'); assert.strictEqual(i1.resolve('a.b.c'), 'x'); }); //# sourceMappingURL=injector.js.map