dependency-injector-pattern
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

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

Dependents (0)

Package Sidebar

Install

npm i dependency-injector-pattern

Weekly Downloads

6

Version

1.0.3

License

none

Unpacked Size

5.62 kB

Total Files

10

Last publish

Collaborators

  • rmingon