Skip to content

Commit 7738403

Browse files
committed
fix: Test initial app link by mocking the event channel
1 parent a5f0ca7 commit 7738403

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

packages/supabase_flutter/test/supabase_flutter_test.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:flutter/services.dart';
12
import 'package:flutter_test/flutter_test.dart';
23
import 'package:supabase_flutter/supabase_flutter.dart';
34

@@ -93,11 +94,12 @@ void main() {
9394

9495
group('Deep Link with PKCE code', () {
9596
late final PkceHttpClient pkceHttpClient;
97+
late final EventChannel appLinksEventChannel;
9698
setUp(() async {
9799
pkceHttpClient = PkceHttpClient();
98100

99101
// Add initial deep link with a `code` parameter
100-
mockAppLink(
102+
appLinksEventChannel = mockAppLink(
101103
initialLink: 'com.supabase://callback/?code=my-code-verifier',
102104
);
103105
await Supabase.initialize(
@@ -118,6 +120,9 @@ void main() {
118120
test(
119121
'Having `code` as the query parameter triggers `getSessionFromUrl` call on initialize',
120122
() async {
123+
// Wait for the initial app link to be handled, as this is an async process
124+
final stream = appLinksEventChannel.receiveBroadcastStream();
125+
await stream.first;
121126
expect(pkceHttpClient.requestCount, 1);
122127
expect(pkceHttpClient.lastRequestBody['auth_code'], 'my-code-verifier');
123128
});

packages/supabase_flutter/test/widget_test_stubs.dart

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,12 @@ class MockEmptyLocalStorage extends LocalStorage {
9494
Future<void> removePersistedSession() async {}
9595
}
9696

97-
/// Registers the mock handler for uni_links
98-
void mockAppLink({String? initialLink}) {
97+
/// Registers the mock handler for app_links
98+
///
99+
/// Returns the [EventChannel] used to mock the incoming links.
100+
EventChannel mockAppLink({String? initialLink}) {
99101
const channel = MethodChannel('com.llfbandit.app_links/messages');
100-
const anotherChannel = MethodChannel('com.llfbandit.app_links/events');
102+
const eventChannel = EventChannel('com.llfbandit.app_links/events');
101103

102104
TestWidgetsFlutterBinding.ensureInitialized();
103105

@@ -107,7 +109,16 @@ void mockAppLink({String? initialLink}) {
107109

108110
// ignore: invalid_null_aware_operator
109111
TestDefaultBinaryMessengerBinding.instance?.defaultBinaryMessenger
110-
.setMockMethodCallHandler(anotherChannel, (message) async => null);
112+
.setMockStreamHandler(
113+
eventChannel,
114+
MockStreamHandler.inline(
115+
onListen: (arguments, events) {
116+
events.success(initialLink);
117+
},
118+
),
119+
);
120+
121+
return eventChannel;
111122
}
112123

113124
class MockAsyncStorage extends GotrueAsyncStorage {

0 commit comments

Comments
 (0)