2017-07-28 17:32:46 +00:00
|
|
|
import createContainer from '../../src/container';
|
2017-05-08 09:52:11 +00:00
|
|
|
|
|
|
|
QUnit.module( 'container', {
|
|
|
|
beforeEach: function () {
|
|
|
|
this.container = createContainer();
|
|
|
|
this.factory = this.sandbox.stub();
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( '#has', function ( assert ) {
|
|
|
|
this.container.set( 'foo', this.factory );
|
|
|
|
|
|
|
|
assert.ok( this.container.has( 'foo' ) );
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( '#get', function ( assert ) {
|
|
|
|
var service = {},
|
|
|
|
that = this;
|
|
|
|
|
|
|
|
this.factory.returns( service );
|
|
|
|
|
|
|
|
this.container.set( 'foo', this.factory );
|
|
|
|
|
|
|
|
assert.strictEqual( service, this.container.get( 'foo' ) );
|
|
|
|
assert.strictEqual(
|
|
|
|
this.container,
|
|
|
|
this.factory.getCall( 0 ).args[ 0 ]
|
|
|
|
);
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
|
|
|
this.container.get( 'foo' );
|
|
|
|
|
|
|
|
assert.ok(
|
|
|
|
this.factory.calledOnce,
|
|
|
|
'It should memoize the result of the factory.'
|
|
|
|
);
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
|
|
|
assert.throws(
|
|
|
|
function () {
|
|
|
|
that.container.get( 'bar' );
|
|
|
|
},
|
|
|
|
/The service "bar" hasn't been defined./
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( '#get should handle values, not just functions', function ( assert ) {
|
|
|
|
this.container.set( 'foo', 'bar' );
|
|
|
|
|
|
|
|
assert.strictEqual( 'bar', this.container.get( 'foo' ) );
|
|
|
|
} );
|