Skip to content

Commit 3f29ce3

Browse files
Remove callAsFunction from logger factory methods. Add test for Logger modules.
1 parent 110ec5b commit 3f29ce3

File tree

4 files changed

+108
-20
lines changed

4 files changed

+108
-20
lines changed

splitio_web/lib/splitio_web.dart

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -241,33 +241,35 @@ class SplitioWeb extends SplitioPlatform {
241241

242242
final logLevel = configuration.configurationMap['logLevel'];
243243
if (logLevel is String) {
244-
JSAny? logger;
245244
switch (SplitLogLevel.values.firstWhere((e) => e.name == logLevel)) {
246245
case SplitLogLevel.verbose:
247246
case SplitLogLevel.debug:
248-
logger = window.splitio!.DebugLogger?.callAsFunction();
247+
config.debug = window.splitio!.DebugLogger != null
248+
? window.splitio!.DebugLogger!()
249+
: 'DEBUG'.toJS;
249250
break;
250251
case SplitLogLevel.info:
251-
logger = window.splitio!.InfoLogger?.callAsFunction();
252+
config.debug = window.splitio!.InfoLogger != null
253+
? window.splitio!.InfoLogger!()
254+
: 'INFO'.toJS;
252255
break;
253256
case SplitLogLevel.warning:
254-
logger = window.splitio!.WarnLogger?.callAsFunction();
257+
config.debug = window.splitio!.WarnLogger != null
258+
? window.splitio!.WarnLogger!()
259+
: 'WARNING'.toJS;
255260
break;
256261
case SplitLogLevel.error:
257-
logger = window.splitio!.ErrorLogger?.callAsFunction();
262+
config.debug = window.splitio!.ErrorLogger != null
263+
? window.splitio!.ErrorLogger!()
264+
: 'ERROR'.toJS;
258265
break;
259266
default:
260267
break;
261268
}
262-
if (logger != null) {
263-
config.debug = logger; // Browser SDK
264-
} else {
265-
config.debug = logLevel.toUpperCase().toJS; // JS SDK
266-
}
267269
} else if (configuration.configurationMap['enableDebug'] == true) {
268270
config.debug = window.splitio!.DebugLogger != null
269-
? window.splitio!.DebugLogger!.callAsFunction() // Browser SDK
270-
: 'DEBUG'.toJS; // JS SDK
271+
? window.splitio!.DebugLogger!()
272+
: 'DEBUG'.toJS;
271273
}
272274

273275
if (configuration.configurationMap['readyTimeout'] != null) {

splitio_web/lib/src/js_interop.dart

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ extension type JS_ImpressionData._(JSObject _) implements JSObject {
2626
}
2727

2828
@JS()
29-
extension type JS_Logger._(JSObject _) implements JSObject {
29+
extension type JS_ILogger._(JSObject _) implements JSObject {
3030
external JSAny? debug(JSString message);
3131
external JSAny? info(JSString message);
3232
external JSAny? warn(JSString message);
@@ -106,7 +106,7 @@ extension type JS_Configuration._(JSObject _) implements JSObject {
106106

107107
@JS()
108108
extension type JS_ISettings._(JSObject _) implements JS_Configuration {
109-
external JS_Logger log;
109+
external JS_ILogger log;
110110
external JS_IImpressionListener? impressionListener;
111111
}
112112

@@ -215,14 +215,19 @@ extension type JS_IBrowserSDK._(JSObject _) implements JSObject {
215215
external JS_IUserConsentAPI UserConsent;
216216
}
217217

218+
@JS()
219+
extension type JS_LoggerFactory._(JSFunction _) implements JSFunction {
220+
external JSObject call();
221+
}
222+
218223
@JS()
219224
extension type JS_BrowserSDKPackage._(JSObject _) implements JSObject {
220225
external JS_IBrowserSDK SplitFactory(JS_Configuration config);
221226
external JSFunction? InLocalStorage;
222-
external JSFunction? DebugLogger;
223-
external JSFunction? InfoLogger;
224-
external JSFunction? WarnLogger;
225-
external JSFunction? ErrorLogger;
227+
external JS_LoggerFactory? DebugLogger;
228+
external JS_LoggerFactory? InfoLogger;
229+
external JS_LoggerFactory? WarnLogger;
230+
external JS_LoggerFactory? ErrorLogger;
226231
}
227232

228233
// Conversion utils: JS to Dart types
@@ -234,7 +239,7 @@ external JSArray<JSString> objectKeys(JSObject obj);
234239
external JSAny? reflectGet(JSObject target, JSAny propertyKey);
235240

236241
@JS('Reflect.set')
237-
external JSAny? reflectSet(JSObject target, JSAny propertyKey, JSAny value);
242+
external JSAny? reflectSet(JSObject target, JSAny propertyKey, JSAny? value);
238243

239244
@JS('JSON.parse')
240245
external JSObject jsonParse(JSString obj);

splitio_web/test/splitio_web_test.dart

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,49 @@ void main() {
838838
'storage': {'type': 'LOCALSTORAGE'}
839839
}));
840840
});
841+
842+
test(
843+
'init with InLocalStorage and Logger factory modules (Browser SDK full CDN)',
844+
() async {
845+
mock.addFactoryModules();
846+
SplitioWeb _platform = SplitioWeb();
847+
848+
await _platform.init(
849+
apiKey: 'api-key',
850+
matchingKey: 'matching-key',
851+
bucketingKey: 'bucketing-key',
852+
sdkConfiguration: SplitConfiguration(
853+
logLevel: SplitLogLevel.info,
854+
rolloutCacheConfiguration: RolloutCacheConfiguration(
855+
expirationDays: 100,
856+
clearOnInit: true,
857+
)));
858+
859+
expect(mock.calls[mock.calls.length - 2].methodName, 'InfoLogger');
860+
861+
expect(mock.calls.last.methodName, 'SplitFactory');
862+
expect(
863+
jsAnyToDart(mock.calls.last.methodArguments[0]),
864+
equals({
865+
'core': {
866+
'authorizationKey': 'api-key',
867+
'key': {
868+
'matchingKey': 'matching-key',
869+
'bucketingKey': 'bucketing-key',
870+
},
871+
},
872+
'startup': {
873+
'readyTimeout': 10,
874+
},
875+
'scheduler': {},
876+
'urls': {},
877+
'sync': {},
878+
'debug': {},
879+
'storage': {'type': 'LOCALSTORAGE', 'expirationDays': 100, 'clearOnInit': true }
880+
}));
881+
882+
mock.removeFactoryModules();
883+
});
841884
});
842885

843886
group('client', () {

splitio_web/test/utils/js_interop_test_utils.dart

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class SplitioMock {
7777
return ['split1'.toJS, 'split2'.toJS].jsify();
7878
}.toJS);
7979

80-
final mockLog = JSObject() as JS_Logger;
80+
final mockLog = JSObject() as JS_ILogger;
8181
reflectSet(
8282
mockLog,
8383
'warn'.toJS,
@@ -397,4 +397,42 @@ class SplitioMock {
397397

398398
return mockClient;
399399
}
400+
401+
void addFactoryModules() {
402+
reflectSet(
403+
splitio,
404+
'DebugLogger'.toJS,
405+
() {
406+
calls.add((methodName: 'DebugLogger', methodArguments: []));
407+
return JSObject();
408+
}.toJS);
409+
reflectSet(
410+
splitio,
411+
'InfoLogger'.toJS,
412+
() {
413+
calls.add((methodName: 'InfoLogger', methodArguments: []));
414+
return JSObject();
415+
}.toJS);
416+
reflectSet(
417+
splitio,
418+
'WarnLogger'.toJS,
419+
() {
420+
calls.add((methodName: 'WarnLogger', methodArguments: []));
421+
return JSObject();
422+
}.toJS);
423+
reflectSet(
424+
splitio,
425+
'ErrorLogger'.toJS,
426+
() {
427+
calls.add((methodName: 'ErrorLogger', methodArguments: []));
428+
return JSObject();
429+
}.toJS);
430+
}
431+
432+
void removeFactoryModules() {
433+
reflectSet(splitio, 'DebugLogger'.toJS, null);
434+
reflectSet(splitio, 'InfoLogger'.toJS, null);
435+
reflectSet(splitio, 'WarnLogger'.toJS, null);
436+
reflectSet(splitio, 'ErrorLogger'.toJS, null);
437+
}
400438
}

0 commit comments

Comments
 (0)