|
| 1 | +import 'package:flutter_test/flutter_test.dart'; |
| 2 | +import 'package:shared_preferences/shared_preferences.dart'; |
| 3 | +import 'package:supabase_flutter/supabase_flutter.dart'; |
| 4 | + |
| 5 | +import 'widget_test_stubs.dart'; |
| 6 | + |
| 7 | +void main() { |
| 8 | + TestWidgetsFlutterBinding.ensureInitialized(); |
| 9 | + |
| 10 | + const supabaseUrl = ''; |
| 11 | + const supabaseKey = ''; |
| 12 | + |
| 13 | + group('Supabase initialization', () { |
| 14 | + setUp(() { |
| 15 | + SharedPreferences.setMockInitialValues({}); |
| 16 | + mockAppLink(); |
| 17 | + }); |
| 18 | + |
| 19 | + tearDown(() async { |
| 20 | + try { |
| 21 | + await Supabase.instance.dispose(); |
| 22 | + } catch (e) { |
| 23 | + // Ignore dispose errors |
| 24 | + } |
| 25 | + }); |
| 26 | + |
| 27 | + group('Basic initialization', () { |
| 28 | + test('initialize successfully with default options', () async { |
| 29 | + await Supabase.initialize( |
| 30 | + url: supabaseUrl, |
| 31 | + anonKey: supabaseKey, |
| 32 | + ); |
| 33 | + |
| 34 | + expect(Supabase.instance, isNotNull); |
| 35 | + expect(Supabase.instance.client, isNotNull); |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + group('Custom storage initialization', () { |
| 40 | + test('initialize successfully with custom localStorage', () async { |
| 41 | + final localStorage = MockLocalStorage(); |
| 42 | + await Supabase.initialize( |
| 43 | + url: supabaseUrl, |
| 44 | + anonKey: supabaseKey, |
| 45 | + authOptions: FlutterAuthClientOptions( |
| 46 | + localStorage: localStorage, |
| 47 | + ), |
| 48 | + ); |
| 49 | + |
| 50 | + expect(Supabase.instance, isNotNull); |
| 51 | + expect(Supabase.instance.client, isNotNull); |
| 52 | + }); |
| 53 | + |
| 54 | + test('handles initialization with expired session in storage', () async { |
| 55 | + await Supabase.initialize( |
| 56 | + url: supabaseUrl, |
| 57 | + anonKey: supabaseKey, |
| 58 | + debug: true, |
| 59 | + authOptions: FlutterAuthClientOptions( |
| 60 | + localStorage: MockExpiredStorage(), |
| 61 | + pkceAsyncStorage: MockAsyncStorage(), |
| 62 | + ), |
| 63 | + ); |
| 64 | + |
| 65 | + // Should handle expired session gracefully |
| 66 | + expect(Supabase.instance.client.auth.currentSession, isNotNull); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + group('Auth options initialization', () { |
| 71 | + test('initialize successfully with PKCE auth flow', () async { |
| 72 | + await Supabase.initialize( |
| 73 | + url: supabaseUrl, |
| 74 | + anonKey: supabaseKey, |
| 75 | + authOptions: const FlutterAuthClientOptions( |
| 76 | + authFlowType: AuthFlowType.pkce, |
| 77 | + ), |
| 78 | + ); |
| 79 | + |
| 80 | + expect(Supabase.instance, isNotNull); |
| 81 | + expect(Supabase.instance.client, isNotNull); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + group('Custom client initialization', () { |
| 86 | + test('initialize successfully with custom HTTP client', () async { |
| 87 | + final httpClient = PkceHttpClient(); |
| 88 | + await Supabase.initialize( |
| 89 | + url: supabaseUrl, |
| 90 | + anonKey: supabaseKey, |
| 91 | + httpClient: httpClient, |
| 92 | + ); |
| 93 | + |
| 94 | + expect(Supabase.instance, isNotNull); |
| 95 | + expect(Supabase.instance.client, isNotNull); |
| 96 | + }); |
| 97 | + |
| 98 | + test('initialize successfully with custom access token', () async { |
| 99 | + await Supabase.initialize( |
| 100 | + url: supabaseUrl, |
| 101 | + anonKey: supabaseKey, |
| 102 | + accessToken: () async => 'custom-access-token', |
| 103 | + ); |
| 104 | + |
| 105 | + expect(Supabase.instance, isNotNull); |
| 106 | + expect(Supabase.instance.client, isNotNull); |
| 107 | + |
| 108 | + // Should throw AuthException when trying to access auth |
| 109 | + expect( |
| 110 | + () => Supabase.instance.client.auth, |
| 111 | + throwsA(isA<AuthException>()), |
| 112 | + ); |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + group('Multiple initialization and disposal', () { |
| 117 | + test('dispose and reinitialize works', () async { |
| 118 | + // First initialization |
| 119 | + await Supabase.initialize( |
| 120 | + url: supabaseUrl, |
| 121 | + anonKey: supabaseKey, |
| 122 | + ); |
| 123 | + |
| 124 | + expect(Supabase.instance, isNotNull); |
| 125 | + |
| 126 | + // Dispose |
| 127 | + await Supabase.instance.dispose(); |
| 128 | + |
| 129 | + // Need to run the event loop to let the dispose complete |
| 130 | + await Future.delayed(Duration.zero); |
| 131 | + |
| 132 | + // Re-initialize should work without errors |
| 133 | + await Supabase.initialize( |
| 134 | + url: supabaseUrl, |
| 135 | + anonKey: supabaseKey, |
| 136 | + ); |
| 137 | + |
| 138 | + expect(Supabase.instance, isNotNull); |
| 139 | + }); |
| 140 | + |
| 141 | + test('handles multiple initializations correctly', () async { |
| 142 | + await Supabase.initialize( |
| 143 | + url: supabaseUrl, |
| 144 | + anonKey: supabaseKey, |
| 145 | + debug: false, |
| 146 | + authOptions: FlutterAuthClientOptions( |
| 147 | + localStorage: MockLocalStorage(), |
| 148 | + pkceAsyncStorage: MockAsyncStorage(), |
| 149 | + ), |
| 150 | + ); |
| 151 | + |
| 152 | + // Store first instance to verify it's different after re-initialization |
| 153 | + final firstInstance = Supabase.instance.client; |
| 154 | + |
| 155 | + // Dispose first instance before re-initializing |
| 156 | + await Supabase.instance.dispose(); |
| 157 | + |
| 158 | + await Supabase.initialize( |
| 159 | + url: supabaseUrl, |
| 160 | + anonKey: supabaseKey, |
| 161 | + debug: true, |
| 162 | + authOptions: FlutterAuthClientOptions( |
| 163 | + localStorage: MockEmptyLocalStorage(), |
| 164 | + pkceAsyncStorage: MockAsyncStorage(), |
| 165 | + ), |
| 166 | + ); |
| 167 | + |
| 168 | + final secondInstance = Supabase.instance.client; |
| 169 | + expect(secondInstance, isNotNull); |
| 170 | + expect(identical(firstInstance, secondInstance), isFalse); |
| 171 | + }); |
| 172 | + }); |
| 173 | + }); |
| 174 | +} |
0 commit comments