UNPKG

feature-management

Version:

Feature Flag Management

71 lines 3.39 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; Object.defineProperty(exports, "__esModule", { value: true }); const feature_flag_decorator_1 = require("../src/decorator/feature-flag.decorator"); const feature_manager_1 = require("../src/feature-manager"); const feature_flag_store_1 = require("../src/feature-flag.store"); const strategy_handler_decorator_1 = require("../src/decorator/strategy-handler.decorator"); describe('Feature Flag Strategy', () => { class AllowedBrowser { constructor(browsers) { this.browsers = browsers; } } let _AllowBrowserHandler = class _AllowBrowserHandler { async evaluate(filter, context) { if (!context.browser) throw new Error('Allow browser filter requires param: browser'); return filter.browsers.includes(context.browser); } }; _AllowBrowserHandler = __decorate([ (0, strategy_handler_decorator_1.StrategyHandler)(AllowedBrowser) ], _AllowBrowserHandler); class AllowUsers { constructor(emails) { this.emails = emails; } } let _AllowUsersHandler = class _AllowUsersHandler { async evaluate(filter, context) { if (!context.email) throw new Error('Allow users filter requires param: email'); return filter.emails.includes(context.email); } }; _AllowUsersHandler = __decorate([ (0, strategy_handler_decorator_1.StrategyHandler)(AllowUsers) ], _AllowUsersHandler); beforeEach(() => { feature_flag_store_1.featureFlagStore.clear(); }); it('feature should be disabled if strategies does not resolves to true', async () => { const featureManager = new feature_manager_1.FeatureManager('production'); let HostReport = class HostReport { }; HostReport = __decorate([ (0, feature_flag_decorator_1.FeatureFlag)('production', [new AllowUsers(['allow@example.com'])]) ], HostReport); const context = { email: 'do-not-allow@gmail.com' }; expect(await featureManager.isEnabled(HostReport, context)).toEqual(false); }); it('feature should be enabled when one of the strategy evaluates to true', async () => { const featureManager = new feature_manager_1.FeatureManager('production'); let HostReport = class HostReport { }; HostReport = __decorate([ (0, feature_flag_decorator_1.FeatureFlag)('production', [new AllowUsers(['ram@gmail.com']), new AllowedBrowser(['firefox'])]) ], HostReport); const context = { email: 'do-not-allow@gmail.com', browser: 'firefox', }; expect(await featureManager.isEnabled(HostReport, context)).toEqual(true); }); }); //# sourceMappingURL=feature-flag-filter.spec.js.map