Comparing version 1.0.0 to 1.0.1
{ | ||
"name": "@owja/ioc", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "dependency injection for javascript", | ||
"main": "dist/ioc.js", | ||
"module": "dist/ioc.mjs", | ||
"module": "dist/ioc.module.js", | ||
"source": "src/index.ts", | ||
@@ -35,15 +35,15 @@ "types": "dist/index.d.ts", | ||
"author": "Hauke Broer <info@owja.de>", | ||
"license": "CC-BY-4.0", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"@types/jest": "^24.0.19", | ||
"@typescript-eslint/eslint-plugin": "^2.4.0", | ||
"@typescript-eslint/parser": "^2.4.0", | ||
"eslint": "^6.5.1", | ||
"eslint-plugin-jest": "^22.19.0", | ||
"jest": "^24.9.0", | ||
"@types/jest": "^25.2.1", | ||
"@typescript-eslint/eslint-plugin": "^2.27.0", | ||
"@typescript-eslint/parser": "^2.27.0", | ||
"eslint": "^6.8.0", | ||
"eslint-plugin-jest": "^23.8.2", | ||
"jest": "^25.3.0", | ||
"microbundle": "^0.11.0", | ||
"prettier": "^1.18.2", | ||
"ts-jest": "^24.1.0", | ||
"ts-node": "^8.4.1", | ||
"typescript": "^3.6.4" | ||
"prettier": "1.19.1", | ||
"ts-jest": "^25.3.1", | ||
"ts-node": "^8.8.2", | ||
"typescript": "^3.8.3" | ||
}, | ||
@@ -54,4 +54,5 @@ "mangle": { | ||
"files": [ | ||
"/dist" | ||
"/dist", | ||
"!/dist/example" | ||
] | ||
} |
120
README.md
@@ -251,3 +251,3 @@ ![OWJA! IoC](resources/owja-ioc-logo.png) | ||
#### Step 2 - Create symbols for our dependencies | ||
#### Step 2 - Creating symbols for our dependencies | ||
@@ -257,9 +257,35 @@ Now we create the folder ***services*** and add the new file ***services/types.ts***: | ||
export const TYPE = { | ||
"MyService" = Symbol("MyService"), | ||
"MyOtherService" = Symbol("MyOtherService"), | ||
MyService: Symbol("MyService"), | ||
MyOtherService: Symbol("MyOtherService"), | ||
}; | ||
``` | ||
#### Step 3 - Creating a container | ||
#### Step 3 - Example services | ||
Next we create out example services. | ||
File ***services/my-service.ts*** | ||
```ts | ||
export interface MyServiceInterface { | ||
hello: string; | ||
} | ||
export class MyService implements MyServiceInterface{ | ||
hello = "world"; | ||
} | ||
``` | ||
File ***services/my-other-service.ts*** | ||
```ts | ||
export interface MyOtherServiceInterface { | ||
random: number; | ||
} | ||
export class MyOtherService implements MyOtherServiceInterface { | ||
random = Math.random(); | ||
} | ||
``` | ||
#### Step 4 - Creating a container | ||
Next we need a container to bind our dependencies to. Let's create the file ***services/container.ts*** | ||
@@ -284,3 +310,3 @@ | ||
#### Step 4 - Injecting dependencies | ||
#### Step 5 - Injecting dependencies | ||
@@ -290,3 +316,3 @@ Lets create a ***example.ts*** file in our source root: | ||
```ts | ||
import {container, TYPE, inject} from "./services/container"; | ||
import {TYPE, inject} from "./service/container"; | ||
import {MyServiceInterface} from "./service/my-service"; | ||
@@ -298,4 +324,4 @@ import {MyOtherServiceInterface} from "./service/my-other-service"; | ||
readonly myService!: MyServiceInterface; | ||
@inject(TYPE.MyOtherSerice) | ||
@inject(TYPE.MyOtherService) | ||
readonly myOtherService!: MyOtherServiceInterface; | ||
@@ -307,3 +333,4 @@ } | ||
console.log(example.myService); | ||
console.log(example.myOtherSerice); | ||
console.log(example.myOtherService); | ||
console.log(example.myOtherService); | ||
``` | ||
@@ -314,14 +341,11 @@ | ||
The dependencies (services) will injected on the first call. This means if you rebind the service after | ||
accessing the properties of the Example class, it will not resolve the new service. If you want a new | ||
accessing the properties of the Example class, it will not resolve a new service. If you want a new | ||
service each time you call `example.myService` you have to add the `NOCACHE` tag: | ||
```ts | ||
import {container, TYPE, inject} from "./services/container"; | ||
// [...] | ||
import {NOCACHE} from "@owja/ioc"; | ||
// [...] | ||
class Example { | ||
@inject(TYPE.MyService, NOCACHE) | ||
readonly myService!: MyServiceInterface; | ||
// [...] | ||
@@ -335,21 +359,63 @@ @inject(TYPE.MyOtherSerice, NOCACHE) | ||
In this case the last two `console.log()` outputs should show different numbers. | ||
## Unit testing with IoC | ||
We can snapshot and restore a container for unit testing. | ||
We are able to make multiple snapshots in a row too. | ||
For unit testing we first create our mocks | ||
***test/my-service-mock.ts*** | ||
```ts | ||
import {container, TYPE} from "./services/container"; | ||
import {MyServiceInterface} from "../service/my-service"; | ||
beforeEach(() => { | ||
container.snapshot(); | ||
}); | ||
export class MyServiceMock implements MyServiceInterface { | ||
hello = "test"; | ||
} | ||
``` | ||
afterEach(() => { | ||
container.restore(); | ||
***test/my-other-service-mock.ts*** | ||
```ts | ||
import {MyOtherServiceInterface} from "../service/my-other-service"; | ||
export class MyOtherServiceMock implements MyOtherServiceInterface { | ||
random = 9; | ||
} | ||
``` | ||
test("can do something", () => { | ||
container.rebind<MyServiceMock>(TYPE.MySerice).toValue(new MyServiceMock()); | ||
const mock = container.get<MyServiceMock>(TYPE.MySerice); | ||
Within the tests we can snapshot and restore a container. | ||
We are able to make multiple snapshots in a row too. | ||
File ***example.test.ts*** | ||
```ts | ||
import {container, TYPE} from "./service/container"; | ||
import {MyServiceInterface} from "./service/my-service"; | ||
import {MyOtherServiceInterface} from "./service/my-other-service"; | ||
import {MyServiceMock} from "./test/my-service-mock"; | ||
import {MyOtherServiceMock} from "./test/my-other-service-mock"; | ||
import {Example} from "./example"; | ||
describe("Example", () => { | ||
let example: Example; | ||
beforeEach(() => { | ||
container.snapshot(); | ||
container.rebind<MyServiceInterface>(TYPE.MyService).to(MyServiceMock); | ||
container.rebind<MyOtherServiceInterface>(TYPE.MyOtherService).to(MyOtherServiceMock); | ||
example = new Example(); | ||
}); | ||
afterEach(() => { | ||
container.restore(); | ||
}); | ||
test("should return \"test\"", () => { | ||
expect(example.myService.hello).toBe("test"); | ||
}); | ||
test("should return \"9\"", () => { | ||
expect(example.myOtherService.random).toBe(9); | ||
}); | ||
}); | ||
@@ -375,4 +441,4 @@ ``` | ||
License under [Creative Commons Attribution 4.0 International](https://spdx.org/licenses/CC-BY-4.0.html) | ||
**MIT** | ||
Copyright © 2019 Hauke Broer |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
436
33596