Files
social-app/apps/test/widget_test.dart
T

88 lines
2.8 KiB
Dart
Raw Normal View History

2026-02-24 18:18:42 +08:00
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
2026-02-25 15:25:31 +08:00
import 'package:mocktail/mocktail.dart';
2026-02-24 18:18:42 +08:00
import 'package:social_app/main.dart';
2026-02-25 15:25:31 +08:00
import 'package:social_app/features/auth/presentation/bloc/auth_bloc.dart';
import 'package:social_app/features/auth/presentation/bloc/auth_state.dart';
import 'package:social_app/features/auth/data/auth_repository.dart';
import 'package:social_app/core/di/injection.dart';
2026-02-25 15:25:31 +08:00
class MockAuthBloc extends Mock implements AuthBloc {}
class MockAuthRepository extends Mock implements AuthRepository {}
2026-02-25 15:25:31 +08:00
class FakeAuthState extends Fake implements AuthState {}
2026-02-24 18:18:42 +08:00
void main() {
2026-02-25 15:25:31 +08:00
setUpAll(() {
registerFallbackValue(FakeAuthState());
});
setUp(() async {
if (sl.isRegistered<AuthRepository>()) {
await sl.reset();
}
sl.registerSingleton<AuthRepository>(MockAuthRepository());
});
2026-02-24 18:18:42 +08:00
testWidgets('Login screen loads correctly', (WidgetTester tester) async {
2026-02-25 15:25:31 +08:00
final mockAuthBloc = MockAuthBloc();
when(() => mockAuthBloc.state).thenReturn(AuthInitial());
when(
() => mockAuthBloc.stream,
).thenAnswer((_) => Stream.value(AuthInitial()));
await tester.pumpWidget(LinksyApp(authBloc: mockAuthBloc));
2026-02-24 18:18:42 +08:00
expect(find.text('linksy'), findsOneWidget);
expect(find.text('登录'), findsOneWidget);
2026-02-24 18:18:42 +08:00
expect(find.text('还没有账号?去注册'), findsOneWidget);
});
testWidgets('Main content is vertically centered above footer', (
WidgetTester tester,
) async {
2026-02-25 15:25:31 +08:00
final mockAuthBloc = MockAuthBloc();
when(() => mockAuthBloc.state).thenReturn(AuthInitial());
when(
() => mockAuthBloc.stream,
).thenAnswer((_) => Stream.value(AuthInitial()));
await tester.pumpWidget(LinksyApp(authBloc: mockAuthBloc));
2026-02-24 18:18:42 +08:00
final safeAreaRect = tester.getRect(find.byType(SafeArea));
final mainRect = tester.getRect(
find.byKey(const Key('login_main_content')),
);
final footerRect = tester.getRect(find.byKey(const Key('login_footer')));
final topSpace = mainRect.top - safeAreaRect.top;
final bottomSpace = footerRect.top - mainRect.bottom;
expect((topSpace - bottomSpace).abs(), lessThanOrEqualTo(2));
});
testWidgets('Login screen does not overflow when keyboard is visible', (
WidgetTester tester,
) async {
final mockAuthBloc = MockAuthBloc();
when(() => mockAuthBloc.state).thenReturn(AuthInitial());
when(
() => mockAuthBloc.stream,
).thenAnswer((_) => Stream.value(AuthInitial()));
await tester.pumpWidget(
MediaQuery(
data: const MediaQueryData(
size: Size(390, 844),
viewInsets: EdgeInsets.only(bottom: 320),
),
child: LinksyApp(authBloc: mockAuthBloc),
),
);
await tester.pumpAndSettle();
expect(tester.takeException(), isNull);
expect(find.text('登录'), findsOneWidget);
});
2026-02-24 18:18:42 +08:00
}