JSPM

dependency-injector-pattern

1.0.3
    • ESM via JSPM
    • ES Module Entrypoint
    • Export Map
    • Keywords
    • License
    • Repository URL
    • TypeScript Types
    • README
    • Created
    • Published
    • Downloads 2
    • Score
      100M100P100Q36599F

    Simply use dependency injector pattern in your project

    Package Exports

    • dependency-injector-pattern
    • dependency-injector-pattern/dist/index.js

    This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (dependency-injector-pattern) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

    Readme

    Dependency injector pattern library

    Install library

    npm i dependency-injector-pattern

    How to use

    @Injectable()
    class Foo {
        hello() {
            console.log('Hello')
        }
    }
    
    @Injectable()
    class Test {
        constructor(private readonly foo: Foo) {}
    
        test() {
            this.foo.hello()
        }
    }
    
    const testService = Injector.resolve(Test)
    testService.test()
    'Hello'

    How to test

    @Injectable()
    class Foo {
    
        mockedFunction() {
            return false
        }
    }
    
    @Injectable()
    class Test {
        constructor(private readonly foo: Foo) {}
    
        testInjection() {
            return this.foo.mockedFunction()
        }
    }
    
    const FooMock = {
        mockedFunction: jest.fn(() => true)
    }
    
    describe("Should test injection pattern", () => {
    
        let useTest: Test;
    
        beforeEach(() => {
            Injector.container.clear()
            Injector.mock("Foo", FooMock)
            useTest = Injector.resolve(Test)
            jest.clearAllMocks()
        })
        
        it("Should Test class exist", () => {
            expect(useTest).toBeTruthy()
        })
    
        it("Should call mocked function inject via dependency", () => {
            useTest.testInjection()
            expect(FooMock.mockedFunction).toHaveBeenCalled()
        })
    })

    You can now use injection pattern