fix(flutter): 评论输入增加登录闸

This commit is contained in:
Zhou Weidong committed 2026-10-20 16:05:47 +08:00
1 parent 1eea9fafe7
commit d0c1a5f499
9 files changed
+177 -41

No files matched your search

+107 -5
View File
@@ -1,5 +1,7 @@
// Author: fruit.com.cn
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:fruitshorts/core/data/models/comment.dart';
@@ -41,6 +43,71 @@ void main() {
});
});
testWidgets('should_gate_guest_comment_input_without_focus', (tester) async {
SharedPreferencesAsyncPlatform.instance =
InMemorySharedPreferencesAsync.empty();
final repository = FakeCommentRepository(pages: [const CommentPage()]);
await _pumpSheet(tester, repository: repository);
final input = find.byKey(const Key('comment-input'));
final field = tester.widget<TextField>(input);
expect(field.decoration?.hintText, 'Sign in to comment');
expect(field.focusNode?.hasFocus, isFalse);
await tester.tap(input);
await tester.pumpAndSettle();
expect(find.text('Sign in to continue'), findsOneWidget);
expect(tester.widget<TextField>(input).focusNode?.hasFocus, isFalse);
await tester.tap(find.text('Not now'));
await tester.pumpAndSettle();
expect(tester.widget<TextField>(input).focusNode?.hasFocus, isFalse);
});
testWidgets('should_focus_comment_input_after_successful_login', (
tester,
) async {
SharedPreferencesAsyncPlatform.instance =
InMemorySharedPreferencesAsync.empty();
final repository = FakeCommentRepository(pages: [const CommentPage()]);
final auth = await _pumpSheet(tester, repository: repository);
await tester.tap(find.byKey(const Key('comment-input')));
await tester.pump();
await tester.pump(const Duration(milliseconds: 300));
await tester.tap(find.text('Sign In'));
await tester.pump();
await tester.pump(const Duration(milliseconds: 300));
(auth as _TestAuthController).setSignedIn(true);
await tester.pump();
Navigator.of(tester.element(find.byKey(const Key('auth-landing')))).pop();
await tester.pump();
await tester.pump(const Duration(milliseconds: 300));
expect(
tester
.widget<TextField>(find.byKey(const Key('comment-input')))
.focusNode
?.hasFocus,
isTrue,
);
});
testWidgets('should_gate_guest_post_without_creating_comment', (
tester,
) async {
SharedPreferencesAsyncPlatform.instance =
InMemorySharedPreferencesAsync.empty();
final repository = FakeCommentRepository(pages: [const CommentPage()]);
await _pumpSheet(tester, repository: repository);
await tester.tap(find.byKey(const Key('comment-post')));
await tester.pumpAndSettle();
expect(find.text('Sign in to continue'), findsOneWidget);
expect(repository.publishCalls, isEmpty);
});
testWidgets('should_show_server_comments_and_append_next_cursor_page', (
tester,
) async {
@@ -392,7 +459,7 @@ void main() {
});
}
Future<void> _pumpSheet(
Future<AuthController> _pumpSheet(
WidgetTester tester, {
required FakeCommentRepository repository,
FakeInteractionRepository? interactions,
@@ -421,16 +488,51 @@ Future<void> _pumpSheet(
),
);
await tester.pumpAndSettle();
return auth;
}
Future<AuthController> _auth() async {
final client = ApiClient(
baseUrl: 'http://127.0.0.1:8081',
adapter: FakeHttpAdapter(
(_) async => FakeHttpResponse('{"code":0,"message":"ok","data":null}'),
),
adapter: FakeHttpAdapter((options) async {
if (options.uri.path == '/app/auth/login') {
return FakeHttpResponse(
jsonEncode({
'code': 0,
'message': 'ok',
'data': {
'accessToken': 'access',
'refreshToken': 'refresh',
'expiresIn': 7200,
'user': {'uid': '9', 'nickname': 'Viewer', 'avatarUrl': ''},
},
}),
);
}
return FakeHttpResponse('{"code":0,"message":"ok","data":null}');
}),
);
final store = AuthSessionStore();
final session = await store.read();
final auth = _TestAuthController(
AuthApi(client),
store,
signedIn: session != null,
);
final auth = AuthController(AuthApi(client), AuthSessionStore());
await auth.load();
return auth;
}
class _TestAuthController extends AuthController {
_TestAuthController(super.api, super.store, {required this._signedIn});
bool _signedIn;
@override
bool get isSignedIn => _signedIn;
void setSignedIn(bool value) {
_signedIn = value;
notifyListeners();
}
}