77 lines
2.8 KiB
JavaScript
77 lines
2.8 KiB
JavaScript
/* globals describe, beforeEach, it */
|
|
/**
|
|
* Unit testing file for the feature flags
|
|
*/
|
|
'use strict';
|
|
const expect = require('chai').expect;
|
|
|
|
const featureFlags = require('./feature-flags.js');
|
|
|
|
describe('feature-flags', function() {
|
|
describe('defaults', function() {
|
|
it('should have `unit-test` flag', function() {
|
|
expect(featureFlags.flagsList).to.contain('unit-test');
|
|
});
|
|
});
|
|
|
|
//
|
|
// Test for all the cases that throw exceptions.
|
|
// NOTE: to catch the exception we must wrap the function in an anonymous
|
|
// function. Using ES6 arrow functions this just adds `() =>` to the call
|
|
//
|
|
describe('parameter verification', function() {
|
|
it('should throw for unspecified flag', function() {
|
|
expect(() => featureFlags.isEnabled('UNDECLARED FLAG NAME', {}))
|
|
.to.throw(/Flag <UNDECLARED FLAG NAME> not declared/);
|
|
});
|
|
|
|
it('should throw if not passed a second param', function() {
|
|
expect(() => featureFlags.isEnabled('unit-test'))
|
|
.to.throw(/Cannot test for flag as obj is not an object./);
|
|
});
|
|
|
|
it('should throw if passed an array rather than object as the second param', function() {
|
|
expect(() => featureFlags.isEnabled('unit-test', []))
|
|
.to.throw(/Cannot test for flag as obj is not an object./);
|
|
});
|
|
|
|
it('should throw if obj.FeatureFlags exists, bit is not an array', function() {
|
|
expect(() => featureFlags.isEnabled('unit-test', {FeatureFlags: 'A string'}))
|
|
.to.throw(/obj.FeatureFlags must be undefined or an array./);
|
|
});
|
|
});
|
|
|
|
describe('isEnabled', function() {
|
|
it('should return true if the flag is enabled', function() {
|
|
const objWithFlag = {
|
|
FeatureFlags: ['unit-test']
|
|
};
|
|
expect(featureFlags.isEnabled('unit-test', objWithFlag))
|
|
.to.equal(true);
|
|
});
|
|
|
|
it('should return false if the flag is not enabled, but others are', function() {
|
|
const objWithOtherFlag = {
|
|
FeatureFlags: ['something else']
|
|
};
|
|
expect(featureFlags.isEnabled('unit-test', objWithOtherFlag))
|
|
.to.equal(false);
|
|
});
|
|
|
|
it('should return false if no flags are enabled', function() {
|
|
const objEmptyFlagsArray = {
|
|
FeatureFlags: []
|
|
};
|
|
expect(featureFlags.isEnabled('unit-test', objEmptyFlagsArray))
|
|
.to.equal(false);
|
|
});
|
|
|
|
it('should return false if FeatureFlags is undefined', function() {
|
|
const objWithoutFlags = {
|
|
};
|
|
expect(featureFlags.isEnabled('unit-test', objWithoutFlags))
|
|
.to.equal(false);
|
|
});
|
|
});
|
|
});
|