Skip to content

Commit bd72e42

Browse files
Fix issue with local evaluation of multivariate flags (#87)
* Fix issue with local evaluation of multivariate flags * Fix broken test
1 parent e6c9eae commit bd72e42

File tree

4 files changed

+68
-12
lines changed

4 files changed

+68
-12
lines changed

sdk/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,8 @@ export class Flagsmith {
323323
const flags = Flags.fromFeatureStateModels({
324324
featureStates: featureStates,
325325
analyticsProcessor: this.analyticsProcessor,
326-
defaultFlagHandler: this.defaultFlagHandler
326+
defaultFlagHandler: this.defaultFlagHandler,
327+
identityID: identityModel.djangoID || identityModel.identityUuid
327328
});
328329

329330
if (!!this.cache) {

tests/sdk/data/environment.json

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,30 @@
6363
"type": "STANDARD",
6464
"id": 1
6565
},
66-
"segment_id": null,
66+
"feature_segment": null,
6767
"enabled": true
68+
},
69+
{
70+
"multivariate_feature_state_values": [
71+
{
72+
"percentage_allocation": 100,
73+
"multivariate_feature_option": {
74+
"value": "bar",
75+
"id": 1
76+
},
77+
"mv_fs_value_uuid": "42d5cdf9-8ec9-4b8d-a3ca-fd43c64d5f05",
78+
"id": 1
79+
}
80+
],
81+
"feature_state_value": "foo",
82+
"feature": {
83+
"name": "mv_feature",
84+
"type": "MULTIVARIATE",
85+
"id": 2
86+
},
87+
"feature_segment": null,
88+
"featurestate_uuid": "96fc3503-09d7-48f1-a83b-2dc903d5c08a",
89+
"enabled": false
6890
}
6991
]
7092
}

tests/sdk/flagsmith-identity-flags.test.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ test('test_get_identity_flags_calls_api_when_no_local_environment_no_traits', as
2727
expect(identityFlags[0].featureName).toBe('some_feature');
2828
});
2929

30-
test('test_get_identity_flags_calls_api_when_local_environment_no_traits', async () => {
30+
test('test_get_identity_flags_uses_environment_when_local_environment_no_traits', async () => {
3131
// @ts-ignore
3232
fetch.mockReturnValue(Promise.resolve(new Response(environmentJSON())));
3333
const identifier = 'identifier';
@@ -138,3 +138,20 @@ test('test_default_flag_is_used_when_no_identity_flags_returned_and_no_custom_de
138138
expect(flag.enabled).toBe(false);
139139
});
140140

141+
142+
test('test_get_identity_flags_multivariate_value_with_local_evaluation_enabled', async () => {
143+
// @ts-ignore
144+
fetch.mockReturnValue(Promise.resolve(new Response(environmentJSON())));
145+
const identifier = 'identifier';
146+
147+
const flg = flagsmith({
148+
environmentKey: 'ser.key',
149+
enableLocalEvaluation: true,
150+
151+
});
152+
153+
const identityFlags = (await flg.getIdentityFlags(identifier))
154+
155+
expect(identityFlags.getFeatureValue('mv_feature')).toBe('bar');
156+
expect(identityFlags.isFeatureEnabled('mv_feature')).toBe(false);
157+
});

tests/sdk/flagsmith.test.ts

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,11 @@ test('test_update_environment_sets_environment', async () => {
4545
await flg.updateEnvironment();
4646
expect(flg.environment).toBeDefined();
4747

48-
// @ts-ignore
49-
flg.environment.featureStates[0].featurestateUUID = undefined;
50-
// @ts-ignore
51-
flg.environment.project.segments[0].featureStates[0].featurestateUUID = undefined;
52-
// @ts-ignore
5348
const model = environmentModel(JSON.parse(environmentJSON()));
54-
// @ts-ignore
55-
model.featureStates[0].featurestateUUID = undefined;
56-
// @ts-ignore
57-
model.project.segments[0].featureStates[0].featurestateUUID = undefined;
49+
50+
wipeFeatureStateUUIDs(flg.environment)
51+
wipeFeatureStateUUIDs(model)
52+
5853
expect(flg.environment).toStrictEqual(model);
5954
});
6055

@@ -231,3 +226,24 @@ test('test onEnvironmentChange is called after error', async () => {
231226

232227
expect(callbackSpy).toBeCalled();
233228
});
229+
230+
231+
232+
async function wipeFeatureStateUUIDs (enviromentModel: EnvironmentModel) {
233+
// TODO: this has been pulled out of tests above as a helper function.
234+
// I'm not entirely sure why it's necessary, however, we should look to remove.
235+
enviromentModel.featureStates.forEach(fs => {
236+
// @ts-ignore
237+
fs.featurestateUUID = undefined;
238+
fs.multivariateFeatureStateValues.forEach(mvfsv => {
239+
// @ts-ignore
240+
mvfsv.mvFsValueUuid = undefined;
241+
})
242+
});
243+
enviromentModel.project.segments.forEach(s => {
244+
s.featureStates.forEach(fs => {
245+
// @ts-ignore
246+
fs.featurestateUUID = undefined;
247+
})
248+
})
249+
}

0 commit comments

Comments
 (0)