|
| 1 | +import { constructor, createContainer, singleton } from '..' |
| 2 | + |
| 3 | +describe('constructor', () => { |
| 4 | + it('can be registered without parameters', async () => { |
| 5 | + class A {} |
| 6 | + const container = createContainer().register('A', constructor(A)) |
| 7 | + const a = await container.resolve('A') |
| 8 | + expect(a).toBeInstanceOf(A) |
| 9 | + }) |
| 10 | + |
| 11 | + it('can be registered with parameters', async () => { |
| 12 | + class A {} |
| 13 | + class B { |
| 14 | + constructor(readonly a: A) {} |
| 15 | + } |
| 16 | + class C { |
| 17 | + constructor(readonly b: B) {} |
| 18 | + } |
| 19 | + |
| 20 | + const container = createContainer() |
| 21 | + .register('A', constructor(A)) |
| 22 | + .register('B', constructor(B, 'A')) |
| 23 | + .register('C', singleton(constructor(C, 'B'))) |
| 24 | + |
| 25 | + // We abuse a bit this test to verify other tangential properties, like |
| 26 | + // uniqueness of instances. |
| 27 | + const b1 = await container.resolve('B') |
| 28 | + const b2 = await container.resolve('B') |
| 29 | + |
| 30 | + expect(b1).toBeInstanceOf(B) // That's the real test. |
| 31 | + expect(b2).toBeInstanceOf(B) |
| 32 | + expect(b1).not.toBe(b2) |
| 33 | + |
| 34 | + const c1 = await container.resolve('C') |
| 35 | + const c2 = await container.resolve('C') |
| 36 | + |
| 37 | + expect(c1).toBeInstanceOf(C) |
| 38 | + expect(c2).toBeInstanceOf(C) |
| 39 | + expect(c1).toBe(c2) |
| 40 | + }) |
| 41 | +}) |
0 commit comments