-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
372 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { expect } from 'chai'; | ||
import sinon from 'sinon'; | ||
import { getFeatureFlags, getFeatureFlagsString, isFeatureFlagPresent } from './feature-flag-utils'; | ||
|
||
describe('getFeatureFlagsString', () => { | ||
describe('returns the env var `FEATURE_FLAGS`', () => { | ||
const env = Object.assign({}, process.env); | ||
|
||
afterEach(() => { | ||
sinon.restore(); | ||
process.env = env; | ||
}); | ||
|
||
it('when the FEATURE_FLAGS env var is undefined', () => { | ||
process.env.FEATURE_FLAGS = undefined; | ||
|
||
expect(getFeatureFlagsString()).to.equal(undefined); | ||
}); | ||
|
||
it('when the FEATURE_FLAGS env var is defined', () => { | ||
process.env.FEATURE_FLAGS = 'flag1,flag2,flag3'; | ||
|
||
expect(getFeatureFlagsString()).to.equal('flag1,flag2,flag3'); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('getFeatureFlags', () => { | ||
describe('returns an array of flags', () => { | ||
const env = Object.assign({}, process.env); | ||
|
||
afterEach(() => { | ||
sinon.restore(); | ||
process.env = env; | ||
}); | ||
|
||
it('when the FEATURE_FLAGS env var is undefined', () => { | ||
process.env.FEATURE_FLAGS = undefined; | ||
|
||
expect(getFeatureFlags()).to.eql([]); | ||
}); | ||
|
||
it('when the FEATURE_FLAGS env var is defined', () => { | ||
process.env.FEATURE_FLAGS = 'flag1,flag2,flag3'; | ||
|
||
expect(getFeatureFlags()).to.eql(['flag1', 'flag2', 'flag3']); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('isFeatureFlagPresent', () => { | ||
describe('returns true', () => { | ||
const env = Object.assign({}, process.env); | ||
|
||
afterEach(() => { | ||
sinon.restore(); | ||
process.env = env; | ||
}); | ||
|
||
it('when one flag matches', () => { | ||
process.env.FEATURE_FLAGS = 'flag1,flag2,flag3'; | ||
|
||
expect(isFeatureFlagPresent(['flag2'])).to.equal(true); | ||
}); | ||
|
||
it('when two flags match', () => { | ||
process.env.FEATURE_FLAGS = 'flag1,flag2,flag3'; | ||
|
||
expect(isFeatureFlagPresent(['flag2', 'flag1'])).to.equal(true); | ||
}); | ||
|
||
it('when some, but not all, flags match', () => { | ||
process.env.FEATURE_FLAGS = 'flag1,flag2,flag3'; | ||
|
||
expect(isFeatureFlagPresent(['flag4', 'flag3'])).to.equal(true); | ||
}); | ||
}); | ||
|
||
describe('returns false', () => { | ||
const env = Object.assign({}, process.env); | ||
|
||
afterEach(() => { | ||
sinon.restore(); | ||
process.env = env; | ||
}); | ||
|
||
it('when no flags match', () => { | ||
process.env.FEATURE_FLAGS = 'flag1,flag2,flag3'; | ||
|
||
expect(isFeatureFlagPresent(['flag4'])).to.equal(false); | ||
}); | ||
|
||
it('when no flags specified', () => { | ||
process.env.FEATURE_FLAGS = 'flag1,flag2,flag3'; | ||
|
||
expect(isFeatureFlagPresent([])).to.equal(false); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Returns the raw feature flag string from the environment variable `REACT_APP_FEATURE_FLAGS`. | ||
* | ||
* @return {*} {(string | undefined)} | ||
*/ | ||
export const getFeatureFlagsString = (): string | undefined => { | ||
return process.env.FEATURE_FLAGS; | ||
}; | ||
|
||
/** | ||
* Returns a parsed array of feature flag strings from the environment variable `REACT_APP_FEATURE_FLAGS`. | ||
* | ||
* @return {*} {string[]} | ||
*/ | ||
export const getFeatureFlags = (): string[] => { | ||
const featureFlagsString = getFeatureFlagsString(); | ||
|
||
if (!featureFlagsString) { | ||
return []; | ||
} | ||
|
||
return featureFlagsString.split(','); | ||
}; | ||
|
||
/** | ||
* Returns `true` if at least one of the provided `featureFlags` is present in the environment variable | ||
* `REACT_APP_FEATURE_FLAGS`. | ||
* | ||
* @param {string[]} featureFlags | ||
* @return {*} {boolean} | ||
*/ | ||
export const isFeatureFlagPresent = (featureFlags: string[]): boolean => { | ||
return getFeatureFlags().some((flag) => featureFlags.includes(flag)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.