##// END OF EJS Templates
Merge
Merge

File last commit:

r158:7e27596a76a8 default
r162:563928fdf335 merge default
Show More
ContainerTests.ts
124 lines | 3.3 KiB | video/mp2t | TypeScriptLexer
/ src / test / ts / tests / ContainerTests.ts
cin
migrating tests to the new project structure
r89 import { test } from "./TestTraits";
import { Container } from "../di/Container";
import { ReferenceDescriptor } from "../di/ReferenceDescriptor";
import { AggregateDescriptor } from "../di/AggregateDescriptor";
import { ValueDescriptor } from "../di/ValueDescriptor";
import { Foo } from "../mock/Foo";
import { Bar } from "../mock/Bar";
import { isNull } from "../safe";
cin
Added test: Optional dependency with child container...
r158 import { Box } from "../mock/Box";
cin
migrating tests to the new project structure
r89
test("Container register/resolve tests", async t => {
cin
improved interfaces and more tight type checking
r120 const container = new Container<{
"bla-bla": string;
"connection": string;
"dbParams": {
timeout: number;
connection: string;
}
}>();
cin
migrating tests to the new project structure
r89
const connection1 = "db://localhost";
t.throws(
cin
corrected code to support ts strict mode...
r115 () => container.register("bla-bla", "bla-bla" as any),
cin
migrating tests to the new project structure
r89 "Do not allow to register anything other than descriptors"
);
t.doesNotThrow(
() => container.register("connection", new ValueDescriptor(connection1)),
"register ValueDescriptor"
);
t.equals(container.resolve("connection"), connection1, "resolve string value");
t.doesNotThrow(
() => container.register(
"dbParams",
new AggregateDescriptor({
timeout: 10,
connection: new ReferenceDescriptor({ name: "connection" })
})
),
"register AggregateDescriptor"
);
const dbParams = container.resolve("dbParams");
t.equals(dbParams.connection, connection1, "should get string value 'dbParams.connection'");
});
test("Container configure/resolve tests", async t => {
cin
improved interfaces and more tight type checking
r120 const container = new Container<{
foo: Foo;
box: Bar;
bar: Bar;
cin
working on fluent container configuration
r125 db: any;
cin
improved interfaces and more tight type checking
r120 }>();
cin
migrating tests to the new project structure
r89
await container.configure({
foo: {
$type: Foo
},
box: {
$type: Bar,
params: {
$dependency: "foo"
}
},
bar: {
$type: Bar,
cin
working on fluent container configuration
r125 params: [{
cin
migrating tests to the new project structure
r89 db: {
provider: {
$dependency: "db"
}
}
cin
working on fluent container configuration
r125 }]
cin
migrating tests to the new project structure
r89 }
});
t.pass("should configure from js object");
const f1 = container.resolve("foo");
t.assert(!isNull(f1), "foo should be not null");
t.throws(() => container.resolve("bar"), "should not resolve dependency 'db'");
});
test("Load configuration from module", async t => {
const container = new Container();
cin
Amd tests are passed
r91 await container.configure("../mock/config1", { contextRequire: require });
cin
migrating tests to the new project structure
r89 t.pass("The configuration should load");
const f1 = container.resolve("foo");
t.assert(!isNull(f1), "foo should be not null");
const b1 = container.resolve("bar") as Bar;
t.assert(!isNull(b1), "bar should not be null");
cin
Working on IoC container configuration
r111 t.assert(!isNull(b1._v), "bar.foo should not be null");
cin
configuration interfaces moved to di/Configuration module...
r118
cin
migrating tests to the new project structure
r89 });
cin
Added test: Optional dependency with child container...
r158
test("Optional dependency with child container", async t => {
const container = new Container<{
foo?: Foo;
box: Box<Foo>;
}>();
await container.fluent({
box: it => it.factory($ => new Box($("foo")))
});
const child = await container.createChildContainer()
.fluent({
foo: it => it.factory(() => new Foo())
})
const box = child.resolve("box");
t.assert(!isNull(box.getValue()), "'foo' dependency is declared in child container");
});