Giter Site home page Giter Site logo

dependency-injection-decorator's Introduction

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

dependency-injection-decorator's People

Contributors

rmingon avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.