-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
200 additions
and
0 deletions.
There are no files selected for viewing
200 changes: 200 additions & 0 deletions
200
tests/browser/packages/container/container/build.test.js
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,200 @@ | ||
import {Container, BindingEntry, ContainerError, CircularDependencyError } from "@aedart/container"; | ||
import { dependencies } from "@aedart/support/container"; | ||
|
||
describe('@aedart/support/container', () => { | ||
describe('build', () => { | ||
|
||
it('fails when concrete is not buildable', () => { | ||
const container = new Container(); | ||
|
||
const callback = () => { | ||
container.build('not_buildable'); | ||
} | ||
|
||
expect(callback) | ||
.toThrowError(ContainerError); | ||
}); | ||
|
||
it('can build constructor type', () => { | ||
const container = new Container(); | ||
|
||
class Service {} | ||
|
||
// -------------------------------------------------------------------- // | ||
|
||
const result = container.build(Service); | ||
|
||
expect(result) | ||
.toBeInstanceOf(Service); | ||
}); | ||
|
||
it('can build binding entry type', () => { | ||
const container = new Container(); | ||
|
||
class Service {} | ||
|
||
const binding = new BindingEntry('my_service', Service); | ||
|
||
// -------------------------------------------------------------------- // | ||
|
||
const result = container.build(binding); | ||
|
||
expect(result) | ||
.toBeInstanceOf(Service); | ||
}); | ||
|
||
it('passes arguments to factory callback', () => { | ||
const container = new Container(); | ||
|
||
class Cache { | ||
ttl; | ||
|
||
constructor(ttl) { | ||
this.ttl = ttl; | ||
} | ||
} | ||
|
||
const binding = new BindingEntry('my_service', (container, ...args) => { | ||
return new Cache(...args); | ||
}); | ||
|
||
// -------------------------------------------------------------------- // | ||
|
||
const ttl = 500; | ||
const result = container.build(binding, [ ttl ]); | ||
|
||
expect(result) | ||
.toBeInstanceOf(Cache); | ||
expect(result.ttl) | ||
.toBe(ttl); | ||
}); | ||
|
||
it('passes arguments to constructor', () => { | ||
const container = new Container(); | ||
|
||
class Cache { | ||
ttl; | ||
|
||
constructor(ttl) { | ||
this.ttl = ttl; | ||
} | ||
} | ||
|
||
const binding = new BindingEntry('my_service', Cache); | ||
|
||
// -------------------------------------------------------------------- // | ||
|
||
const ttl = 200; | ||
const result = container.build(binding, [ ttl ]); | ||
|
||
expect(result) | ||
.toBeInstanceOf(Cache); | ||
expect(result.ttl) | ||
.toBe(ttl); | ||
}); | ||
|
||
it('resolves dependencies for constructor', () => { | ||
const container = new Container(); | ||
|
||
class MyDriver {} | ||
|
||
@dependencies('my_driver') | ||
class Service { | ||
driver; | ||
|
||
constructor(driver) { | ||
this.driver = driver; | ||
} | ||
} | ||
|
||
container.bind('my_driver', MyDriver); | ||
|
||
// -------------------------------------------------------------------- // | ||
|
||
const result = container.build(Service); | ||
|
||
expect(result.driver) | ||
.toBeInstanceOf(MyDriver); | ||
}); | ||
|
||
it('resolves nested dependencies ', () => { | ||
const container = new Container(); | ||
|
||
class A {} | ||
|
||
@dependencies('a') | ||
class B { | ||
item; | ||
|
||
constructor(item) { | ||
this.item = item; | ||
} | ||
} | ||
|
||
@dependencies('b') | ||
class C { | ||
item; | ||
|
||
constructor(item) { | ||
this.item = item; | ||
} | ||
} | ||
|
||
container | ||
.bind('a', A) | ||
.bind('b', B); | ||
|
||
// -------------------------------------------------------------------- // | ||
|
||
const result = container.build(C); | ||
|
||
expect(result.item) | ||
.withContext('B was expected as resolved dependency') | ||
.toBeInstanceOf(B); | ||
|
||
expect(result.item.item) | ||
.withContext('A was expected as nested resolved dependency') | ||
.toBeInstanceOf(A); | ||
}); | ||
|
||
it('fails on circular dependency', () => { | ||
const container = new Container(); | ||
|
||
// NOTE: This causes a circular dependency when resolving... | ||
@dependencies('c') | ||
class A {} | ||
|
||
@dependencies('a') | ||
class B { | ||
item; | ||
|
||
constructor(item) { | ||
this.item = item; | ||
} | ||
} | ||
|
||
@dependencies('b') | ||
class C { | ||
item; | ||
|
||
constructor(item) { | ||
this.item = item; | ||
} | ||
} | ||
|
||
container | ||
.bind('a', A) | ||
.bind('b', B) | ||
.bind('c', C); | ||
|
||
// -------------------------------------------------------------------- // | ||
|
||
const callback = () => { | ||
return container.build(C); | ||
} | ||
|
||
expect(callback) | ||
.toThrowError(CircularDependencyError); | ||
}); | ||
}); | ||
}); |