@@ -16,26 +16,105 @@ extension on web.Window {
1616}
1717
1818void main () {
19- final List <({String methodName, List <dynamic > methodArguments})> calls = [];
19+ final List <({String methodName, List <JSAny ?> methodArguments})> calls = [];
20+
21+ final mockClient = JSObject ();
22+ mockClient['getTreatment' ] =
23+ (JSAny ? flagName, JSAny ? attributes, JSAny ? evaluationOptions) {
24+ calls.add ((
25+ methodName: 'getTreatment' ,
26+ methodArguments: [flagName, attributes, evaluationOptions]
27+ ));
28+ return 'on' .toJS;
29+ }.toJS;
2030
2131 final mockLog = JSObject ();
2232 mockLog['warn' ] = (JSAny ? arg1) {
2333 calls.add ((methodName: 'warn' , methodArguments: [arg1]));
2434 }.toJS;
35+
2536 final mockSettings = JSObject ();
2637 mockSettings['log' ] = mockLog;
2738
2839 final mockFactory = JSObject ();
2940 mockFactory['settings' ] = mockSettings;
41+ mockFactory['client' ] = (JSAny ? splitKey) {
42+ calls.add ((methodName: 'client' , methodArguments: [splitKey]));
43+ return mockClient;
44+ }.toJS;
3045
3146 final mockSplitio = JSObject ();
3247 mockSplitio['SplitFactory' ] = (JSAny ? arg1) {
3348 calls.add ((methodName: 'SplitFactory' , methodArguments: [arg1]));
3449 return mockFactory;
3550 }.toJS;
3651
52+ SplitioWeb _platform = SplitioWeb ();
53+
3754 setUp (() {
38- (web.window as JSObject ).setProperty ('splitio' .toJS, mockSplitio);
55+ (web.window as JSObject )['splitio' ] = mockSplitio;
56+
57+ _platform.init (
58+ apiKey: 'apiKey' ,
59+ matchingKey: 'matching-key' ,
60+ bucketingKey: 'bucketing-key' );
61+ });
62+
63+ group ('evaluation' , () {
64+ test ('getTreatment without attributes' , () async {
65+ final result = await _platform.getTreatment (
66+ matchingKey: 'matching-key' ,
67+ bucketingKey: 'bucketing-key' ,
68+ splitName: 'split' );
69+
70+ expect (result, 'on' );
71+ expect (calls.last.methodName, 'getTreatment' );
72+ expect (calls.last.methodArguments.map (jsAnyToDart), ['split' , {}, {}]);
73+ });
74+
75+ test ('getTreatment with attributes' , () async {
76+ final result = await _platform.getTreatment (
77+ matchingKey: 'matching-key' ,
78+ bucketingKey: 'bucketing-key' ,
79+ splitName: 'split' ,
80+ attributes: {
81+ 'attrBool' : true ,
82+ 'attrString' : 'value' ,
83+ 'attrInt' : 1 ,
84+ 'attrDouble' : 1.1 ,
85+ 'attrList' : ['value1' , 100 , false ],
86+ 'attrSet' : {'value3' , 100 , true },
87+ 'attrNull' : null , // ignored
88+ 'attrInvalid' : {'value5' : true } // ignored
89+ });
90+
91+ expect (result, 'on' );
92+ expect (calls.last.methodName, 'getTreatment' );
93+ expect (calls.last.methodArguments.map (jsAnyToDart), [
94+ 'split' ,
95+ {
96+ 'attrBool' : true ,
97+ 'attrString' : 'value' ,
98+ 'attrInt' : 1 ,
99+ 'attrDouble' : 1.1 ,
100+ 'attrList' : ['value1' , 100 , false ],
101+ 'attrSet' : ['value3' , 100 , true ]
102+ },
103+ {}
104+ ]);
105+
106+ // assert warnings
107+ expect (calls[calls.length - 2 ].methodName, 'warn' );
108+ expect (
109+ jsAnyToDart (calls[calls.length - 2 ].methodArguments[0 ]),
110+ equals (
111+ 'Invalid attribute value: {value5: true}, for key: attrInvalid, will be ignored' ));
112+ expect (calls[calls.length - 3 ].methodName, 'warn' );
113+ expect (
114+ jsAnyToDart (calls[calls.length - 3 ].methodArguments[0 ]),
115+ equals (
116+ 'Invalid attribute value: null, for key: attrNull, will be ignored' ));
117+ });
39118 });
40119
41120 group ('initialization' , () {
@@ -47,7 +126,7 @@ void main() {
47126
48127 expect (calls.last.methodName, 'SplitFactory' );
49128 expect (
50- jsObjectToMap (calls.last.methodArguments[0 ]),
129+ jsAnyToDart (calls.last.methodArguments[0 ]),
51130 equals ({
52131 'core' : {
53132 'authorizationKey' : 'api-key' ,
@@ -66,7 +145,7 @@ void main() {
66145
67146 expect (calls.last.methodName, 'SplitFactory' );
68147 expect (
69- jsObjectToMap (calls.last.methodArguments[0 ]),
148+ jsAnyToDart (calls.last.methodArguments[0 ]),
70149 equals ({
71150 'core' : {
72151 'authorizationKey' : 'api-key' ,
@@ -89,7 +168,7 @@ void main() {
89168
90169 expect (calls.last.methodName, 'SplitFactory' );
91170 expect (
92- jsObjectToMap (calls.last.methodArguments[0 ]),
171+ jsAnyToDart (calls.last.methodArguments[0 ]),
93172 equals ({
94173 'core' : {
95174 'authorizationKey' : 'api-key' ,
@@ -152,7 +231,7 @@ void main() {
152231
153232 expect (calls[calls.length - 5 ].methodName, 'SplitFactory' );
154233 expect (
155- jsObjectToMap (calls[calls.length - 5 ].methodArguments[0 ]),
234+ jsAnyToDart (calls[calls.length - 5 ].methodArguments[0 ]),
156235 equals ({
157236 'core' : {
158237 'authorizationKey' : 'api-key' ,
@@ -241,7 +320,7 @@ void main() {
241320
242321 expect (calls.last.methodName, 'SplitFactory' );
243322 expect (
244- jsObjectToMap (calls.last.methodArguments[0 ]),
323+ jsAnyToDart (calls.last.methodArguments[0 ]),
245324 equals ({
246325 'core' : {
247326 'authorizationKey' : 'api-key' ,
0 commit comments