This repository has been archived on 2018-10-12. You can view files and clone it, but cannot push or open issues or pull requests.
ProgBlog/test/unit/base/Config_test.js
2016-02-19 12:58:16 -05:00

109 lines
2.8 KiB
JavaScript

'use strict';
const testBase = require('../../test-base');
const expect = testBase.expect;
const glob = require('glob');
const path = require('path');
suite('Config class tests', () => {
let config = null;
setup(() => {
config = testBase.require('app/base/Config');
});
test('Get a non-existent config value', () => {
let actual = config.get('foo');
expect(actual).to.be.undefined;
});
suite('Value cascade', () => {
suite('Explicit values are loaded first', () => {
setup(() => {
config._set('foo', 'bar');
});
test(`'foo' exists in config`, () => {
expect(config.has('foo')).to.be.true;
});
test(`value of 'foo' is returned by get method`, () => {
expect(config.get('foo')).to.be.equal('bar');
});
});
suite('Environment values are loaded before defaults', () => {
setup(() => {
process.env.BAR = 'baz';
});
test(`'bar' is not directly defined in config`, () => {
expect(config.has('bar')).to.be.false;
});
test(`'bar' is defined in environment`, () => {
expect(config.hasEnv('bar')).to.be.true;
expect(config.getEnv('bar')).to.be.equal(process.env.BAR);
});
test(`Gets 'bar' from environment`, () => {
expect(config.get('bar')).to.be.equal(config.getEnv('bar'));
});
test(`'foo' is now cached in config`, () => {
expect(config.has('bar')).to.be.true;
});
teardown(() => {
delete process.env.BAR;
});
});
suite('Default values are loaded as a last resort', () => {
setup(() => {
delete process.env.HOST;
});
test(`'host' is not directly defined in config`, () => {
expect(config.has('host')).to.be.false;
});
test(`'host' is not defined in environment`, () => {
expect(config.hasEnv('host')).to.be.false;
});
test(`Get default value of 'host'`, () => {
expect(config.get('host')).to.equal('localhost');
});
test(`'host' is now cached in config`, () => {
expect(config.has('host')).to.be.true;
});
});
});
suite('normalizeValue', () => {
test(`'true' returns true`, () => {
let val = config._normalizeValue('true');
expect(val).to.be.true;
});
test(`'false' returns false`, () => {
let val = config._normalizeValue('false');
expect(val).to.be.false;
});
test(`'436' returns 436`, () => {
let val = config._normalizeValue('436');
expect(val).to.be.equal(436);
});
test(`'foo' returns 'foo'`, () => {
let val = config._normalizeValue('foo');
expect(val).to.be.equal('foo');
});
});
test('Keys returns values in config', () => {
// Get the list of keys for files
let configFiles = glob.sync(
path.resolve(__dirname, '../../../app/config/**/*.js')
);
let expected = configFiles.map((fullPath) => {
return path.basename(fullPath, '.js');
});
let actual = config.keys();
expect(actual).to.be.deep.equal(expected);
});
});