UNPKG

@furystack/inject

Version:
60 lines 2.74 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; }; import { Injectable, InjectableOptionsSymbol, getInjectableOptions, hasInjectableOptions } from './injectable.js'; import { describe, expect, it } from 'vitest'; describe('hasInjectableOptions', () => { it('Should return true if the object has the InjectableOptionsSymbol', () => { class Alma { } Object.assign(Alma, { [InjectableOptionsSymbol]: { lifetime: 'singleton' } }); expect(hasInjectableOptions(Alma)).toBe(true); }); it('Should return false if the object does not have the InjectableOptionsSymbol', () => { class Alma { } expect(hasInjectableOptions(Alma)).toBe(false); }); it('Should return false if the object is not an object', () => { expect(hasInjectableOptions('')).toBe(false); }); }); describe('getInjectableOptions', () => { it('Should throw an error if the object does not have the InjectableOptionsSymbol', () => { class Alma { } expect(() => getInjectableOptions(Alma)).toThrowError("The class 'Alma' is not an injectable"); }); it('Should return the options if the object has the InjectableOptionsSymbol', () => { class Alma { } Object.assign(Alma, { [InjectableOptionsSymbol]: { lifetime: 'singleton' } }); expect(getInjectableOptions(Alma)).toEqual({ lifetime: 'singleton' }); }); }); describe('@Injectable()', () => { it('Should attach the default options by default', () => { let TestClass1 = class TestClass1 { }; TestClass1 = __decorate([ Injectable() ], TestClass1); const meta = getInjectableOptions(TestClass1); expect(meta).toBeDefined(); expect(meta?.lifetime).toBe('transient'); }); it('Should attach the explicitly set options', () => { let TestClass2 = class TestClass2 { }; TestClass2 = __decorate([ Injectable({ lifetime: 'scoped' }) ], TestClass2); const meta = getInjectableOptions(TestClass2); expect(meta).toBeDefined(); expect(meta?.lifetime).toBe('scoped'); }); }); //# sourceMappingURL=injectable.spec.js.map