Skip to content

Commit 7d017d3

Browse files
authored
Added all Switcher Strategies (#47)
1 parent 45a9181 commit 7d017d3

File tree

4 files changed

+71
-18
lines changed

4 files changed

+71
-18
lines changed

src/dto/feature-request.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
export type FeatureRequestDto = {
22
feature: string;
33
parameters?: {
4-
value: string;
4+
value?: string;
5+
number?: string;
6+
date?: string;
7+
time?: string;
8+
payload?: string;
9+
regex?: string;
10+
network?: string;
511
};
612
};
713

src/services/feature.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,22 @@ class FeatureService {
1515
const featuresRes = await Promise.all(featuresReq.features.map(async (featureReq) => {
1616
const switcher = Client.getSwitcher();
1717

18-
if (featureReq?.parameters?.value) {
19-
switcher.checkValue(featureReq.parameters.value);
18+
const paramHandlers: Record<string, (value: string) => void> = {
19+
value: (val) => switcher.checkValue(val),
20+
number: (val) => switcher.checkNumeric(val),
21+
date: (val) => switcher.checkDate(val),
22+
time: (val) => switcher.checkTime(val),
23+
payload: (val) => switcher.checkPayload(val),
24+
regex: (val) => switcher.checkRegex(val),
25+
network: (val) => switcher.checkNetwork(val),
26+
};
27+
28+
if (featureReq?.parameters) {
29+
Object.entries(featureReq.parameters).forEach(([key, val]) => {
30+
if (paramHandlers[key]) {
31+
paramHandlers[key](val);
32+
}
33+
});
2034
}
2135

2236
return {

tests/services/feature.integrated.test.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ const testBody = (fn: (t: Deno.TestContext) => void | Promise<void>) => {
2626
Deno.test({
2727
name: 'Feature service integrated - it should return feature enabled - from snapshot cache',
2828
fn: testBody(async () => {
29-
//given
30-
await featureService.initialize(false);
31-
3229
//test
3330
const response = await featureService.isFeatureEnabled({ feature: 'PLACEHOLDER' });
3431

tests/services/feature.test.ts

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,48 @@ const setupDenoEnv = async (local: boolean, interval: string = '') => {
1010
await import('../../src/app.ts');
1111
};
1212

13+
const tearDown = () => {
14+
Client.terminateSnapshotAutoUpdate();
15+
};
16+
1317
const testBody = (fn: (t: Deno.TestContext) => void | Promise<void>) => {
1418
return async (t: Deno.TestContext) => {
1519
await setupDenoEnv(true);
1620
await fn(t);
21+
tearDown();
1722
};
1823
};
1924

25+
const testParameters = [{
26+
when: StrategiesType.VALUE,
27+
field: 'value',
28+
value: 'value',
29+
}, {
30+
when: StrategiesType.NUMERIC,
31+
field: 'number',
32+
value: '123',
33+
}, {
34+
when: StrategiesType.DATE,
35+
field: 'date',
36+
value: '2023-10-01',
37+
}, {
38+
when: StrategiesType.TIME,
39+
field: 'time',
40+
value: '12:00',
41+
}, {
42+
when: StrategiesType.PAYLOAD,
43+
field: 'payload',
44+
value: '{"key":"value"}',
45+
}, {
46+
when: StrategiesType.REGEX,
47+
field: 'regex',
48+
value: '^test$',
49+
}, {
50+
when: StrategiesType.NETWORK,
51+
field: 'network',
52+
value: '198.168.0.1',
53+
}];
54+
2055
Deno.test({
2156
name: 'Feature service - it should return feature disabled',
2257
fn: testBody(async () => {
@@ -25,28 +60,29 @@ Deno.test({
2560
const featureName = 'FEATURE_NAME';
2661

2762
//test
28-
featureService.initialize(false);
2963
const response = await featureService.isFeatureEnabled({ feature: featureName });
3064

3165
//assert
3266
assertFalse(response);
33-
Client.terminateSnapshotAutoUpdate();
3467
}),
3568
});
3669

3770
Deno.test({
38-
name: 'Feature service - it should return feature enabled - with parameters',
71+
name: 'Feature service - it should return feature enabled (parameterized)',
3972
fn: testBody(async () => {
40-
//given
41-
Client.assume('FEATURE_NAME').true().when(StrategiesType.VALUE, 'value');
42-
const featureName = 'FEATURE_NAME';
73+
for (const param of testParameters) {
74+
//given
75+
Client.assume('FEATURE_NAME').true().when(param.when, param.value);
76+
const featureName = 'FEATURE_NAME';
4377

44-
//test
45-
featureService.initialize(false);
46-
const response = await featureService.isFeatureEnabled({ feature: featureName, parameters: { value: 'value' } });
78+
//test
79+
const response = await featureService.isFeatureEnabled({
80+
feature: featureName,
81+
parameters: { [param.field]: param.value },
82+
});
4783

48-
//assert
49-
assert(response);
50-
Client.terminateSnapshotAutoUpdate();
84+
//assert
85+
assert(response, `Feature should be enabled for ${param.when} with value ${param.value}`);
86+
}
5187
}),
5288
});

0 commit comments

Comments
 (0)