-
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
98 additions
and
0 deletions.
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
tests/browser/packages/container/container/contextual-bindings.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,98 @@ | ||
import { Container } from "@aedart/container"; | ||
import { dependencies } from "@aedart/support/container"; | ||
|
||
describe('@aedart/support/container', () => { | ||
describe('addContextualBinding', () => { | ||
|
||
it('register and resolve contextual binding', () => { | ||
const container = new Container(); | ||
|
||
class A {} | ||
|
||
class B {} | ||
|
||
@dependencies(A) | ||
class C { | ||
resolved; | ||
constructor(resolved) { | ||
this.resolved = resolved; | ||
} | ||
} | ||
|
||
// ----------------------------------------------------------------- // | ||
|
||
container | ||
.when(C) | ||
.needs(A) | ||
.give(B); | ||
|
||
// ----------------------------------------------------------------- // | ||
|
||
expect(container.hasContextualBindings(C)) | ||
.withContext('Container SHOULD have contextual bindings registered for C') | ||
.toBeTrue(); | ||
|
||
expect(container.hasContextualBinding(C, A)) | ||
.withContext('Container SHOULD have a contextual binding registered') | ||
.toBeTrue(); | ||
|
||
const result = container.make(C); | ||
expect(result) | ||
.withContext('Incorrect instance resolved (C expected)') | ||
.toBeInstanceOf(C); | ||
|
||
expect(result.resolved) | ||
.withContext('Incorrect dependency resolved (B expected) for C') | ||
.toBeInstanceOf(B); | ||
}); | ||
|
||
it('can register contextual binding for multiple constructors', () => { | ||
const container = new Container(); | ||
|
||
const identifier = 'storage'; | ||
|
||
@dependencies(identifier) | ||
class ServiceA { | ||
storage; | ||
|
||
constructor(storage) { | ||
this.storage = storage; | ||
} | ||
} | ||
|
||
@dependencies(identifier) | ||
class ServiceB { | ||
storage; | ||
|
||
constructor(storage) { | ||
this.storage = storage; | ||
} | ||
} | ||
|
||
class MyStorage {} | ||
|
||
// ----------------------------------------------------------------- // | ||
|
||
const storage = new MyStorage(); | ||
container | ||
.when(ServiceA, ServiceB) | ||
.needs(identifier) | ||
.give(() => { | ||
return storage; | ||
}); | ||
|
||
// ----------------------------------------------------------------- // | ||
|
||
const a = container.make(ServiceA); | ||
const b = container.make(ServiceB); | ||
|
||
expect(a.storage) | ||
.withContext('Incorrect resolved for A') | ||
.toBe(storage); | ||
|
||
expect(b.storage) | ||
.withContext('Incorrect resolved for B') | ||
.toBe(storage); | ||
}); | ||
}); | ||
}); |