-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add registerInjections helper function for dependency injection
- Loading branch information
Showing
4 changed files
with
110 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"pumpit": minor | ||
--- | ||
|
||
create `registerInjections` helper function for an easier way to register dependencies for a class or a function |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { describe, expect, test } from "vitest" | ||
import { PumpIt, SCOPE } from "../pumpit" | ||
import { registerInjections } from "../utils" | ||
|
||
describe("injection helper", () => { | ||
describe("factory", () => { | ||
test("use helper to inject in to factory", () => { | ||
const pumpIt = new PumpIt() | ||
|
||
type Fn = ReturnType<typeof factory> | ||
|
||
function factory(a: TestA, b: TestB) { | ||
return () => { | ||
return { a, b } | ||
} | ||
} | ||
|
||
class TestA {} | ||
class TestB {} | ||
|
||
registerInjections(factory, [TestA, TestB]) | ||
|
||
pumpIt | ||
.bindFactory(factory, factory) | ||
.bindClass(TestA, TestA) | ||
.bindClass(TestB, TestB) | ||
|
||
const result = pumpIt.resolve<Fn>(factory)() | ||
|
||
expect(result.a).toBeInstanceOf(TestA) | ||
expect(result.b).toBeInstanceOf(TestB) | ||
}) | ||
}) | ||
|
||
describe("class", () => { | ||
test("use helper to inject in to class", () => { | ||
const pumpIt = new PumpIt() | ||
|
||
class TestA {} | ||
class TestB {} | ||
class TestC { | ||
constructor( | ||
public a: TestA, | ||
public b: TestB, | ||
) {} | ||
} | ||
|
||
registerInjections(TestC, [TestA, TestB]) | ||
|
||
pumpIt | ||
.bindClass(TestA, TestA) | ||
.bindClass(TestB, TestB) | ||
.bindClass(TestC, TestC) | ||
|
||
const result = pumpIt.resolve<TestC>(TestC) | ||
|
||
expect(result.a).toBeInstanceOf(TestA) | ||
expect(result.b).toBeInstanceOf(TestB) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters