diff --git a/src/dto/feature-request.ts b/src/dto/feature-request.ts index 810f2ad..b7b6607 100644 --- a/src/dto/feature-request.ts +++ b/src/dto/feature-request.ts @@ -1,7 +1,13 @@ export type FeatureRequestDto = { feature: string; parameters?: { - value: string; + value?: string; + number?: string; + date?: string; + time?: string; + payload?: string; + regex?: string; + network?: string; }; }; diff --git a/src/services/feature.ts b/src/services/feature.ts index 5bec36b..e695ef4 100644 --- a/src/services/feature.ts +++ b/src/services/feature.ts @@ -15,8 +15,22 @@ class FeatureService { const featuresRes = await Promise.all(featuresReq.features.map(async (featureReq) => { const switcher = Client.getSwitcher(); - if (featureReq?.parameters?.value) { - switcher.checkValue(featureReq.parameters.value); + const paramHandlers: Record void> = { + value: (val) => switcher.checkValue(val), + number: (val) => switcher.checkNumeric(val), + date: (val) => switcher.checkDate(val), + time: (val) => switcher.checkTime(val), + payload: (val) => switcher.checkPayload(val), + regex: (val) => switcher.checkRegex(val), + network: (val) => switcher.checkNetwork(val), + }; + + if (featureReq?.parameters) { + Object.entries(featureReq.parameters).forEach(([key, val]) => { + if (paramHandlers[key]) { + paramHandlers[key](val); + } + }); } return { diff --git a/tests/services/feature.integrated.test.ts b/tests/services/feature.integrated.test.ts index 59b2751..5887315 100644 --- a/tests/services/feature.integrated.test.ts +++ b/tests/services/feature.integrated.test.ts @@ -26,9 +26,6 @@ const testBody = (fn: (t: Deno.TestContext) => void | Promise) => { Deno.test({ name: 'Feature service integrated - it should return feature enabled - from snapshot cache', fn: testBody(async () => { - //given - await featureService.initialize(false); - //test const response = await featureService.isFeatureEnabled({ feature: 'PLACEHOLDER' }); diff --git a/tests/services/feature.test.ts b/tests/services/feature.test.ts index dbcad9a..329a3ad 100644 --- a/tests/services/feature.test.ts +++ b/tests/services/feature.test.ts @@ -10,13 +10,48 @@ const setupDenoEnv = async (local: boolean, interval: string = '') => { await import('../../src/app.ts'); }; +const tearDown = () => { + Client.terminateSnapshotAutoUpdate(); +}; + const testBody = (fn: (t: Deno.TestContext) => void | Promise) => { return async (t: Deno.TestContext) => { await setupDenoEnv(true); await fn(t); + tearDown(); }; }; +const testParameters = [{ + when: StrategiesType.VALUE, + field: 'value', + value: 'value', +}, { + when: StrategiesType.NUMERIC, + field: 'number', + value: '123', +}, { + when: StrategiesType.DATE, + field: 'date', + value: '2023-10-01', +}, { + when: StrategiesType.TIME, + field: 'time', + value: '12:00', +}, { + when: StrategiesType.PAYLOAD, + field: 'payload', + value: '{"key":"value"}', +}, { + when: StrategiesType.REGEX, + field: 'regex', + value: '^test$', +}, { + when: StrategiesType.NETWORK, + field: 'network', + value: '198.168.0.1', +}]; + Deno.test({ name: 'Feature service - it should return feature disabled', fn: testBody(async () => { @@ -25,28 +60,29 @@ Deno.test({ const featureName = 'FEATURE_NAME'; //test - featureService.initialize(false); const response = await featureService.isFeatureEnabled({ feature: featureName }); //assert assertFalse(response); - Client.terminateSnapshotAutoUpdate(); }), }); Deno.test({ - name: 'Feature service - it should return feature enabled - with parameters', + name: 'Feature service - it should return feature enabled (parameterized)', fn: testBody(async () => { - //given - Client.assume('FEATURE_NAME').true().when(StrategiesType.VALUE, 'value'); - const featureName = 'FEATURE_NAME'; + for (const param of testParameters) { + //given + Client.assume('FEATURE_NAME').true().when(param.when, param.value); + const featureName = 'FEATURE_NAME'; - //test - featureService.initialize(false); - const response = await featureService.isFeatureEnabled({ feature: featureName, parameters: { value: 'value' } }); + //test + const response = await featureService.isFeatureEnabled({ + feature: featureName, + parameters: { [param.field]: param.value }, + }); - //assert - assert(response); - Client.terminateSnapshotAutoUpdate(); + //assert + assert(response, `Feature should be enabled for ${param.when} with value ${param.value}`); + } }), });