@furystack/inject
Version:
Core FuryStack package
119 lines • 4.77 kB
JavaScript
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);
};
import { Injectable } from './injectable.js';
import { Injected, getDependencyList } from './injected.js';
import { describe, expect, it } from 'vitest';
import { Injector } from './injector.js';
describe('@Injected()', () => {
it('Should register into the injectable fields', () => {
let Property = class Property {
foo = 3;
};
Property = __decorate([
Injectable()
], Property);
class TestClass {
}
__decorate([
Injected(Property),
__metadata("design:type", Property)
], TestClass.prototype, "property", void 0);
__decorate([
Injected(Property),
__metadata("design:type", Property)
], TestClass.prototype, "property3", void 0);
const dependencyList = getDependencyList(TestClass);
expect(dependencyList.has(Property)).toBeTruthy();
});
it('Should inject a property from the decorator', () => {
let Property = class Property {
foo = 3;
};
Property = __decorate([
Injectable()
], Property);
let TestClass = class TestClass {
};
__decorate([
Injected(Property),
__metadata("design:type", Property)
], TestClass.prototype, "property", void 0);
TestClass = __decorate([
Injectable()
], TestClass);
const instance = new Injector().getInstance(TestClass);
expect(instance.property).toBeInstanceOf(Property);
expect(instance.property.foo).toBe(3);
});
it('Should throw an error when trying to modify the injected property', () => {
let Property = class Property {
foo = 3;
};
Property = __decorate([
Injectable()
], Property);
let TestClass = class TestClass {
};
__decorate([
Injected(Property),
__metadata("design:type", Property)
], TestClass.prototype, "property", void 0);
TestClass = __decorate([
Injectable()
], TestClass);
const instance = new Injector().getInstance(TestClass);
expect(() => (instance.property = new Property())).toThrowErrorMatchingInlineSnapshot(`[Error: Injected property 'TestClass.property' is read-only]`);
});
it('Should inject a property with a callback syntax', () => {
let Property = class Property {
foo = 3;
};
Property = __decorate([
Injectable()
], Property);
let TestClass = class TestClass {
initial = 2;
};
__decorate([
Injected(function (injector) {
return injector.getInstance(Property).foo + this.initial;
}),
__metadata("design:type", Number)
], TestClass.prototype, "property", void 0);
TestClass = __decorate([
Injectable()
], TestClass);
const instance = new Injector().getInstance(TestClass);
expect(instance.property).toBe(5);
});
it('Should throw an error when trying to modify the injected property with a callback syntax', () => {
let Property = class Property {
foo = 3;
};
Property = __decorate([
Injectable()
], Property);
let TestClass = class TestClass {
initial = 2;
};
__decorate([
Injected(function (injector) {
return injector.getInstance(Property).foo + this.initial;
}),
__metadata("design:type", Number)
], TestClass.prototype, "property", void 0);
TestClass = __decorate([
Injectable()
], TestClass);
const instance = new Injector().getInstance(TestClass);
expect(() => (instance.property = 3)).toThrowErrorMatchingInlineSnapshot(`[Error: Injected property 'TestClass.property' is read-only]`);
});
});
//# sourceMappingURL=injected.spec.js.map