-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
restore feature flag logic so it can be used in future
- Loading branch information
Showing
3 changed files
with
112 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,74 @@ | ||
import { | ||
FeatureFlagEnum, | ||
getIsUserEligibleForNewFeature | ||
} from "../featureFlag"; | ||
import { FiscalCode } from "@pagopa/ts-commons/lib/strings"; | ||
import { aFiscalCode, anotherFiscalCode } from "../../__mocks__/mocks"; | ||
|
||
const betaUsers: FiscalCode[] = [aFiscalCode]; | ||
const isUserBeta = (fc: FiscalCode) => betaUsers.includes(fc); | ||
|
||
describe("isUserForFeatureFlag", () => { | ||
it("should return true when featureFlag === all", () => { | ||
const isUserForFeatureFlag = getIsUserEligibleForNewFeature( | ||
isUserBeta, | ||
_ => false, | ||
FeatureFlagEnum.ALL | ||
); | ||
expect(isUserForFeatureFlag(aFiscalCode)).toBeTruthy(); | ||
}); | ||
|
||
it("should return false when featureFlag === beta and the user is not beta", () => { | ||
const isUserForFeatureFlag = getIsUserEligibleForNewFeature( | ||
isUserBeta, | ||
_ => false, | ||
FeatureFlagEnum.BETA | ||
); | ||
expect(isUserForFeatureFlag(anotherFiscalCode)).toBeFalsy(); | ||
}); | ||
|
||
it("should return true when featureFlag === beta and the first callback return true", () => { | ||
const isUserForFeatureFlag = getIsUserEligibleForNewFeature( | ||
isUserBeta, | ||
_ => false, | ||
FeatureFlagEnum.BETA | ||
); | ||
expect(isUserForFeatureFlag(aFiscalCode)).toBeTruthy(); | ||
}); | ||
|
||
it("should return false when featureFlag === canary and callbacks return false", () => { | ||
const isUserForFeatureFlag = getIsUserEligibleForNewFeature( | ||
isUserBeta, | ||
_ => false, | ||
FeatureFlagEnum.CANARY | ||
); | ||
expect(isUserForFeatureFlag(anotherFiscalCode)).toBeFalsy(); | ||
}); | ||
|
||
it("should return true when featureFlag === canary and the first callback return true", () => { | ||
const isUserForFeatureFlag = getIsUserEligibleForNewFeature( | ||
isUserBeta, | ||
_ => false, | ||
FeatureFlagEnum.CANARY | ||
); | ||
expect(isUserForFeatureFlag(aFiscalCode)).toBeTruthy(); | ||
}); | ||
|
||
it("should return true when featureFlag === canary and the second callback return true", () => { | ||
const isUserForFeatureFlag = getIsUserEligibleForNewFeature( | ||
isUserBeta, | ||
_ => true, | ||
FeatureFlagEnum.CANARY | ||
); | ||
expect(isUserForFeatureFlag(anotherFiscalCode)).toBeTruthy(); | ||
}); | ||
|
||
it("should return false when featureFlag === none", () => { | ||
const isUserForFeatureFlag = getIsUserEligibleForNewFeature( | ||
isUserBeta, | ||
_ => true, | ||
FeatureFlagEnum.NONE | ||
); | ||
expect(isUserForFeatureFlag(aFiscalCode)).toBeFalsy(); | ||
}); | ||
}); |
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,35 @@ | ||
import { enumType } from "@pagopa/ts-commons/lib/types"; | ||
import * as t from "io-ts"; | ||
|
||
export enum FeatureFlagEnum { | ||
ALL = "ALL", | ||
BETA = "BETA", | ||
CANARY = "CANARY", | ||
NONE = "NONE" | ||
} | ||
|
||
export const FeatureFlag = enumType<FeatureFlagEnum>( | ||
FeatureFlagEnum, | ||
"FeatureFlag" | ||
); | ||
|
||
export type FeatureFlag = t.TypeOf<typeof FeatureFlag>; | ||
|
||
export const getIsUserEligibleForNewFeature = <T>( | ||
isUserBeta: (i: T) => boolean, | ||
isUserCanary: (i: T) => boolean, | ||
featureFlag: FeatureFlag | ||
): ((i: T) => boolean) => (i): boolean => { | ||
switch (featureFlag) { | ||
case "ALL": | ||
return true; | ||
case "BETA": | ||
return isUserBeta(i); | ||
case "CANARY": | ||
return isUserCanary(i) || isUserBeta(i); | ||
case "NONE": | ||
return false; | ||
default: | ||
return false; | ||
} | ||
}; |