@reliance/feature-flags
Version:
A package for determining feature flag status based on environment variables or flag objects.
49 lines (41 loc) • 1.54 kB
JavaScript
const {
PredicateNotAFunctionError,
InvalidFeatureDefinition,
} = require("../src/error");
const { setFeature, features } = require("../src/setFeature");
describe("setFeature", () => {
test("should add valid feature predicates to the features object", () => {
const localFeatures = {
newUI: (params) => params.isBetaUser === true,
adminDashboard: (params) => params.admin === true,
};
setFeature(localFeatures);
expect(features.newUI).toBeDefined();
expect(features.adminDashboard).toBeDefined();
expect(features.newUI({ isBetaUser: true })).toBe(true);
expect(features.adminDashboard({ admin: true })).toBe(true);
});
test("should throw error if feature Definitions is not an object", () => {
expect(() => {
setFeature(null);
}).toThrow("Feature definitions must be an object and cannot be null");
expect(() => {
setFeature("invalid");
}).toThrow("Feature definitions must be an object and cannot be null");
});
test("should throw PredicateNotAFunctionError if a feature value is not a function", () => {
const invalidFeatures = {
validFeature: () => true,
invalidFeature: "not-a-function",
};
expect(() => {
setFeature(invalidFeatures);
}).toThrow(PredicateNotAFunctionError);
});
test("should throw InvalidFeatureDefinition if not a valid feature object", () => {
const invalideFeature = "hello";
expect(() => {
setFeature(invalideFeature);
}).toThrow(InvalidFeatureDefinition);
});
});