-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for feature flags in react-core (#662)
- Loading branch information
1 parent
f46c441
commit d447834
Showing
5 changed files
with
109 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { hasFlag, setFlags, clearFlags } from '../../src/utils/featureFlags'; | ||
|
||
describe('Feature flags', () => { | ||
afterEach(() => { | ||
clearFlags(); | ||
}); | ||
|
||
test('are not set initially', () => { | ||
expect(hasFlag('A')).toStrictEqual(false); | ||
expect(hasFlag('B')).toStrictEqual(false); | ||
expect(hasFlag('C')).toStrictEqual(false); | ||
}); | ||
|
||
test('can be set using an array of strings', () => { | ||
setFlags(['A', 'B']); | ||
expect(hasFlag('A')).toStrictEqual(true); | ||
expect(hasFlag('B')).toStrictEqual(true); | ||
expect(hasFlag('C')).toStrictEqual(false); | ||
|
||
setFlags([]); | ||
expect(hasFlag('A')).toStrictEqual(false); | ||
expect(hasFlag('B')).toStrictEqual(false); | ||
expect(hasFlag('C')).toStrictEqual(false); | ||
}); | ||
|
||
test('can be set using an object', () => { | ||
setFlags({ | ||
A: 10, | ||
B: true, | ||
C: false, | ||
D: undefined, | ||
E: null, | ||
F: [], | ||
G: {} | ||
}); | ||
expect(hasFlag('A')).toStrictEqual(true); | ||
expect(hasFlag('B')).toStrictEqual(true); | ||
expect(hasFlag('C')).toStrictEqual(false); | ||
expect(hasFlag('D')).toStrictEqual(false); | ||
expect(hasFlag('E')).toStrictEqual(false); | ||
expect(hasFlag('F')).toStrictEqual(true); | ||
expect(hasFlag('G')).toStrictEqual(true); | ||
|
||
setFlags({}); | ||
expect(hasFlag('A')).toStrictEqual(false); | ||
expect(hasFlag('B')).toStrictEqual(false); | ||
expect(hasFlag('C')).toStrictEqual(false); | ||
}); | ||
|
||
test('are cleared', () => { | ||
setFlags(['A']); | ||
expect(hasFlag('A')).toStrictEqual(true); | ||
clearFlags(); | ||
expect(hasFlag('A')).toStrictEqual(false); | ||
}); | ||
|
||
const invaild = [10, '', 'A', false, { '': 10 }, ['A', '']]; | ||
test.each(invaild)('fail in case of invalid flags %p', (x) => { | ||
const t = () => { | ||
setFlags(x); | ||
}; | ||
expect(t).toThrow(); | ||
}); | ||
}); |
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,3 @@ | ||
export function setFlags(flags: Record<string, any> | string[]): void | ||
export function clearFlags(): void | ||
export function hasFlag(flag: string): boolean |
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,30 @@ | ||
let featureFlags = []; | ||
|
||
export function setFlags(flags) { | ||
const isValidFlag = (f) => typeof f === 'string' && f; | ||
|
||
if (Array.isArray(flags) && flags.every(isValidFlag)) { | ||
featureFlags = flags; | ||
} else if ( | ||
!Array.isArray(flags) && | ||
typeof flags === 'object' && | ||
Object.keys(flags).every(isValidFlag) | ||
) { | ||
featureFlags = []; | ||
for (const [flag, value] of Object.entries(flags)) { | ||
if (value) { | ||
featureFlags.push(flag); | ||
} | ||
} | ||
} else { | ||
throw new Error(`Invalid feature flags: ${flags}`); | ||
} | ||
} | ||
|
||
export function clearFlags() { | ||
featureFlags = []; | ||
} | ||
|
||
export function hasFlag(flag) { | ||
return featureFlags.includes(flag); | ||
} |