92 lines
2.4 KiB
JavaScript
92 lines
2.4 KiB
JavaScript
'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.
|
|
container = testBase.requireNoCache('app/Container');
|
|
});
|
|
|
|
test('Multiple requires return the same instance', () => {
|
|
let container2 = testBase.require('app/Container');
|
|
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);
|
|
});
|
|
|
|
test('Attempt to get non-existent item throws error', () => {
|
|
let fn = () => container.get('aseiutj');
|
|
expect(fn).to.throw(Error, `Cannot find module 'aseiutj'`);
|
|
});
|
|
});
|
|
|
|
suite('get method require', () => {
|
|
test('Returns same object as testInclude', () => {
|
|
let containerFile = container.get('./bootstrap');
|
|
let testFile = testBase.require('app/bootstrap');
|
|
|
|
expect(containerFile).to.be.equal(testFile);
|
|
});
|
|
|
|
test('Returns same object as native require', () => {
|
|
let containerFile = container.get('express');
|
|
let nativeRequire = require('express');
|
|
|
|
expect(containerFile).to.be.equal(nativeRequire);
|
|
});
|
|
|
|
test('getHelper(foo) returns same object as get(helpers/foo)', () => {
|
|
let expected = container.get('helpers/promisify');
|
|
let actual = container.getHelper('promisify');
|
|
|
|
expect(expected).to.deep.equal(actual);
|
|
});
|
|
|
|
test('getBase(foo) returns same object as get(base/foo)', () => {
|
|
let expected = container.get('base/Model');
|
|
let actual = container.getBase('Model');
|
|
|
|
expect(expected).to.deep.equal(actual);
|
|
});
|
|
});
|
|
}); |