1
+ import 'package:app_links/app_links.dart' ;
1
2
import 'package:flutter/services.dart' ;
2
3
import 'package:flutter_test/flutter_test.dart' ;
3
4
import 'package:supabase_flutter/supabase_flutter.dart' ;
@@ -95,11 +96,32 @@ void main() {
95
96
group ('Deep Link with PKCE code' , () {
96
97
late final PkceHttpClient pkceHttpClient;
97
98
late final EventChannel appLinksEventChannel;
99
+ late final bool mockEventChannel;
100
+
101
+ /// Check if the current version of AppLinks uses an explicit call to get
102
+ /// the initial link. This is only the case before version 6.0.0, where we
103
+ /// can find the getInitialAppLink function.
104
+ bool appLinksExposesInitialLinkInStream () {
105
+ try {
106
+ // before app_links 6.0.0
107
+ (AppLinks () as dynamic ).getInitialAppLink;
108
+ return false ;
109
+ } on NoSuchMethodError catch (_) {
110
+ return true ;
111
+ }
112
+ }
113
+
98
114
setUp (() async {
99
115
pkceHttpClient = PkceHttpClient ();
100
116
101
- // Add initial deep link with a `code` parameter
117
+ // Add initial deep link with a `code` parameter, use method channel if
118
+ // we are in a version of AppLinks that use the explcit method for
119
+ // getting the initial link. Otherwise we want to mock the event channel
120
+ // and put the initial link there.
121
+ mockEventChannel = appLinksExposesInitialLinkInStream ();
102
122
appLinksEventChannel = mockAppLink (
123
+ mockMethodChannel: ! mockEventChannel,
124
+ mockEventChannel: mockEventChannel,
103
125
initialLink: 'com.supabase://callback/?code=my-code-verifier' ,
104
126
);
105
127
await Supabase .initialize (
@@ -121,8 +143,10 @@ void main() {
121
143
'Having `code` as the query parameter triggers `getSessionFromUrl` call on initialize' ,
122
144
() async {
123
145
// Wait for the initial app link to be handled, as this is an async process
124
- final stream = appLinksEventChannel.receiveBroadcastStream ();
125
- await stream.first;
146
+ if (mockEventChannel) {
147
+ final stream = appLinksEventChannel.receiveBroadcastStream ();
148
+ await stream.first;
149
+ }
126
150
expect (pkceHttpClient.requestCount, 1 );
127
151
expect (pkceHttpClient.lastRequestBody['auth_code' ], 'my-code-verifier' );
128
152
});
0 commit comments