diff --git a/lib/core/token_storage.dart b/lib/core/token_storage.dart index 871140c7..9925f9aa 100644 --- a/lib/core/token_storage.dart +++ b/lib/core/token_storage.dart @@ -1,3 +1,4 @@ +import 'package:flutter/foundation.dart'; import 'package:shared_preferences/shared_preferences.dart'; abstract class TokenStorage { @@ -8,6 +9,13 @@ abstract class TokenStorage { return _cachedPrefs!; } + /// Clears the cached SharedPreferences instance. + /// This is primarily used for testing to ensure fresh state between tests. + @visibleForTesting + static void clearCache() { + _cachedPrefs = null; + } + static Future haveTokenInStorage() async { try { final sharedPreferences = await _getPrefs(); diff --git a/test/core/routing/auth_routes_test.dart b/test/core/routing/auth_routes_test.dart index 6437b14d..41ce977d 100644 --- a/test/core/routing/auth_routes_test.dart +++ b/test/core/routing/auth_routes_test.dart @@ -12,10 +12,7 @@ void main() { test('createRoutes содержит маршрут authScreen', () { final routes = AuthRoutes.createRoutes(); - final authRoute = routes.firstWhere( - (route) => route is GoRoute && route.path == '/auth-screen', - orElse: () => throw Exception('Route not found'), - ); + final authRoute = routes.firstWhere((route) => route is GoRoute && route.path == '/auth-screen', orElse: () => throw Exception('Route not found')); expect(authRoute, isA()); }); @@ -67,10 +64,7 @@ void main() { test('createRoutes содержит маршрут verifyScreen', () { final routes = AuthRoutes.createRoutes(); - final verifyRoute = routes.firstWhere( - (route) => route is GoRoute && route.path == '/verify-screen', - orElse: () => throw Exception('Route not found'), - ); + final verifyRoute = routes.firstWhere((route) => route is GoRoute && route.path == '/verify-screen', orElse: () => throw Exception('Route not found')); expect(verifyRoute, isA()); }); @@ -92,44 +86,25 @@ void main() { expect(registerViaSocialRoute, isA()); }); - test('authScreen имеет redirect функцию', () { - final routes = AuthRoutes.createRoutes(); - final authRoute = routes.first as GoRoute; - expect(authRoute.redirect, isNotNull); - }); - test('forgetPasswordScreen builder обрабатывает extra данные', () { final routes = AuthRoutes.createRoutes(); final authRoute = routes.first as GoRoute; - final forgetPasswordRoute = authRoute.routes.firstWhere( - (route) => route is GoRoute && route.path == 'forget-password-screen', - orElse: () => throw Exception('Route not found'), - ) as GoRoute; + final forgetPasswordRoute = + authRoute.routes.firstWhere((route) => route is GoRoute && route.path == 'forget-password-screen', orElse: () => throw Exception('Route not found')) + as GoRoute; expect(forgetPasswordRoute.builder, isNotNull); }); test('verifyScreen builder обрабатывает extra данные', () { final routes = AuthRoutes.createRoutes(); - final verifyRoute = routes.firstWhere( - (route) => route is GoRoute && route.path == '/verify-screen', - ) as GoRoute; + final verifyRoute = routes.firstWhere((route) => route is GoRoute && route.path == '/verify-screen') as GoRoute; expect(verifyRoute.builder, isNotNull); }); test('registerViaSocialScreen builder обрабатывает extra данные', () { final routes = AuthRoutes.createRoutes(); - final registerViaSocialRoute = routes.firstWhere( - (route) => route is GoRoute && route.path == '/register-via-social-screen', - ) as GoRoute; + final registerViaSocialRoute = routes.firstWhere((route) => route is GoRoute && route.path == '/register-via-social-screen') as GoRoute; expect(registerViaSocialRoute.builder, isNotNull); }); }); } - - - - - - - - diff --git a/test/core/token_storage_test.dart b/test/core/token_storage_test.dart index 403339ce..de8fb3b1 100644 --- a/test/core/token_storage_test.dart +++ b/test/core/token_storage_test.dart @@ -6,6 +6,10 @@ void main() { group('TokenStorage', () { setUp(() async { SharedPreferences.setMockInitialValues({}); + // Clear cached instance to ensure fresh state for each test + TokenStorage.clearCache(); + final prefs = await SharedPreferences.getInstance(); + await prefs.clear(); }); test('haveTokenInStorage returns false when no token is stored', () async { @@ -66,6 +70,10 @@ void main() { group('SocketIdStorage', () { setUp(() async { SharedPreferences.setMockInitialValues({}); + // Clear cached instance to ensure fresh state for each test + TokenStorage.clearCache(); + final prefs = await SharedPreferences.getInstance(); + await prefs.clear(); }); test('haveSocketIdInStorage returns false when no socketId is stored', () async {