Skip to content

Commit ff8b67e

Browse files
committed
test: support web test for supabase_flutter
1 parent 1692299 commit ff8b67e

File tree

2 files changed

+75
-61
lines changed

2 files changed

+75
-61
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
@TestOn('!browser')
2+
3+
import 'package:app_links/app_links.dart';
4+
import 'package:flutter_test/flutter_test.dart';
5+
import 'package:supabase_flutter/supabase_flutter.dart';
6+
7+
import 'widget_test_stubs.dart';
8+
9+
void main() {
10+
const supabaseUrl = '';
11+
const supabaseKey = '';
12+
13+
group('Deep Link with PKCE code', () {
14+
late final PkceHttpClient pkceHttpClient;
15+
late final bool mockEventChannel;
16+
17+
/// Check if the current version of AppLinks uses an explicit call to get
18+
/// the initial link. This is only the case before version 6.0.0, where we
19+
/// can find the getInitialAppLink function.
20+
///
21+
/// CI pipeline is set so that it tests both app_links newer and older than v6.0.0
22+
bool appLinksExposesInitialLinkInStream() {
23+
try {
24+
// before app_links 6.0.0
25+
(AppLinks() as dynamic).getInitialAppLink;
26+
return false;
27+
} on NoSuchMethodError catch (_) {
28+
return true;
29+
}
30+
}
31+
32+
setUp(() async {
33+
pkceHttpClient = PkceHttpClient();
34+
35+
// Add initial deep link with a `code` parameter, use method channel if
36+
// we are in a version of AppLinks that use the explcit method for
37+
// getting the initial link. Otherwise we want to mock the event channel
38+
// and put the initial link there.
39+
mockEventChannel = appLinksExposesInitialLinkInStream();
40+
mockAppLink(
41+
mockMethodChannel: !mockEventChannel,
42+
mockEventChannel: mockEventChannel,
43+
initialLink: 'com.supabase://callback/?code=my-code-verifier',
44+
);
45+
await Supabase.initialize(
46+
url: supabaseUrl,
47+
anonKey: supabaseKey,
48+
debug: false,
49+
httpClient: pkceHttpClient,
50+
authOptions: FlutterAuthClientOptions(
51+
localStorage: MockEmptyLocalStorage(),
52+
pkceAsyncStorage: MockAsyncStorage()
53+
..setItem(
54+
key: 'supabase.auth.token-code-verifier',
55+
value: 'raw-code-verifier'),
56+
),
57+
);
58+
});
59+
60+
test(
61+
'Having `code` as the query parameter triggers `getSessionFromUrl` call on initialize',
62+
() async {
63+
// Wait for the initial app link to be handled, as this is an async
64+
// process when mocking the event channel.
65+
if (mockEventChannel) {
66+
await Future.delayed(const Duration(milliseconds: 500));
67+
}
68+
expect(pkceHttpClient.requestCount, 1);
69+
expect(pkceHttpClient.lastRequestBody['auth_code'], 'my-code-verifier');
70+
});
71+
});
72+
}

packages/supabase_flutter/test/supabase_flutter_test.dart

Lines changed: 3 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,14 @@ void main() {
7979
authOptions: FlutterAuthClientOptions(
8080
localStorage: MockExpiredStorage(),
8181
pkceAsyncStorage: MockAsyncStorage(),
82+
autoRefreshToken: false,
8283
),
8384
);
8485
});
8586

86-
test('initial session contains the error', () async {
87+
test('emits exception when no auto refresh', () async {
8788
// Give it a delay to wait for recoverSession to throw
88-
await Future.delayed(const Duration(milliseconds: 10));
89+
await Future.delayed(const Duration(milliseconds: 100));
8990

9091
await expectLater(Supabase.instance.client.auth.onAuthStateChange,
9192
emitsError(isA<AuthException>()));
@@ -113,65 +114,6 @@ void main() {
113114
});
114115
});
115116

116-
group('Deep Link with PKCE code', () {
117-
late final PkceHttpClient pkceHttpClient;
118-
late final bool mockEventChannel;
119-
120-
/// Check if the current version of AppLinks uses an explicit call to get
121-
/// the initial link. This is only the case before version 6.0.0, where we
122-
/// can find the getInitialAppLink function.
123-
///
124-
/// CI pipeline is set so that it tests both app_links newer and older than v6.0.0
125-
bool appLinksExposesInitialLinkInStream() {
126-
try {
127-
// before app_links 6.0.0
128-
(AppLinks() as dynamic).getInitialAppLink;
129-
return false;
130-
} on NoSuchMethodError catch (_) {
131-
return true;
132-
}
133-
}
134-
135-
setUp(() async {
136-
pkceHttpClient = PkceHttpClient();
137-
138-
// Add initial deep link with a `code` parameter, use method channel if
139-
// we are in a version of AppLinks that use the explcit method for
140-
// getting the initial link. Otherwise we want to mock the event channel
141-
// and put the initial link there.
142-
mockEventChannel = appLinksExposesInitialLinkInStream();
143-
mockAppLink(
144-
mockMethodChannel: !mockEventChannel,
145-
mockEventChannel: mockEventChannel,
146-
initialLink: 'com.supabase://callback/?code=my-code-verifier',
147-
);
148-
await Supabase.initialize(
149-
url: supabaseUrl,
150-
anonKey: supabaseKey,
151-
debug: false,
152-
httpClient: pkceHttpClient,
153-
authOptions: FlutterAuthClientOptions(
154-
localStorage: MockEmptyLocalStorage(),
155-
pkceAsyncStorage: MockAsyncStorage()
156-
..setItem(
157-
key: 'supabase.auth.token-code-verifier',
158-
value: 'raw-code-verifier'),
159-
),
160-
);
161-
});
162-
163-
test(
164-
'Having `code` as the query parameter triggers `getSessionFromUrl` call on initialize',
165-
() async {
166-
// Wait for the initial app link to be handled, as this is an async
167-
// process when mocking the event channel.
168-
if (mockEventChannel) {
169-
await Future.delayed(const Duration(milliseconds: 500));
170-
}
171-
expect(pkceHttpClient.requestCount, 1);
172-
expect(pkceHttpClient.lastRequestBody['auth_code'], 'my-code-verifier');
173-
});
174-
});
175117
group('EmptyLocalStorage', () {
176118
late EmptyLocalStorage localStorage;
177119

0 commit comments

Comments
 (0)