1+ 'use strict'
2+
3+ import iniettore from '../../src/iniettore'
4+ import { TRANSIENT , PERSISTENT , PROVIDER , SINGLETON , CONSTRUCTOR , EAGER } from '../../src/options'
5+
6+ describe ( 'Given a provider and a contructor' , function ( ) {
7+
8+ var provider = sinon . spy ( )
9+ var constructorSpy = sinon . spy ( )
10+
11+ class Foo {
12+ constructor ( ...args ) {
13+ constructorSpy . apply ( null , args )
14+ }
15+ }
16+
17+ beforeEach ( function ( ) {
18+ provider . reset ( )
19+ constructorSpy . reset ( )
20+ } )
21+
22+ describe ( 'when registering them as EAGER, SINGLETONs' , function ( ) {
23+
24+ it ( 'should create instances straightaway' , function ( ) {
25+
26+ iniettore . create ( function ( context ) {
27+ context
28+ . map ( 'bar' )
29+ . to ( provider )
30+ . as ( EAGER , SINGLETON , PROVIDER )
31+
32+ . map ( 'foo' )
33+ . to ( Foo )
34+ . as ( EAGER , SINGLETON , CONSTRUCTOR )
35+ } )
36+ expect ( provider ) . to . be . calledOnce
37+ expect ( constructorSpy ) . to . be . calledOnce
38+ } )
39+
40+ describe ( 'and $context as dependency' , function ( ) {
41+
42+ var context = sinon . match . object . and ( sinon . match . has ( 'map' , sinon . match . func ) )
43+
44+ it ( 'should provide a reference to the context' , function ( ) {
45+ iniettore . create ( function ( context ) {
46+ context
47+ . map ( 'bar' )
48+ . to ( provider )
49+ . as ( EAGER , SINGLETON , PROVIDER )
50+ . injecting ( '$context' )
51+
52+ . map ( 'foo' )
53+ . to ( Foo )
54+ . as ( EAGER , SINGLETON , CONSTRUCTOR )
55+ . injecting ( '$context' )
56+ } )
57+ expect ( provider )
58+ . to . be . calledOnce
59+ . and . to . be . calledWith ( context )
60+ expect ( constructorSpy )
61+ . to . be . calledOnce
62+ . and . to . be . calledWith ( context )
63+ } )
64+ } )
65+ } )
66+
67+ describe ( 'registered as simple CONSTRUCTOR and PROVIDER (not singleton)' , function ( ) {
68+ var container
69+
70+ describe ( 'with $context as dependency' , function ( ) {
71+
72+ before ( function ( ) {
73+ container = iniettore . create ( function ( context ) {
74+ context
75+ . map ( 'bar' )
76+ . to ( provider )
77+ . as ( TRANSIENT , SINGLETON , PROVIDER )
78+ . injecting ( '$context' )
79+
80+ . map ( 'foo' )
81+ . to ( Foo )
82+ . as ( PERSISTENT , SINGLETON , CONSTRUCTOR )
83+ . injecting ( '$context' )
84+ } )
85+ } )
86+ describe ( 'when requesting those' , function ( ) {
87+ it ( 'should throw an Error specifying that $context injection is available only for eager singletons' , function ( ) {
88+ function testCase1 ( ) {
89+ container . get ( 'foo' )
90+ }
91+ function testCase2 ( ) {
92+ container . get ( 'bar' )
93+ }
94+ expect ( testCase1 ) . to . throw ( Error )
95+ expect ( testCase2 ) . to . throw ( Error )
96+ } )
97+ } )
98+ } )
99+ } )
100+ } )
0 commit comments