Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/dto/feature-request.ts
Original file line number Diff line number Diff line change
@@ -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;
};
};

Expand Down
18 changes: 16 additions & 2 deletions src/services/feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, (value: string) => 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 {
Expand Down
3 changes: 0 additions & 3 deletions tests/services/feature.integrated.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ const testBody = (fn: (t: Deno.TestContext) => void | Promise<void>) => {
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' });

Expand Down
60 changes: 48 additions & 12 deletions tests/services/feature.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>) => {
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 () => {
Expand All @@ -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}`);
}
}),
});