ContainerTests.ts
74 lines
| 1.9 KiB
| video/mp2t
|
TypeScriptLexer
|
|
r41 | import { test, TapeWriter } from "./TestTraits"; | ||
|
|
r40 | import { Container } from "@implab/core/di/Container"; | ||
| import { ReferenceDescriptor } from "@implab/core/di/ReferenceDescriptor"; | ||||
| import { AggregateDescriptor } from "@implab/core/di/AggregateDescriptor"; | ||||
|
|
r41 | import { ValueDescriptor } from "@implab/core/di/ValueDescriptor"; | ||
| import { TraceSource, DebugLevel } from "@implab/core/log/TraceSource"; | ||||
| import { Foo } from "./mock/Foo"; | ||||
| import { Bar } from "./mock/Bar"; | ||||
| import { isNull } from "@implab/core/safe"; | ||||
|
|
r40 | |||
|
|
r41 | test("Container register/resolve tests", async t => { | ||
| const writer = new TapeWriter(t); | ||||
| TraceSource.on(ts => { | ||||
| ts.level = DebugLevel; | ||||
| writer.writeEvents(ts.events); | ||||
| }); | ||||
|
|
r40 | const container = new Container(); | ||
| const connection1 = "db://localhost"; | ||||
|
|
r41 | container.register("connection", new ValueDescriptor(connection1)); | ||
|
|
r40 | |||
| t.equals(container.getService("connection"), connection1); | ||||
| container.register( | ||||
| "dbParams", | ||||
| new AggregateDescriptor({ | ||||
| timeout: 10, | ||||
|
|
r41 | connection: new ReferenceDescriptor({ name: "connection" }) | ||
|
|
r40 | }) | ||
| ); | ||||
| const dbParams = container.getService("dbParams"); | ||||
|
|
r41 | t.equals(dbParams.connection, connection1, "should get connection"); | ||
| writer.destroy(); | ||||
| }); | ||||
| test("Container configure/resolve tests", async t => { | ||||
| const writer = new TapeWriter(t); | ||||
| TraceSource.on(ts => { | ||||
| ts.level = DebugLevel; | ||||
| writer.writeEvents(ts.events); | ||||
| }); | ||||
| const container = new Container(); | ||||
| await container.configure({ | ||||
| foo: { | ||||
| $type: Foo | ||||
| }, | ||||
|
|
r40 | |||
|
|
r41 | bar: { | ||
| $type: Bar, | ||||
| params: { | ||||
| db: { | ||||
| provider: { | ||||
| $dependency: "db" | ||||
| } | ||||
| } | ||||
| } | ||||
| } | ||||
| }); | ||||
| const f1 = container.resolve("foo"); | ||||
| t.assert(!isNull(f1), "foo should be not null"); | ||||
| const b1 = container.resolve("bar"); | ||||
| writer.destroy(); | ||||
|
|
r40 | }); | ||
