Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
buenaflor committed Dec 18, 2024
1 parent cf41126 commit 6caf42b
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 63 deletions.
111 changes: 55 additions & 56 deletions flutter/example/integration_test/web_sdk_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,73 +27,72 @@ void main() {
group('Web SDK Integration', () {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();

tearDown(() async {
await Sentry.close();
});

testWidgets('Sentry JS SDK is callable', (tester) async {
final completer = Completer();
const expectedMessage = 'test message';
String actualMessage = '';

await restoreFlutterOnErrorAfter(() async {
await SentryFlutter.init((options) {
options.dsn = fakeDsn;
}, appRunner: () async {
await tester.pumpWidget(const app.MyApp());
group('enabled', () {
testWidgets('Sentry JS SDK is callable', (tester) async {
final completer = Completer<String>();
const expectedMessage = 'test message';

await restoreFlutterOnErrorAfter(() async {
await SentryFlutter.init((options) {
options.initializeNativeJsSdk = true;
options.automatedTestMode = true;
options.dsn = fakeDsn;
}, appRunner: () async {
await tester.pumpWidget(const app.MyApp());
});

final beforeSendFn = (JSObject event, JSObject hint) {
completer.complete(event.getProperty('message'.toJS).toString());
return event;
}.toJS;

final options = {
'dsn': app.exampleDsn,
'beforeSend': beforeSendFn,
'debug': true,
'defaultIntegrations': [],
}.jsify();

_init(options);
_captureMessage(expectedMessage.toJS);
});

final beforeSendFn = (JSObject event, JSObject hint) {
actualMessage = event.getProperty('message'.toJS).toString();
completer.complete();
return event;
}.toJS;

final options = {
'dsn': app.exampleDsn,
'beforeSend': beforeSendFn,
'debug': true,
'defaultIntegrations': [],
}.jsify();

_init(options);
_captureMessage(expectedMessage.toJS);
});
final actualMessage = await completer.future
.timeout(const Duration(seconds: 5), onTimeout: () {
fail('beforeSend was not triggered');
});

await completer.future.timeout(const Duration(seconds: 5), onTimeout: () {
fail('beforeSend was not triggered');
expect(actualMessage, equals(expectedMessage));
});

expect(actualMessage, equals(expectedMessage));
});

testWidgets('Default: JS SDK initialized', (tester) async {
await restoreFlutterOnErrorAfter(() async {
await SentryFlutter.init((options) {
options.dsn = fakeDsn;
}, appRunner: () async {
await tester.pumpWidget(const app.MyApp());
testWidgets('Sentry JS SDK initialized', (tester) async {
await restoreFlutterOnErrorAfter(() async {
await SentryFlutter.init((options) {
options.initializeNativeJsSdk = true;
options.automatedTestMode = true;
options.dsn = fakeDsn;
}, appRunner: () async {
await tester.pumpWidget(const app.MyApp());
});
});
});

expect(globalThis['Sentry'], isNotNull);
expect(globalThis['Sentry'], isNotNull);
});
});

testWidgets('WebSdkIntegration removed: JS SDK not initialized',
(tester) async {
await restoreFlutterOnErrorAfter(() async {
await SentryFlutter.init((options) {
options.dsn = fakeDsn;
// Remove WebSdkIntegration
final integration = options.integrations.firstWhere((integration) =>
integration.runtimeType.toString() == 'WebSdkIntegration');
options.removeIntegration(integration);
}, appRunner: () async {
await tester.pumpWidget(const app.MyApp());
group('disabled', () {
testWidgets('Sentry JS SDK is not initialized', (tester) async {
await restoreFlutterOnErrorAfter(() async {
await SentryFlutter.init((options) {
options.dsn = fakeDsn;
options.automatedTestMode = true;
}, appRunner: () async {
await tester.pumpWidget(const app.MyApp());
});
});
});

expect(globalThis['Sentry'], isNull);
expect(globalThis['Sentry'], isNull);
});
});
});
}
11 changes: 7 additions & 4 deletions flutter/lib/src/integrations/web_sdk_integration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class WebSdkIntegration implements Integration<SentryFlutterOptions> {

@override
FutureOr<void> call(Hub hub, SentryFlutterOptions options) async {
if (!options.initializeNativeJsSdk) {
if (!options.initializeNativeJsSdk || !options.autoInitializeNativeSdk) {
return;
}

Expand All @@ -37,12 +37,11 @@ class WebSdkIntegration implements Integration<SentryFlutterOptions> {
: productionScripts;
await _scriptLoader.loadWebSdk(scripts);
await _web.init(hub);

options.sdk.addIntegration(name);
} catch (exception, stackTrace) {
options.logger(
SentryLevel.fatal,
'$name failed to be installed',
'$name failed to be installed.',
exception: exception,
stackTrace: stackTrace,
);
Expand All @@ -58,7 +57,11 @@ class WebSdkIntegration implements Integration<SentryFlutterOptions> {
await _web.close();
await _scriptLoader.close();
} catch (error, stackTrace) {
if (_options?.automatedTestMode == true) {}
_options?.logger(SentryLevel.warning, '$name failed to be closed.',
exception: error, stackTrace: stackTrace);
if (_options?.automatedTestMode == true) {
rethrow;
}
}
}
}
40 changes: 37 additions & 3 deletions flutter/test/integrations/web_sdk_integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ void main() {
when(fixture.web.close()).thenReturn(null);
});

group('initializeNativeJsSdk: true', () {
group('enabled', () {
setUp(() {
fixture.options.autoInitializeNativeSdk = true;
fixture.options.initializeNativeJsSdk = true;
});

Expand All @@ -43,14 +44,21 @@ void main() {
});
});

// false by default
group('initializeNativeJsSdk: false', () {
// disabled by default
group('disabled', () {
test('does not add integration', () async {
await sut.call(fixture.hub, fixture.options);

expect(fixture.options.sdk.integrations,
isNot(contains(WebSdkIntegration.name)));
});

test('does not load scripts and initialize web', () async {
await sut.call(fixture.hub, fixture.options);

expect(fixture.scriptLoader.loadScriptsCalls, 0);
verify(fixture.web.init(fixture.hub)).called(0);
});
});

test('closes resources', () async {
Expand All @@ -59,6 +67,32 @@ void main() {
expect(fixture.scriptLoader.closeCalls, 1);
verify(fixture.web.close()).called(1);
});

group('initialization conditions', () {
test('disabled when only autoInitializeNativeSdk is true', () async {
fixture.options.autoInitializeNativeSdk = true;
fixture.options.initializeNativeJsSdk = false;

await sut.call(fixture.hub, fixture.options);

expect(fixture.options.sdk.integrations,
isNot(contains(WebSdkIntegration.name)));
expect(fixture.scriptLoader.loadScriptsCalls, 0);
verify(fixture.web.init(fixture.hub)).called(0);
});

test('disabled when only initializeNativeJsSdk is true', () async {
fixture.options.autoInitializeNativeSdk = false;
fixture.options.initializeNativeJsSdk = true;

await sut.call(fixture.hub, fixture.options);

expect(fixture.options.sdk.integrations,
isNot(contains(WebSdkIntegration.name)));
expect(fixture.scriptLoader.loadScriptsCalls, 0);
verify(fixture.web.init(fixture.hub)).called(0);
});
});
});
}

Expand Down

0 comments on commit 6caf42b

Please sign in to comment.