2016-01-25 09:19:28 -05:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const testBase = require('../../test-base');
|
|
|
|
const expect = testBase.expect;
|
|
|
|
|
|
|
|
suite('Dependency Container tests', () => {
|
|
|
|
let container = null;
|
|
|
|
|
|
|
|
setup(() => {
|
|
|
|
// Delete cached version of container class that may have been required
|
|
|
|
// at an earlier time. The container module has a saved state to keep track
|
|
|
|
// of modules during normal use. Removing the cached version ensures that
|
|
|
|
// a new instance of the module is used.
|
2016-02-18 21:50:45 -05:00
|
|
|
container = testBase.requireNoCache('app/Container');
|
2016-01-25 09:19:28 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Multiple requires return the same instance', () => {
|
2016-02-18 21:50:45 -05:00
|
|
|
let container2 = testBase.require('app/Container');
|
2016-01-25 09:19:28 -05:00
|
|
|
expect(container2).to.be.equal(container);
|
|
|
|
});
|
|
|
|
|
|
|
|
suite('has method', () => {
|
|
|
|
setup(() => {
|
|
|
|
container.set('foobar', {
|
|
|
|
foo: {
|
|
|
|
bar: [1, 2, 3],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Item "foobar" exists', () => {
|
|
|
|
expect(container.has('foobar')).to.be.true;
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Item "abc" does not exist', () => {
|
|
|
|
expect(container.has('abc')).to.be.false;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
suite('Get/set functionality', () => {
|
|
|
|
let obj = {
|
|
|
|
foo: {
|
|
|
|
bar: [1, 2, 3],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
test('Set method returns Container', () => {
|
|
|
|
let actual = container.set('foobar', obj);
|
|
|
|
expect(actual).to.be.equal(container);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Get method returns set object', () => {
|
|
|
|
let actual = container.get('foobar');
|
|
|
|
expect(actual).to.be.equal(obj);
|
|
|
|
});
|
|
|
|
|
2016-02-18 21:50:45 -05:00
|
|
|
test('Attempt to get non-existent item returns undefined', () => {
|
|
|
|
expect(container.get('aseiutj')).to.be.undefined;
|
2016-01-25 09:19:28 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-02-18 21:50:45 -05:00
|
|
|
suite('get method require', () => {
|
2016-01-25 09:19:28 -05:00
|
|
|
test('Returns same object as testInclude', () => {
|
2016-02-18 21:50:45 -05:00
|
|
|
let containerFile = container.get('./bootstrap');
|
|
|
|
let testFile = testBase.require('app/bootstrap');
|
2016-01-25 09:19:28 -05:00
|
|
|
|
|
|
|
expect(containerFile).to.be.equal(testFile);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Returns same object as native require', () => {
|
2016-02-18 21:50:45 -05:00
|
|
|
let containerFile = container.get('express');
|
2016-01-25 09:19:28 -05:00
|
|
|
let nativeRequire = require('express');
|
|
|
|
|
|
|
|
expect(containerFile).to.be.equal(nativeRequire);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|