Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iamigo committed Oct 31, 2016
1 parent 34b8d7f commit 79ff549
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
18 changes: 11 additions & 7 deletions config/toggles.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
'use strict'; // eslint-disable-line strict
const featureToggles = require('feature-toggles');
const pe = process.env; // eslint-disable-line no-process-env
const nodeEnv = pe.NODE_ENV || 'development';

/**
* Return boolean true if the named environment variable is boolean true or
Expand All @@ -31,8 +30,8 @@ const nodeEnv = pe.NODE_ENV || 'development';
* @returns {Boolean} true if the named environment variable is boolean true or
* case-insensitive string 'true'.
*/
function environmentVariableTrue(environmentVariableName) {
const x = pe[environmentVariableName];
function environmentVariableTrue(processEnv, environmentVariableName) {
const x = processEnv[environmentVariableName];
return typeof x !== 'undefined' && x !== null &&
x.toString().toLowerCase() === 'true';
} // environmentVariableTrue
Expand All @@ -42,16 +41,21 @@ function environmentVariableTrue(environmentVariableName) {
*/
const toggles = {
// Enable caching for GET /v1/perspectives/{key}?
enableCachePerspective: environmentVariableTrue('ENABLE_CACHE_PERSPECTIVE'),
enableCachePerspective: environmentVariableTrue(pe,
'ENABLE_CACHE_PERSPECTIVE'),

// Enable heroku clock dyno
enableClockDyno: environmentVariableTrue('HEROKU_CLOCK_DYNO'),
enableClockDyno: environmentVariableTrue(pe, 'HEROKU_CLOCK_DYNO'),

// Enable GET /v1/subjects?tags=...
filterSubjByTags: environmentVariableTrue('FILTER_SUBJ_BY_TAGS'),
filterSubjByTags: environmentVariableTrue(pe, 'FILTER_SUBJ_BY_TAGS'),

// Enable bulk upsert optimization
optimizeUpsert: environmentVariableTrue('OPTIMIZE_UPSERT'),
optimizeUpsert: environmentVariableTrue(pe, 'OPTIMIZE_UPSERT'),
};

featureToggles.load(toggles);

module.exports = {
environmentVariableTrue, // exporting to make it easy to test
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"test-realtime": "mocha -R dot --recursive tests/realtime",
"test-subj-tag-filter": "FILTER_SUBJ_BY_TAGS=true mocha -R dot --recursive tests/subjectTagFilter",
"test-view": "NODE_ENV=test mocha -R dot --recursive --compilers js:babel-core/register --require ./tests/view/setup.js tests/view",
"test": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R dot --recursive tests/api tests/db && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage && npm run test-view && npm run test-realtime && npm run test-token-req && npm run test-token-notreq && npm run test-disablehttp && npm run test-subj-tag-filter",
"test": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R dot --recursive tests/api tests/db tests/config && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage && npm run test-view && npm run test-realtime && npm run test-token-req && npm run test-token-notreq && npm run test-disablehttp && npm run test-subj-tag-filter",
"postinstall": "NODE_ENV=production gulp browserifyViews && gulp movecss && gulp movesocket && gulp movelensutil && npm run checkdb",
"prestart": "npm run checkprivatedb"
},
Expand Down
34 changes: 34 additions & 0 deletions tests/config/toggles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Copyright (c) 2016, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or
* https://opensource.org/licenses/BSD-3-Clause
*/

/**
* tests/config/toggles.js
*/
const expect = require('chai').expect;
const toggles = require('../../config/toggles');

describe('toggles', () => {
it('environmentVariableTrue', (done) => {
const env = {
foo: true,
bar: false,
baz: null,
foobar: 'True',
barbaz: 'falSE',
};
expect(toggles.environmentVariableTrue(env, 'foo')).to.equal(true);
expect(toggles.environmentVariableTrue(env, 'bar')).to.equal(false);
expect(toggles.environmentVariableTrue(env, 'baz')).to.equal(false);
expect(toggles.environmentVariableTrue(env, 'foobar')).to.equal(true);
expect(toggles.environmentVariableTrue(env, 'barbaz')).to.equal(false);
expect(toggles.environmentVariableTrue(env, '')).to.equal(false);
expect(toggles.environmentVariableTrue(env)).to.equal(false);
expect(toggles.environmentVariableTrue(env, 'hello')).to.equal(false);
done();
});
});

0 comments on commit 79ff549

Please sign in to comment.