feat(flutter): 优化登录页与底部导航
This commit is contained in:
1 parent
e52d81881c
commit
e609e3ad8d
12 files changed
+702
-189
No files matched your search
+223
-21
@@ -1,9 +1,12 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:shared_preferences_platform_interface/in_memory_shared_preferences_async.dart';
|
||||
import 'package:shared_preferences_platform_interface/shared_preferences_async_platform_interface.dart';
|
||||
|
||||
import 'package:fruitshorts/core/data/models/auth_session.dart';
|
||||
import 'package:fruitshorts/core/data/remote/auth_api.dart';
|
||||
import 'package:fruitshorts/core/data/remote/api_client.dart';
|
||||
import 'package:fruitshorts/core/data/services/auth_controller.dart';
|
||||
@@ -20,15 +23,22 @@ void main() {
|
||||
InMemorySharedPreferencesAsync.empty();
|
||||
});
|
||||
|
||||
Future<void> pumpAuthPage(WidgetTester tester, {bool signUp = false}) async {
|
||||
Future<void> pumpAuthPage(
|
||||
WidgetTester tester, {
|
||||
bool signUp = false,
|
||||
AuthApi? api,
|
||||
}) async {
|
||||
final auth = AuthController(
|
||||
AuthApi(
|
||||
// 这个测试不联网:请求一律 500,正好用于验证"本地校验没过就不发请求"
|
||||
ApiClient(
|
||||
baseUrl: 'http://127.0.0.1:1',
|
||||
adapter: FakeHttpAdapter((_) async => FakeHttpResponse('{}', 500)),
|
||||
),
|
||||
),
|
||||
api ??
|
||||
AuthApi(
|
||||
// 这个测试不联网:请求一律 500,正好用于验证"本地校验没过就不发请求"
|
||||
ApiClient(
|
||||
baseUrl: 'http://127.0.0.1:1',
|
||||
adapter: FakeHttpAdapter(
|
||||
(_) async => FakeHttpResponse('{}', 500),
|
||||
),
|
||||
),
|
||||
),
|
||||
AuthSessionStore(),
|
||||
);
|
||||
await tester.pumpWidget(
|
||||
@@ -45,7 +55,9 @@ void main() {
|
||||
await tester.pumpAndSettle();
|
||||
}
|
||||
|
||||
testWidgets('should_show_email_error_and_stay_on_page_when_form_is_empty', (tester) async {
|
||||
testWidgets('should_show_email_error_and_stay_on_page_when_form_is_empty', (
|
||||
tester,
|
||||
) async {
|
||||
await pumpAuthPage(tester);
|
||||
|
||||
expect(find.text('Welcome back'), findsOneWidget);
|
||||
@@ -57,25 +69,215 @@ void main() {
|
||||
expect(find.text('Welcome back'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('should_reject_weak_password_locally_when_only_letters_are_typed', (tester) async {
|
||||
testWidgets(
|
||||
'should_reject_weak_password_locally_when_only_letters_are_typed',
|
||||
(tester) async {
|
||||
await pumpAuthPage(tester);
|
||||
|
||||
await tester.enterText(find.byType(TextField).first, 'a@b.com');
|
||||
await tester.enterText(find.byType(TextField).last, 'abcdefgh');
|
||||
await tester.tap(find.byType(FilledButton));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(
|
||||
find.text('Password needs both letters and numbers'),
|
||||
findsOneWidget,
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets(
|
||||
'should_flip_between_sign_in_and_sign_up_when_switch_link_is_tapped',
|
||||
(tester) async {
|
||||
await pumpAuthPage(tester);
|
||||
expect(find.text('Welcome back'), findsOneWidget);
|
||||
|
||||
await tester.tap(find.text('New here? Create an account'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('Create your account'), findsOneWidget);
|
||||
expect(find.text('Already have an account? Sign in'), findsOneWidget);
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets('should_show_brand_and_form_accessibility_when_page_opens', (
|
||||
tester,
|
||||
) async {
|
||||
await pumpAuthPage(tester);
|
||||
|
||||
await tester.enterText(find.byType(TextField).first, 'a@b.com');
|
||||
await tester.enterText(find.byType(TextField).last, 'abcdefgh');
|
||||
await tester.tap(find.byType(FilledButton));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('Password needs both letters and numbers'), findsOneWidget);
|
||||
expect(find.text('FruitShorts'), findsOneWidget);
|
||||
expect(find.text('Short dramas made for Indonesia'), findsOneWidget);
|
||||
final email = tester.widget<TextField>(find.byKey(const Key('auth-email')));
|
||||
final password = tester.widget<TextField>(
|
||||
find.byKey(const Key('auth-password')),
|
||||
);
|
||||
expect(email.autofillHints, contains(AutofillHints.email));
|
||||
expect(password.autofillHints, contains(AutofillHints.password));
|
||||
expect(
|
||||
tester.getSize(find.byKey(const Key('auth-submit'))).height,
|
||||
greaterThanOrEqualTo(44),
|
||||
);
|
||||
expect(
|
||||
tester.getSize(find.byKey(const Key('auth-password-visibility'))).height,
|
||||
greaterThanOrEqualTo(44),
|
||||
);
|
||||
expect(
|
||||
tester.getSize(find.byKey(const Key('auth-switch-mode'))).height,
|
||||
greaterThanOrEqualTo(44),
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('should_flip_between_sign_in_and_sign_up_when_switch_link_is_tapped', (tester) async {
|
||||
testWidgets('should_toggle_password_visibility_when_eye_button_is_tapped', (
|
||||
tester,
|
||||
) async {
|
||||
await pumpAuthPage(tester);
|
||||
expect(find.text('Welcome back'), findsOneWidget);
|
||||
expect(
|
||||
tester
|
||||
.widget<TextField>(find.byKey(const Key('auth-password')))
|
||||
.obscureText,
|
||||
isTrue,
|
||||
);
|
||||
|
||||
await tester.tap(find.text('New here? Create an account'));
|
||||
await tester.tap(find.byKey(const Key('auth-password-visibility')));
|
||||
await tester.pump();
|
||||
|
||||
expect(
|
||||
tester
|
||||
.widget<TextField>(find.byKey(const Key('auth-password')))
|
||||
.obscureText,
|
||||
isFalse,
|
||||
);
|
||||
expect(find.byTooltip('Hide password'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('should_move_focus_from_email_to_password_when_next_is_pressed', (
|
||||
tester,
|
||||
) async {
|
||||
await pumpAuthPage(tester);
|
||||
await tester.tap(find.byKey(const Key('auth-email')));
|
||||
await tester.testTextInput.receiveAction(TextInputAction.next);
|
||||
await tester.pump();
|
||||
|
||||
expect(
|
||||
FocusManager.instance.primaryFocus?.debugLabel,
|
||||
'auth-password-focus',
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('should_disable_form_and_submit_while_done_action_is_pending', (
|
||||
tester,
|
||||
) async {
|
||||
final api = _PendingAuthApi();
|
||||
await pumpAuthPage(tester, api: api);
|
||||
await tester.enterText(find.byKey(const Key('auth-email')), 'a@b.com');
|
||||
await tester.enterText(find.byKey(const Key('auth-password')), 'abcd1234');
|
||||
await tester.tap(find.byKey(const Key('auth-password')));
|
||||
await tester.testTextInput.receiveAction(TextInputAction.done);
|
||||
await tester.pump();
|
||||
|
||||
expect(api.loginCalls, 1);
|
||||
expect(
|
||||
tester.widget<TextField>(find.byKey(const Key('auth-email'))).enabled,
|
||||
isFalse,
|
||||
);
|
||||
expect(
|
||||
tester.widget<TextField>(find.byKey(const Key('auth-password'))).enabled,
|
||||
isFalse,
|
||||
);
|
||||
expect(
|
||||
tester
|
||||
.widget<IconButton>(find.byKey(const Key('auth-password-visibility')))
|
||||
.onPressed,
|
||||
isNull,
|
||||
);
|
||||
expect(
|
||||
tester
|
||||
.widget<FilledButton>(find.byKey(const Key('auth-submit')))
|
||||
.onPressed,
|
||||
isNull,
|
||||
);
|
||||
expect(
|
||||
tester
|
||||
.widget<TextButton>(find.byKey(const Key('auth-switch-mode')))
|
||||
.onPressed,
|
||||
isNull,
|
||||
);
|
||||
|
||||
api.release();
|
||||
await tester.pumpAndSettle();
|
||||
});
|
||||
|
||||
testWidgets('should_render_validation_error_with_theme_semantic_color', (
|
||||
tester,
|
||||
) async {
|
||||
await pumpAuthPage(tester);
|
||||
await tester.enterText(find.byKey(const Key('auth-email')), 'a@b.com');
|
||||
await tester.enterText(find.byKey(const Key('auth-password')), 'abcdefgh');
|
||||
await tester.tap(find.byKey(const Key('auth-submit')));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('Create your account'), findsOneWidget);
|
||||
expect(find.text('Already have an account? Sign in'), findsOneWidget);
|
||||
final errorFinder = find.byKey(const Key('auth-error'));
|
||||
final error = tester.widget<Text>(errorFinder);
|
||||
final expected = Theme.of(tester.element(errorFinder)).colorScheme.error;
|
||||
expect(error.style?.color, expected);
|
||||
});
|
||||
|
||||
testWidgets(
|
||||
'should_keep_submit_reachable_when_keyboard_opens_on_small_screen',
|
||||
(tester) async {
|
||||
tester.view.physicalSize = const Size(320, 568);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
tester.view.viewInsets = const FakeViewPadding(bottom: 260);
|
||||
addTearDown(tester.view.resetPhysicalSize);
|
||||
addTearDown(tester.view.resetDevicePixelRatio);
|
||||
addTearDown(tester.view.resetViewInsets);
|
||||
|
||||
await pumpAuthPage(tester);
|
||||
await tester.scrollUntilVisible(
|
||||
find.byKey(const Key('auth-submit')),
|
||||
120,
|
||||
scrollable: find
|
||||
.descendant(
|
||||
of: find.byKey(const Key('auth-scroll')),
|
||||
matching: find.byType(Scrollable),
|
||||
)
|
||||
.first,
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(
|
||||
find.byKey(const Key('auth-submit')).hitTestable(),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(tester.takeException(), isNull);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
class _PendingAuthApi extends AuthApi {
|
||||
_PendingAuthApi()
|
||||
: super(
|
||||
ApiClient(
|
||||
baseUrl: 'http://127.0.0.1:1',
|
||||
adapter: FakeHttpAdapter((_) async => FakeHttpResponse('{}', 500)),
|
||||
),
|
||||
);
|
||||
|
||||
final _gate = Completer<void>();
|
||||
int loginCalls = 0;
|
||||
|
||||
void release() => _gate.complete();
|
||||
|
||||
@override
|
||||
Future<AuthSession> login({
|
||||
required String email,
|
||||
required String password,
|
||||
required String deviceId,
|
||||
String platform = '',
|
||||
String appVersion = '',
|
||||
}) async {
|
||||
loginCalls++;
|
||||
await _gate.future;
|
||||
throw const ApiException(500, 'pending failure');
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user