2017-07-28 17:32:46 +00:00
|
|
|
import createContainer from '../../src/container';
|
2017-05-08 09:52:11 +00:00
|
|
|
|
|
|
|
QUnit.module( 'container', {
|
2018-03-14 22:04:59 +00:00
|
|
|
beforeEach() {
|
2017-05-08 09:52:11 +00:00
|
|
|
this.container = createContainer();
|
|
|
|
this.factory = this.sandbox.stub();
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( '#has', function ( assert ) {
|
|
|
|
this.container.set( 'foo', this.factory );
|
|
|
|
|
2018-05-08 19:48:17 +00:00
|
|
|
assert.ok( this.container.has( 'foo' ), 'The container checks the factory.' );
|
2017-05-08 09:52:11 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( '#get', function ( assert ) {
|
2018-03-19 19:39:41 +00:00
|
|
|
const service = {};
|
2017-05-08 09:52:11 +00:00
|
|
|
|
|
|
|
this.factory.returns( service );
|
|
|
|
|
|
|
|
this.container.set( 'foo', this.factory );
|
|
|
|
|
|
|
|
assert.strictEqual( service, this.container.get( 'foo' ) );
|
|
|
|
assert.strictEqual(
|
|
|
|
this.container,
|
2018-05-08 19:48:17 +00:00
|
|
|
this.factory.getCall( 0 ).args[ 0 ],
|
|
|
|
'The container uses the factory.'
|
2017-05-08 09:52:11 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
|
|
|
this.container.get( 'foo' );
|
|
|
|
|
|
|
|
assert.ok(
|
|
|
|
this.factory.calledOnce,
|
|
|
|
'It should memoize the result of the factory.'
|
|
|
|
);
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
|
|
|
assert.throws(
|
2018-02-16 12:52:56 +00:00
|
|
|
() => { this.container.get( 'bar' ); },
|
2018-05-08 19:48:17 +00:00
|
|
|
/The service "bar" hasn't been defined./,
|
|
|
|
'The container throws an error when no factory exists.'
|
2017-05-08 09:52:11 +00:00
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( '#get should handle values, not just functions', function ( assert ) {
|
|
|
|
this.container.set( 'foo', 'bar' );
|
|
|
|
|
2018-05-08 19:48:17 +00:00
|
|
|
assert.strictEqual(
|
|
|
|
'bar',
|
|
|
|
this.container.get( 'foo' ),
|
|
|
|
'The container understands string values.'
|
|
|
|
);
|
2017-05-08 09:52:11 +00:00
|
|
|
} );
|