Skip to content

Commit a0233a5

Browse files
Add getTreatments[WithConfig]ByFlagSet[s] methods
1 parent 4abc03b commit a0233a5

File tree

3 files changed

+271
-1
lines changed

3 files changed

+271
-1
lines changed

splitio_web/lib/splitio_web.dart

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,4 +477,80 @@ class SplitioWeb extends SplitioPlatform {
477477
_convertEvaluationOptions(evaluationOptions)) as JSObject;
478478
return jsTreatmentsWithConfigToMap(result);
479479
}
480+
481+
Future<Map<String, String>> getTreatmentsByFlagSet(
482+
{required String matchingKey,
483+
required String? bucketingKey,
484+
required String flagSet,
485+
Map<String, dynamic> attributes = const {},
486+
EvaluationOptions evaluationOptions = const EvaluationOptions.empty()}) async {
487+
final client = await _getClient(
488+
matchingKey: matchingKey,
489+
bucketingKey: bucketingKey,
490+
);
491+
492+
final result = client.getTreatmentsByFlagSet.callAsFunction(
493+
null,
494+
flagSet.toJS,
495+
_convertMap(attributes, true),
496+
_convertEvaluationOptions(evaluationOptions)) as JSObject;
497+
return jsTreatmentsToMap(result);
498+
}
499+
500+
Future<Map<String, String>> getTreatmentsByFlagSets(
501+
{required String matchingKey,
502+
required String? bucketingKey,
503+
required List<String> flagSets,
504+
Map<String, dynamic> attributes = const {},
505+
EvaluationOptions evaluationOptions = const EvaluationOptions.empty()}) async {
506+
final client = await _getClient(
507+
matchingKey: matchingKey,
508+
bucketingKey: bucketingKey,
509+
);
510+
511+
final result = client.getTreatmentsByFlagSets.callAsFunction(
512+
null,
513+
flagSets.jsify(),
514+
_convertMap(attributes, true),
515+
_convertEvaluationOptions(evaluationOptions)) as JSObject;
516+
return jsTreatmentsToMap(result);
517+
}
518+
519+
Future<Map<String, SplitResult>> getTreatmentsWithConfigByFlagSet(
520+
{required String matchingKey,
521+
required String? bucketingKey,
522+
required String flagSet,
523+
Map<String, dynamic> attributes = const {},
524+
EvaluationOptions evaluationOptions = const EvaluationOptions.empty()}) async {
525+
final client = await _getClient(
526+
matchingKey: matchingKey,
527+
bucketingKey: bucketingKey,
528+
);
529+
530+
final result = client.getTreatmentsWithConfigByFlagSet.callAsFunction(
531+
null,
532+
flagSet.toJS,
533+
_convertMap(attributes, true),
534+
_convertEvaluationOptions(evaluationOptions)) as JSObject;
535+
return jsTreatmentsWithConfigToMap(result);
536+
}
537+
538+
Future<Map<String, SplitResult>> getTreatmentsWithConfigByFlagSets(
539+
{required String matchingKey,
540+
required String? bucketingKey,
541+
required List<String> flagSets,
542+
Map<String, dynamic> attributes = const {},
543+
EvaluationOptions evaluationOptions = const EvaluationOptions.empty()}) async {
544+
final client = await _getClient(
545+
matchingKey: matchingKey,
546+
bucketingKey: bucketingKey,
547+
);
548+
549+
final result = client.getTreatmentsWithConfigByFlagSets.callAsFunction(
550+
null,
551+
flagSets.jsify(),
552+
_convertMap(attributes, true),
553+
_convertEvaluationOptions(evaluationOptions)) as JSObject;
554+
return jsTreatmentsWithConfigToMap(result);
555+
}
480556
}

splitio_web/lib/src/js_interop.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ extension type JS_IBrowserClient._(JSObject _) implements JSObject {
2121
external JSFunction getTreatmentsWithConfig;
2222
external JSFunction getTreatmentsByFlagSet;
2323
external JSFunction getTreatmentsByFlagSets;
24-
external JSFunction getTreatmentWithConfigByFlagSet;
24+
external JSFunction getTreatmentsWithConfigByFlagSet;
2525
external JSFunction getTreatmentsWithConfigByFlagSets;
2626
}
2727

splitio_web/test/splitio_web_test.dart

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,60 @@ void main() {
7474
}
7575
return JSObject();
7676
}.toJS;
77+
mockClient['getTreatmentsByFlagSet'] =
78+
(JSAny? flagSetName, JSAny? attributes, JSAny? evaluationOptions) {
79+
calls.add((
80+
methodName: 'getTreatmentsByFlagSet',
81+
methodArguments: [flagSetName, attributes, evaluationOptions]
82+
));
83+
final result = JSObject();
84+
result.setProperty('split1'.toJS, 'on'.toJS);
85+
result.setProperty('split2'.toJS, 'on'.toJS);
86+
return result;
87+
}.toJS;
88+
mockClient['getTreatmentsByFlagSets'] =
89+
(JSAny? flagSetNames, JSAny? attributes, JSAny? evaluationOptions) {
90+
calls.add((
91+
methodName: 'getTreatmentsByFlagSets',
92+
methodArguments: [flagSetNames, attributes, evaluationOptions]
93+
));
94+
final result = JSObject();
95+
result.setProperty('split1'.toJS, 'on'.toJS);
96+
result.setProperty('split2'.toJS, 'on'.toJS);
97+
return result;
98+
}.toJS;
99+
mockClient['getTreatmentsWithConfigByFlagSet'] =
100+
(JSAny? flagSetName, JSAny? attributes, JSAny? evaluationOptions) {
101+
calls.add((
102+
methodName: 'getTreatmentsWithConfigByFlagSet',
103+
methodArguments: [flagSetName, attributes, evaluationOptions]
104+
));
105+
106+
final treatmentWithConfig = JSObject();
107+
treatmentWithConfig.setProperty('treatment'.toJS, 'on'.toJS);
108+
treatmentWithConfig.setProperty('config'.toJS, 'some-config'.toJS);
109+
110+
final result = JSObject();
111+
result.setProperty('split1'.toJS, treatmentWithConfig);
112+
result.setProperty('split2'.toJS, treatmentWithConfig);
113+
return result;
114+
}.toJS;
115+
mockClient['getTreatmentsWithConfigByFlagSets'] =
116+
(JSAny? flagSetNames, JSAny? attributes, JSAny? evaluationOptions) {
117+
calls.add((
118+
methodName: 'getTreatmentsWithConfigByFlagSets',
119+
methodArguments: [flagSetNames, attributes, evaluationOptions]
120+
));
121+
122+
final treatmentWithConfig = JSObject();
123+
treatmentWithConfig.setProperty('treatment'.toJS, 'on'.toJS);
124+
treatmentWithConfig.setProperty('config'.toJS, 'some-config'.toJS);
125+
126+
final result = JSObject();
127+
result.setProperty('split1'.toJS, treatmentWithConfig);
128+
result.setProperty('split2'.toJS, treatmentWithConfig);
129+
return result;
130+
}.toJS;
77131

78132
final mockLog = JSObject();
79133
mockLog['warn'] = (JSAny? arg1) {
@@ -317,6 +371,146 @@ void main() {
317371
{}
318372
]);
319373
});
374+
375+
test('getTreatmentsByFlagSet without attributes', () async {
376+
final result = await _platform.getTreatmentsByFlagSet(
377+
matchingKey: 'matching-key',
378+
bucketingKey: 'bucketing-key',
379+
flagSet: 'set_1');
380+
381+
expect(result, {'split1': 'on', 'split2': 'on'});
382+
expect(calls.last.methodName, 'getTreatmentsByFlagSet');
383+
expect(calls.last.methodArguments.map(jsAnyToDart), ['set_1', {}, {}]);
384+
});
385+
386+
test('getTreatmentsByFlagSet with attributes', () async {
387+
final result = await _platform.getTreatmentsByFlagSet(
388+
matchingKey: 'matching-key',
389+
bucketingKey: 'bucketing-key',
390+
flagSet: 'set_1',
391+
attributes: {'attr1': true});
392+
393+
expect(result, {'split1': 'on', 'split2': 'on'});
394+
expect(calls.last.methodName, 'getTreatmentsByFlagSet');
395+
expect(calls.last.methodArguments.map(jsAnyToDart), [
396+
'set_1',
397+
{'attr1': true},
398+
{}
399+
]);
400+
});
401+
402+
test('getTreatmentsByFlagSets without attributes', () async {
403+
final result = await _platform.getTreatmentsByFlagSets(
404+
matchingKey: 'matching-key',
405+
bucketingKey: 'bucketing-key',
406+
flagSets: ['set_1', 'set_2']);
407+
408+
expect(result, {'split1': 'on', 'split2': 'on'});
409+
expect(calls.last.methodName, 'getTreatmentsByFlagSets');
410+
expect(calls.last.methodArguments.map(jsAnyToDart), [
411+
['set_1', 'set_2'],
412+
{},
413+
{}
414+
]);
415+
});
416+
417+
test('getTreatmentsByFlagSets with attributes', () async {
418+
final result = await _platform.getTreatmentsByFlagSets(
419+
matchingKey: 'matching-key',
420+
bucketingKey: 'bucketing-key',
421+
flagSets: ['set_1', 'set_2'],
422+
attributes: {'attr1': true});
423+
424+
expect(result, {'split1': 'on', 'split2': 'on'});
425+
expect(calls.last.methodName, 'getTreatmentsByFlagSets');
426+
expect(calls.last.methodArguments.map(jsAnyToDart), [
427+
['set_1', 'set_2'],
428+
{'attr1': true},
429+
{}
430+
]);
431+
});
432+
433+
test('getTreatmentsWithConfigByFlagSet without attributes', () async {
434+
final result = await _platform.getTreatmentsWithConfigByFlagSet(
435+
matchingKey: 'matching-key',
436+
bucketingKey: 'bucketing-key',
437+
flagSet: 'set_1');
438+
439+
expect(result, predicate<Map<String, SplitResult>>((result) {
440+
return result.length == 2 &&
441+
result['split1'].toString() ==
442+
SplitResult('on', 'some-config').toString() &&
443+
result['split2'].toString() ==
444+
SplitResult('on', 'some-config').toString();
445+
}));
446+
expect(calls.last.methodName, 'getTreatmentsWithConfigByFlagSet');
447+
expect(calls.last.methodArguments.map(jsAnyToDart), ['set_1', {}, {}]);
448+
});
449+
450+
test('getTreatmentsWithConfigByFlagSet with attributes', () async {
451+
final result = await _platform.getTreatmentsWithConfigByFlagSet(
452+
matchingKey: 'matching-key',
453+
bucketingKey: 'bucketing-key',
454+
flagSet: 'set_1',
455+
attributes: {'attr1': true});
456+
457+
expect(result, predicate<Map<String, SplitResult>>((result) {
458+
return result.length == 2 &&
459+
result['split1'].toString() ==
460+
SplitResult('on', 'some-config').toString() &&
461+
result['split2'].toString() ==
462+
SplitResult('on', 'some-config').toString();
463+
}));
464+
expect(calls.last.methodName, 'getTreatmentsWithConfigByFlagSet');
465+
expect(calls.last.methodArguments.map(jsAnyToDart), [
466+
'set_1',
467+
{'attr1': true},
468+
{}
469+
]);
470+
});
471+
472+
test('getTreatmentsWithConfigByFlagSets without attributes', () async {
473+
final result = await _platform.getTreatmentsWithConfigByFlagSets(
474+
matchingKey: 'matching-key',
475+
bucketingKey: 'bucketing-key',
476+
flagSets: ['set_1', 'set_2']);
477+
478+
expect(result, predicate<Map<String, SplitResult>>((result) {
479+
return result.length == 2 &&
480+
result['split1'].toString() ==
481+
SplitResult('on', 'some-config').toString() &&
482+
result['split2'].toString() ==
483+
SplitResult('on', 'some-config').toString();
484+
}));
485+
expect(calls.last.methodName, 'getTreatmentsWithConfigByFlagSets');
486+
expect(calls.last.methodArguments.map(jsAnyToDart), [
487+
['set_1', 'set_2'],
488+
{},
489+
{}
490+
]);
491+
});
492+
493+
test('getTreatmentsWithConfigByFlagSets with attributes', () async {
494+
final result = await _platform.getTreatmentsWithConfigByFlagSets(
495+
matchingKey: 'matching-key',
496+
bucketingKey: 'bucketing-key',
497+
flagSets: ['set_1', 'set_2'],
498+
attributes: {'attr1': true});
499+
500+
expect(result, predicate<Map<String, SplitResult>>((result) {
501+
return result.length == 2 &&
502+
result['split1'].toString() ==
503+
SplitResult('on', 'some-config').toString() &&
504+
result['split2'].toString() ==
505+
SplitResult('on', 'some-config').toString();
506+
}));
507+
expect(calls.last.methodName, 'getTreatmentsWithConfigByFlagSets');
508+
expect(calls.last.methodArguments.map(jsAnyToDart), [
509+
['set_1', 'set_2'],
510+
{'attr1': true},
511+
{}
512+
]);
513+
});
320514
});
321515

322516
group('initialization', () {

0 commit comments

Comments
 (0)